Pagini recente » Cod sursa (job #3274548) | Cod sursa (job #2562266) | Cod sursa (job #367184) | Cod sursa (job #529739) | Cod sursa (job #1650619)
#include <fstream>
#include <vector>
using namespace std;
vector <int> G[10010];
int a,b,c,A[10010],B[10010],i,j,n,m,x,y,ans;
char f[3000];
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
bool cuplaj( int nod )
{
if( f[ ( nod - 1 ) / 8 ] & ( 1<<( (nod - 1) % 8 ) ) )
return false;
f[ ( nod - 1 ) / 8 ] += ( 1<<( ( nod - 1 ) % 8 ) );
for( vector<int>::iterator it = G[ nod ].begin() ; it != G[ nod ].end() ; it++ )
{
if( B[ *it ] == 0 )
{
A[ nod ] = *it;
B[ *it ] = nod;
return true;
}
}
for( vector<int>::iterator it = G[ nod ].begin() ; it != G[ nod ].end() ; it++ )
{
if( cuplaj( B[ *it ] ) )
{
A[ nod ] = *it;
B[ *it ] = nod;
return true;
}
}
return false;
}
int main()
{
fin>>a>>b>>m;
for( i = 1 ; i <= m ; i++ )
{
fin>>x>>y;
G[ x ].push_back( y );
}
for( i = 1 ; i <= a ; i++ )
if( A[ i ] == 0 && cuplaj( i ) )
{
++ans;
for( j = 0 ; j <= a / 8 + 1 ; j++ )
f[ j ] = 0;
}
fout<<ans<<'\n';
for( i = 1 ; i <= a ; i++ )
if( A[ i ] )
fout<<i<<' '<<A[ i ]<<'\n';
return 0;
}