Pagini recente » Cod sursa (job #1566072) | Cod sursa (job #2228150) | Cod sursa (job #370837) | Cod sursa (job #1784352) | Cod sursa (job #1395748)
#include <fstream>
#include <vector>
#include <queue>
#define MAXV 10001
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int L, R, E;
vector <int> G[2 * MAXV];
int match[2 * MAXV];
int father[2 * MAXV];
bool isMarked[2 * MAXV];
int root[2 * MAXV];
int path;
int maxMatch = 0;
inline void readAndBuild() {
in >> L >> R >> E;
int x, y;
for (int e = 0; e < E; ++ e) {
in >> x >> y;
G[x].push_back(y + L);
G[y + L].push_back(x);
}
}
inline bool findAugmentingPath() {
queue <int> qF;
for (int node = L + R; node > 0; -- node) {
if (!match[node]) {
qF.push(node);
father[node] = node;
root[node] = node;
} else {
father[node] = -1;
root[node] = -1;
}
isMarked[node] = false;
}
for (int node; !qF.empty(); qF.pop()) {
node = qF.front();
for (vector <int>::iterator it = G[node].begin(); it != G[node].end(); ++ it) {
if (match[*it] != node) {
if (match[*it] != 0) {
father[*it] = node;
father[match[*it]] = *it;
root[*it] = root[node];
root[match[*it]] = root[node];
if (!isMarked[match[*it]]) {
qF.push(match[*it]);
isMarked[match[*it]] = true;
}
} else if (root[node] != root[*it]) {
father[root[*it]] = node;
path = *it;
return true;
}
}
}
}
return false;
}
inline void bipartiteMatchingWithEdmondsBlossom() {
for (int isPath = findAugmentingPath(); isPath; isPath = findAugmentingPath()) {
printf("%d ", path);
for (int node = path; father[node] != node; node = father[father[node]]) {
match[node] = father[node];
match[father[node]] = node;
printf("%d %d ", father[node], father[father[node]]);
}
printf("\n");
maxMatch ++;
}
}
inline void printResult() {
out << maxMatch << '\n';
for (int i = 1; i <= L; ++ i)
if (match[i]) {
out << i << ' ' << match[i] - L << '\n';
}
}
int main() {
readAndBuild();
bipartiteMatchingWithEdmondsBlossom();
printResult();
return 0;
}