Pagini recente » Monitorul de evaluare | Statistici Oncea Alexandru (Alekapas) | Monitorul de evaluare | Diferente pentru runda/anaconda intre reviziile 2 si 1 | Cod sursa (job #1551187)
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
ifstream fin( "cuplaj.in" ); ofstream fout( "cuplaj.out" );
typedef vector< int > graf;
const int nmax = 1e4;
bool viz[ nmax + 1 ];
int l[ nmax + 1 ];
graf g[ nmax + 1 ];
int pair_up( int nod ) {
if ( viz[ nod ] == 1 ) {
return 0;
}
viz[ nod ] = 1;
for( graf::iterator it = g[ nod ].begin(); it != g[ nod ].end(); ++ it ) {
if ( l[ *it ] == 0 || pair_up( l[ *it ] ) ) {
l[ *it ] = nod;
return 1;
}
}
return 0;
}
int main() {
int n, m, e;
fin >> n >> m >> e;
for( int i = 0; i < e; ++ i ) {
int x, y;
fin >> x >> y;
g[ x ].push_back( y );
}
int ans = 0;
bool ok = 1;
while( ok == 1 ) {
memset( viz, 0, sizeof( viz ) );
ok = 0;
for( int i = 1; i <= n; ++ i ) {
if ( l[ i ] == 0 && pair_up( i ) ) {
++ ans;
ok = 1;
}
}
}
fout << ans << "\n";
for( int i = 1; i <= m; ++ i ) {
if ( l[ i ] > 0 ) {
fout << l[ i ] << " " << i << "\n";
}
}
fin.close();
fout.close();
return 0;
}