Pagini recente » Cod sursa (job #184796) | Cod sursa (job #2906274) | Cod sursa (job #700877) | Cod sursa (job #2928904) | Cod sursa (job #2475822)
#include <fstream>
#include <vector>
#include <cstring>
#define N 10005
using namespace std;
int n, m, e, l[N], r[N];
bool o[N];
vector<int> G[N];
int match(int s) {
if (o[s]) return 0;
o[s] = true;
for (int x : G[s])
if (!r[x]) {
l[s] = x;
r[x] = s;
return 1;
}
for (int x : G[s])
if (match(r[x])) {
l[s] = x;
r[x] = s;
return 1;
}
return 0;
}
int main() {
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
cin >> n >> m >> e;
int x, y;
while (e--) {
cin >> x >> y;
G[x].push_back(y);
}
int matching;
do {
memset(o, 0, sizeof(o));
matching = 0;
for (int i = 1; i <= n; i++)
if (!l[i])
matching += match(i);
} while (matching);
matching = 0;
for (int i = 1; i <= n; i++)
if (l[i]) matching++;
cout << matching << '\n';
for (int i = 1; i <= n; i++)
if (l[i])
cout << i << " " << l[i] << '\n';
return 0;
}