Cod sursa(job #856797)
#include <fstream>
#include <vector>
std::vector<std::vector<int> > G;
std::vector<int> match;
std::vector<int> vis;
bool DFS(int k) {
if(vis[k])
return false;
vis[k] = 1;
for(std::vector<int>::iterator v = G[k].begin(); v != G[k].end(); ++v) {
if(match[*v] == -1 || DFS(match[*v])) { //if DFS(match[*v]) returns true we're sure we've found a path
match[k] = *v; match[*v] = k;
return true;
}
}
return false;
}
int main() {
std::ifstream fin("cuplaj.in");
std::ofstream fout("cuplaj.out");
int L,R,m;
fin >> L >> R >> m;
G.resize(L + R,std::vector<int>());
match.resize(L + R, -1);
vis.resize(L + R,0);
while(m--) {
int x,y;
fin >> x >> y;
--x; --y;
G[x].push_back(y + L);
G[y + L].push_back(x);
}
int matches = 0;
for(bool more = true; more;) {
more = false;
for(int i=0;i<L;++i) {
if(match[i] == -1 && DFS(i)) {
matches ++;
more = true;
}
}
vis.assign(vis.size(),0);
}
fout << matches << '\n';
for(int i=0;i<L;++i)
if(match[i] != -1)
fout << i + 1 << ' ' << match[i] + 1 - L<< '\n';
return 0;
}