Pagini recente » Cod sursa (job #379246) | Cod sursa (job #3141197) | Cod sursa (job #3163151) | Cod sursa (job #474517) | Cod sursa (job #862192)
Cod sursa(job #862192)
#include <cstdio>
#include <vector>
using namespace std;
#define max_n 20005
#define pb push_back
int n,m,e,i,a,b,rez;
bool Viz[ max_n ], Cuplaj[ max_n ];
int Other[ max_n ];
vector<int> T[ max_n ];
bool df ( int nod ){
Viz[ nod ] = 1;
if ( !Cuplaj[ nod ] ){
Cuplaj[ nod ] = 1;
return 1;
}
if ( nod > n ){
return df ( Other[ nod ] );
}
// nodul e din partea stanga.
for ( int i=0; i<int ( T[ nod ].size() ); ++i ){
if ( !Viz[ T[ nod ][ i ] ] ){
if ( df ( T[ nod ][ i ] ) ){
Other[ nod ] = T[ nod ][ i ];
Other[ T[ nod ][ i ] ] = nod;
return 1;
}
}
}
return 0;
}
int main(){
freopen ("cuplaj.in","r",stdin);
freopen ("cuplaj.out","w",stdout);
scanf ("%d %d %d", &n, &m, &e );
for ( i=1; i<=e; ++i ){
scanf ("%d %d", &a, &b );
b+=n;
T[ a ].pb ( b );
T[ b ].pb ( a );
}
for ( i=1; i<=n; ++i ){
if ( !Cuplaj[ i ] ){
Cuplaj[ i ] = 1;
if ( df ( i ) ){
rez++;
for ( int j=1; j<=n+m; ++j ){
Viz[ j ] = 0;
}
}
else{
Cuplaj[ i ] = 0;
}
}
}
printf("%d\n", rez );
for ( i=1; i<=n; ++i ){
if ( Other[ i ] ){
printf("%d %d\n", i, Other[ i ] - n );
}
}
return 0;
}