Cod sursa(job #553826)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const char iname[] = "cuplaj.in";
const char oname[] = "cuplaj.out";
const int nmax = 10905;
ifstream fin(iname);
ofstream fout(oname);
int n, m, e, i, x, y, ct, am_cuplat;
vector<int> gr[nmax];
int viz[nmax], L[nmax], R[nmax];
int pair_up(int nod)
{
if(viz[nod])
return 0;
viz[nod] = 1;
for(vector<int>::iterator it = gr[nod].begin(); it != gr[nod].end(); ++ it)
if(pair_up(R[*it]) || R[*it] == 0)
{
L[i] = *it;
R[*it] = nod;
return 1;
}
return 0;
}
int main()
{
fin >> n >> m >> e;
for(i = 1; i <= e; i ++)
{
fin >> x >> y;
gr[x].push_back(y);
}
am_cuplat = 1;
while(am_cuplat)
{
for(i = 1; i <= n; i ++)
viz[i] = 0;
am_cuplat = 0;
for(i = 1; i <= n; i ++)
if(!L[i])
if(pair_up(i))
{
am_cuplat = 1;
++ct;
}
}
fout << ct << "\n";
for(i = 1; i <= n; i ++)
if(L[i])
fout << i << " " << L[i] << "\n";
return 0;
}