Pagini recente » Cod sursa (job #906634) | Istoria paginii utilizator/grape27 | laborator_7_sd_313cab | Cod sursa (job #1417884) | Cod sursa (job #2142110)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int L[N], R[N];
bool used[N];
vector <int> edge[N];
bool match(int x){
if(used[x] == true){
return false;
}
used[x] = true;
for(auto &it : edge[x]){
if(R[it] == 0){
L[x] = it;
R[it] = x;
return true;
}
}
for(auto &it : edge[x]){
if(match(R[it])){
L[x] = it;
R[it] = x;
return true;
}
}
return false;
}
int main(){
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
int n, m, e;
scanf("%d %d %d", &n, &m, &e);
for(int i = 1;i <= e;i++){
int x, y;
scanf("%d %d", &x, &y);
edge[x].push_back(y);
}
bool found = true;
int ans = 0;
while(found){
found = false;
for(int i = 1;i <= n;i++){
if(L[i] == 0){
if(match(i)){
found = true;
ans++;
}
}
}
for(int i = 1;i <= n;i++){
used[i] = 0;
}
}
printf("%d\n", ans);
for(int i = 1;i <= n;i++){
if(L[i]){
printf("%d %d\n", i, L[i]);
}
}
return 0;
}