Pagini recente » Monitorul de evaluare | Arhiva de probleme | Cod sursa (job #2470140) | Monitorul de evaluare | Cod sursa (job #2247679)
#include <bits/stdc++.h>
using namespace std;
const int kNmax = 1e4+4;
int l[kNmax], r[kNmax];
bool use[kNmax];
vector<int> g[kNmax];
bool pair_up(int which) {
if (use[which]) return false;
use[which] = true;
for (int y : g[which]) {
if (!r[y]) {
l[which] = y;
r[y] = which;
return true;
}
}
for (int y : g[which]) {
if (pair_up(r[y])) {
l[which] = y;
r[y] = which;
return true;
}
}
return false;
}
int main() {
#ifdef INFOARENA
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
#endif
int n, m, e;
cin >> n >> m >> e;
for (int u, v, i = 1; i <= e; i++) {
cin >> u >> v;
g[u].push_back(v);
}
for (bool ok = true; ok; ) {
ok = false;
memset(use, false, sizeof use);
for (int i = 1; i <= n; i++)
if (!l[i])
ok |= pair_up(i);
}
vector<pair<int, int> > matching;
for (int i = 1; i <= n; i++) {
if (!l[i]) continue;
matching.emplace_back(i, l[i]);
}
cout << matching.size() << '\n';
for (const auto& pii : matching) {
cout << pii.first << ' ' << pii.second << '\n';
}
return 0;
}