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