Pagini recente » Cod sursa (job #1870373) | Cod sursa (job #1765023) | Cod sursa (job #1562805) | Cod sursa (job #1447165) | Cod sursa (job #573803)
Cod sursa(job #573803)
#include<fstream>
#include<vector>
#define InFile "cuplaj.in"
#define OutFile "cuplaj.out"
using namespace std;
ifstream f (InFile);
ofstream g (OutFile);
const int MaxN = 10001;
int L,R,E,unit[MaxN],l[MaxN],r[MaxN];
vector<int> G[MaxN];
int pairup(int nod)
{
if( unit[nod] )
return 0;
unit[nod] = 1;
vector<int>::iterator it;
for( it = G[nod].begin() ; it != G[nod].end() ; ++it )
if( !r[*it] )
{
l[nod] = *it;
r[*it] = nod;
return 1;
}
for( it = G[nod].begin() ; it != G[nod].end() ; ++it )
if( pairup(r[*it]) )
{
l[nod] = *it;
r[*it] = nod;
return 1;
}
return 0;
}
int main()
{
f >> L >> R >> E;
int i,x,y;
for( i = 0 ; i < E ; i++ )
{
f >> x >> y;
G[x].push_back(y);
}
int stare = 1;
while( stare )
{
stare = 0;
memset(unit,0,sizeof(unit));
for( i = 1 ; i <= L ; i++ )
if( !l[i] )
stare = stare | pairup(i);
}
int cuplaj= 0;
for( i = 1 ; i <= L ; i++ )
if( l[i] )
cuplaj++;
g << cuplaj << '\n';
for( i = 1 ; i <= L ; i++ )
if( l[i] )
g << i << ' ' << l[i] <<'\n';
return 0;
}