Pagini recente » Cod sursa (job #978297) | Cod sursa (job #2323187) | Cod sursa (job #2111555) | Cod sursa (job #21265) | Cod sursa (job #3194201)
#include <bits/stdc++.h>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e;
vector <int> adj[20004];
int cost[2004][2004];
int father[20004];
bool findBfs() {
queue <int> q;
bitset <20004> reached;
q.push(0);
while (!q.empty()) {
int front = q.front();
q.pop();
reached[front] = 1;
if (front == n + m + 1) {
return true;
}
for (int next : adj[front]) {
if (front < next && cost[front][next] < 1 && !reached[next]) {
reached[next] = 1;
father[next] = front;
q.push(next);
} else if (front > next && cost[front][next] && !reached[next]) {
reached[next] = 1;
father[next] = front;
q.push(next);
}
}
}
return false;
}
void update(int currNode) {
if (currNode == 0) {
return;
}
update(father[currNode]);
cost[father[currNode]][currNode] += 1;
cost[currNode][father[currNode]] -= 1;
}
int main() {
cin >> n >> m >> e;
for (int i = 0; i < e; ++i) {
int l, r;
cin >> l >> r;
adj[l].push_back(n + r);
adj[n + r].push_back(l);
}
for (int i = 1; i <= n; ++i) {
adj[0].push_back(i);
}
for (int i = 1; i <= m; ++i) {
adj[n + i].push_back(n + m + 1);
}
while (findBfs()) {
update(n + m + 1);
}
vector <pair <int, int> > cuplaj;
for (int i = 1; i <= n; ++i) {
for (int next : adj[i]) {
if (cost[i][next] == 1) {
cuplaj.push_back({i, next});
}
}
}
cout << cuplaj.size() << '\n';
for (auto curr : cuplaj) {
cout << curr.first << ' ' << curr.second - n<< '\n';
}
return 0;
}