Pagini recente » Cod sursa (job #1052096) | Cod sursa (job #2232520) | Cod sursa (job #825450) | Cod sursa (job #2762180) | Cod sursa (job #2825015)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int const N = 1e4 + 3;
int n , m , e;
int a , b;
vector<int> v[N];
int l[N] , r[N] , viz[N];
int ans;
int cuplaj(int x){
if(viz[x])
return 0;
viz[x] = 1;
for(auto y : v[x]){
if(r[y])
continue;
l[x] = y;
r[y] = x;
++ ans;
return 1;
}
for(auto y : v[x]){
if(cuplaj(r[y])){
l[x] = y;
r[y] = x;
return 1;
}
}
return 0;
}
int main()
{
fin >> n >> m >> e;
for(int i = 1 ; i <= e ; ++ i){
fin >> a >> b;
v[a].push_back(b);
}
while(true){
fill(viz , viz + 1 + n , 0);
int r = 0;
for(int i = 1 ; i <= n ; ++ i)
if(!l[i])
r += cuplaj(i);
if(!r){
break;
}
}
fout << ans << '\n';
for(int i = 1 ; i <= m ; ++ i){
if(r[i]){
fout << r[i] << ' ' << i << '\n';
}
}
return 0;
}