Pagini recente » Cod sursa (job #2623660) | Cod sursa (job #1442595) | Cod sursa (job #2327778) | Cod sursa (job #2321958) | Cod sursa (job #2651548)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
const int maxn = 10005;
vector <int> nod[maxn];
int a, b, m, x, y, ans;
int l[maxn], r[maxn];
bool viz[maxn];
bool check(int x) {
if(viz[x] == true) {
return false;
}
viz[x] = true;
for(auto u : nod[x]) {
if(l[u] == false) {
l[u] = x;
r[x] = u;
return true;
}
}
for(auto u : nod[a]) {
if(check(l[u]) == true) {
r[x] = u;
l[u] = x;
return true;
}
}
return false;
}
int main()
{
int i;
f >> a >> b >> m;
for(i = 1; i <= m; i ++) {
f >> x >> y;
nod[x].push_back(y);
}
bool ok;
do {
ok = false;
memset(viz, false, sizeof(viz));
for(i = 1; i <= a; i ++) {
if(r[i] == false && check(i) == true) {
ans ++;
ok = true;
}
}
} while(ok == true);
g << ans << '\n';
for(i = 1; i <= a; i ++) {
if(r[i] != 0) {
g << i << ' ' << r[i] << '\n';
}
}
f.close();
g.close();
return 0;
}