Pagini recente » Cod sursa (job #2086344) | Cod sursa (job #2309379) | Cod sursa (job #703015) | Cod sursa (job #1355782) | Cod sursa (job #1069811)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int nmax= 10000;
vector <int> v[2*nmax+1];
int p[2*nmax+1];
bool u[2*nmax+1];
int pair_up( int x ) {
if ( u[x]==1 ) {
return 0;
}
u[x]= 1;
for ( vector <int>::iterator it= v[x].begin(); it!=v[x].end(); ++it ) {
if ( p[*it]==0 || pair_up(p[*it])==1 ) {
p[x]= *it;
p[*it]= x;
return 1;
}
}
return 0;
}
int main( ) {
int n, m, e;
fin>>n>>m>>e;
for ( ; e>0; --e ) {
int x, y;
fin>>x>>y;
v[x].push_back(n+y);
v[n+y].push_back(x);
}
int f= 0, x= 1;
while ( x>0 ) {
x= 0;
for ( int i= 1; i<=n; ++i ) {
u[i]= 0;
}
for ( int i= 1; i<=n; ++i ) {
if ( p[i]==0 ) {
x+= pair_up(i);
}
}
f+= x;
}
fout<<f<<"\n";
for ( int i= 1; i<=n; ++i ) {
if ( p[i]>0 ) {
fout<<i<<" "<<p[i]-n<<"\n";
}
}
return 0;
}