Pagini recente » Cod sursa (job #447160) | Cod sursa (job #2773245) | Cod sursa (job #39704) | Cod sursa (job #1905505) | Cod sursa (job #2462830)
#include <bits/stdc++.h>
#define N 10005
using namespace std;
int n, m, e, l[N], r[N];
vector<int> G[N], pairs;
bool o[N];
int match(int s) {
if (o[s]) return 0;
o[s] = true;
for (int x : G[s])
if (!l[x]) {
r[s] = x;
l[x] = s;
return 1;
}
for (int x : G[s])
if (match(l[x])) {
r[s] = x;
l[x] = s;
return 1;
}
return 0;
}
int main() {
ifstream cin ("cuplaj.in");
ofstream cout ("cuplaj.out");
cin >> n >> m >> e;
int x, y;
for (int i = 0; i < e; i++) {
cin >> x >> y;
G[x].push_back(y);
}
int matchings;
do {
matchings = 0;
memset(o, 0, sizeof(o));
for (int i = 1; i <= n; i++)
if (!r[i])
matchings += match(i);
} while (matchings);
for (int i = 1; i <= n; i++)
if (r[i]) pairs.push_back(i);
cout << pairs.size();
for (int p : pairs)
cout << '\n' << p << " " << r[p];
return 0;
}