Pagini recente » Cod sursa (job #638067) | Cod sursa (job #1410996) | Cod sursa (job #1772763) | Cod sursa (job #2178526) | Cod sursa (job #3270538)
#include <string.h>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
vector <int> G[MAXN];
int l[MAXN], r[MAXN], viz[MAXN];
/// Returns true if found a way to pair it up
bool pairup(const int n) {
if (viz[n])
return 0;
viz[n] = 1;
/// Try to pair with neighbour which doesn't already have pair
for(const int i : G[n]) {
if (r[i] == 0) {
l[n] = i;
r[i] = n;
return 1;
}
}
/// Try to free up neighbour and then make pair
for(const int i : G[n]) {
if (pairup(r[i])) {
l[n] = i;
r[i] = n;
return 1;
}
}
return 0;
}
int main(void) {
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int n, m, cnt_edges;
in >> n >> m >> cnt_edges;
while (cnt_edges --) {
int x, y;
in >> x >> y;
G[x].push_back(y);
}
bool change = true;
while (change) {
change = false;
memset(viz, 0, sizeof(viz)); // RESET VIZ
for(int i = 1; i <= n; i++) {
if (l[i] == 0) /// If I has no pair
change |= pairup(i);
}
}
int cuplaj = 0;
for(int i = 1; i <= n; i++) {
cuplaj += (l[i] > 0);
}
out << cuplaj << '\n';
for(int i = 1; i <= n; i++)
if (l[i] > 0)
out << i << ' ' << l[i] << '\n';
return 0;
}