Pagini recente » Cod sursa (job #2176268) | Cod sursa (job #3186329) | Cod sursa (job #1821425) | Cod sursa (job #426680) | Cod sursa (job #1784002)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10000;
vector <int> g[MAXN + 1];
int conleft[MAXN + 1], conright[MAXN + 1], seen[MAXN + 1];
int match(int node) {
if (seen[node])
return 0;
seen[node] = 1;
for (auto it : g[node])
if (conright[it] == 0) {
conleft[node] = it;
conright[it] = node;
return 1;
}
for (auto it : g[node])
if (match(it)) {
conleft[node] = it;
conright[it] = node;
return 1;
}
return 0;
}
int main()
{
int n, m, e, i, x, y, augm;
ifstream fin("cuplaj.in");
fin >> n >> m >> e;
for (i = 0; i < e; ++i) {
fin >> x >> y;
g[x].push_back(y);
}
fin.close();
do {
augm = 0;
memset(seen, 0, sizeof(seen));
for (i = 1; i <= n; ++i)
if (conleft[i] == 0)
augm += match(i);
} while (augm);
for (i = 1, augm = 0; i <= n; ++i)
augm += (conleft[i] > 0);
ofstream fout("cuplaj.out");
fout << augm << '\n';
for (i = 1; i <= n; ++i)
if (conleft[i] > 0)
fout << i << " " << conleft[i] << '\n';
fout.close();
return 0;
}