Pagini recente » Cod sursa (job #1682135) | Cod sursa (job #2535066)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 20505;
int N, M, E;
vector<int> G[NMAX];
int L[NMAX], R[NMAX];
void read() {
scanf("%d%d%d", &N, &M, &E);
int x, y;
for (int edgeIdx = 0; edgeIdx < E; edgeIdx++) {
scanf("%d%d", &x, &y);
G[x].push_back(y + N);
}
}
bool dfs(int node, vector<bool>& visited) {
if (visited[node]) {
return false;
}
visited[node] = true;
for (auto& nodeRight : G[node]) {
if (!R[nodeRight] || dfs(R[nodeRight], visited)) {
L[node] = nodeRight;
R[nodeRight] = node;
return true;
}
}
return false;
}
void solve() {
bool foundImprovement = true;
vector<bool> visited;
int matchSize = 0;
while (foundImprovement) {
foundImprovement = false;
visited.assign(N + 1, false);
for (int node = 1; node <= N; node++) {
if (!L[node] && dfs(node, visited)) {
foundImprovement = true;
matchSize++;
}
}
}
printf("%d\n", matchSize);
for (int leftNode = 1; leftNode <= N; leftNode++) {
if (L[leftNode]) {
printf("%d %d\n", leftNode, L[leftNode] - N);
}
}
}
int main() {
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
read();
solve();
return 0;
}