Pagini recente » Cod sursa (job #9293) | Cod sursa (job #1468377) | Cod sursa (job #1397458) | Cod sursa (job #1469486) | Cod sursa (job #2744319)
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int NMAX = 10000;
int n, m, e;
vector<int> vst[NMAX + 5];
int st[NMAX + 5];
int dr[NMAX + 5];
bool viz[NMAX + 5];
bool alternating_path(int nod_st) {
if(viz[nod_st])
return false;
viz[nod_st] = true;
for(int nod_dr : vst[nod_st])
if(st[nod_dr] == 0) {
st[nod_dr] = nod_st;
dr[nod_st] = nod_dr;
return true;
}
for(int nod_dr : vst[nod_st])
if(alternating_path(st[nod_dr])) {
st[nod_dr] = nod_st;
dr[nod_st] = nod_dr;
return true;
}
return false;
}
int main() {
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
scanf("%d %d %d", &n, &m, &e);
for(int i = 1; i <= e; i++) {
int x, y;
scanf("%d %d", &x, &y);
vst[x].push_back(y);
}
int cuplaj_max = 0;
int old_cuplaj;
do {
old_cuplaj = cuplaj_max;
for(int i = 1; i <= n; i++)
viz[i] = false;
for(int i = 1; i <= n; i++)
if(dr[i] == 0)
cuplaj_max += alternating_path(i);
} while(cuplaj_max != old_cuplaj);
printf("%d\n", cuplaj_max);
for(int i = 1; i <= n; i++)
if(dr[i] != 0)
printf("%d %d\n", i, dr[i]);
return 0;
}