Pagini recente » Cod sursa (job #711709) | Cod sursa (job #1136716) | Cod sursa (job #2261681) | Cod sursa (job #381250) | Cod sursa (job #1166471)
#include<iostream>
#include<fstream>
#include<cstring>
#include<vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int MAXN = 10005;
int n, m, k, i, x, y, st[MAXN], dr[MAXN], cuplaj;
bool viz[MAXN], ok;
vector<int> g[MAXN];
bool cuplat(int x) {
vector<int>::iterator it;
if(!viz[x]) {
viz[x] = 1;
for(it = g[x].begin(); it != g[x].end(); it++) {
int y = *it;
if(!st[y] || cuplat(st[y])) {
dr[x] = y;
st[y] = x;
return 1;
}
}
}
return 0;
}
int main() {
fin >> n >> m >> k;
for(i = 0; i < k; i++) {
fin >> x >> y;
g[x].push_back(y);
}
ok = 1;
while(ok) {
ok = 0;
memset(viz, 0, sizeof(viz));
viz[0] = 1;
for(i = 1; i <= n; i++) {
if(!dr[i] && cuplat(i)) {
cuplaj++;
ok = 1;
}
}
}
fout << cuplaj << '\n';
for(i = 1; i <= n; i++) {
if(dr[i] != 0) {
fout << i << ' ' << dr[i] << '\n';
}
}
fin.close();
fout.close();
return 0;
}