Pagini recente » Cod sursa (job #2318828) | Cod sursa (job #896082) | Cod sursa (job #2888833) | Cod sursa (job #1993716) | Cod sursa (job #2920071)
#include <bits/stdc++.h>//Hopcroft Karp Algorithm
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int dim=1e4+9;
vector<int>v[dim];
int n,m,e;
bool used[dim];
int current_matching_right[dim];
int current_matching_left[dim];
bool try_khun(int x){
if(used[x]){
return false;
}
used[x]=true;
for(int y:v[x]){
if(!current_matching_right[y]||try_khun(current_matching_right[y])){
current_matching_right[y]=x;
current_matching_left[x]=y;
return true;
}
}
return false;
}
signed main(){
fin>>n>>m>>e;
for(int i=1;i<=e;i++){
int x,y;
fin>>x>>y;
v[x].push_back(y);
}
bool ok=true;
while(ok){
for(int i=1;i<=n;i++){
used[i]=false;
}
ok=false;
for(int i=1;i<=n;i++){
if(!current_matching_left[i]&&try_khun(i)){
ok=true;
}
}
}
int nr=0;
for(int i=1;i<=m;i++){
if(current_matching_right[i]){
nr++;
}
}
fout<<nr<<'\n';
for(int i=1;i<=m;i++){
if(current_matching_right[i]){
fout<<current_matching_right[i]<<' '<<i<<'\n';
}
}
}