Pagini recente » Cod sursa (job #3003667) | Cod sursa (job #3003613) | Cod sursa (job #3231147) | Cod sursa (job #1402923) | Cod sursa (job #3234011)
#include <bits/stdc++.h>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
const int lim = 1e4 + 5;
vector<int> righty[lim];
vector<int> lefty[lim];
int taken[lim];
int last_run[lim];
int wifey[lim];
int hubby[lim];
int n, m, e;
int x, y;
bool df(int nod, int id) {
if (last_run[nod] == id) {
return false;
}
last_run[nod] = id;
for (int x : righty[nod]) {
if (hubby[x] == 0) {
hubby[x] = nod;
wifey[nod] = x;
return true;
}
}
for (int x : righty[nod]) {
if (df(hubby[x], id)) {
hubby[x] = nod;
wifey[nod] = x;
return true;
}
}
return false;
}
int main() {
in >> n >> m >> e;
while (e--) {
in >> x >> y;
righty[x].push_back(y);
lefty[y].push_back(x);
}
int id;
bool modif = true;
for (id = 1; modif; id++) {
modif = false;
for (int i = 1; i <= n; ++i) {
if (last_run[i] != id and wifey[i] == 0) {
modif |= df(i, id);
}
}
}
int count = 0;
for (int i = 1; i <= n; ++i) {
count += (wifey[i] != 0);
}
out << count << '\n';
for (int i = 1; i <= n; ++i) {
if (wifey[i]) {
out << i << ' ' << wifey[i] << '\n';
}
}
return 0;
}