Pagini recente » Cod sursa (job #1866780) | Cod sursa (job #2224156) | Cod sursa (job #1407966) | Cod sursa (job #1558788) | Cod sursa (job #1802024)
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, m, l, x, y, i;
int incolo[10005], incoace[10005];
vector <int> ls[10005];
bool ok, fol[10005];
int pairup(int n) {
if (fol[n])
return 0;
fol[n] = 1;
int l = ls[n].size();
int i, y;
for (i = 0; i < l; i++) {
y = ls[n][i];
if (incoace[y] == 0) {
incolo[n] = y;
incoace[y] = n;
return 1;
}
}
for (i = 0; i < l; i++) {
y = ls[n][i];
if (pairup(incoace[y])) {
incolo[n] = y;
incoace[y] = n;
return 1;
}
}
return 0;
}
int main() {
f >> n >> m >> l;
while (l--) {
f >> x >> y;
ls[x].push_back(y);
}
for (ok = 1; ok;) {
ok = 0;
for (i = 1; i <= n; i++)
fol[i] = 0;
for (i = 1; i <= n; i++)
if (incolo[i] == 0 && pairup(i))
ok = 1;
}
for (i = 1, l = 0; i <= n; i++)
l += (incolo[i] > 0);
g << l << '\n';
for (i = 1; i <= n; i++)
if (incolo[i] > 0)
g << i << ' ' << incolo[i]<< "\n";
return 0;
}