Pagini recente » Cod sursa (job #559794) | Cod sursa (job #546159) | Istoria paginii runda/oni_10_3 | Cod sursa (job #425658) | Cod sursa (job #1339541)
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>
#define DIM 10005
#define vint vector<int>::iterator
#define infile "cuplaj.in"
#define outfile "cuplaj.out"
using namespace std;
ifstream f(infile);
ofstream g(outfile);
int nrNodesLeft, nrNodesRight, nrEdges;
bool viz[DIM];
int matchLeft[DIM], matchRight[DIM];
vector<int> Graph[DIM];
bool graphMatch(int node) {
if (viz[node])
return 0;
viz[node] = true;
for (vint it = Graph[node].begin(); it != Graph[node].end(); ++it) {
if (!matchRight[*it]) {
matchRight[*it] = node;
matchLeft[node] = *it;
return 1;
}
}
for (vint it = Graph[node].begin(); it != Graph[node].end(); ++it) {
if (graphMatch(matchRight[*it])) {
matchRight[*it] = node;
matchLeft[node] = *it;
return 1;
}
}
return 0;
}
int main() {
f >> nrNodesLeft >> nrNodesRight >> nrEdges;
for (int i = 1; i <= nrEdges; ++i) {
int source, destination;
f >> source >> destination;
Graph[source].push_back(destination);
}
bool ok;
int nrMatches = 0;
do {
ok = false;
memset(viz, false, sizeof(viz));
for (int i = 1; i <= nrNodesLeft; ++i) {
if (!matchLeft[i] && graphMatch(i)) {
++nrMatches;
ok = true;
}
}
} while (ok);
g << nrMatches << '\n';
for (int i = 1; i <= nrNodesLeft; ++i) {
if (!matchLeft[i])
continue;
g << i << ' ' << matchLeft[i] << '\n';
}
return 0;
}
//Trust me, I'm the Doctor!