Pagini recente » Cod sursa (job #1247962) | Cod sursa (job #2369254) | Cod sursa (job #1923315) | Cod sursa (job #2031425) | Cod sursa (job #2101899)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 10;
vector < int > gr[MAXN];
int boss_left[MAXN];
int boss_right[MAXN];
bool viz[MAXN];
bool cuplaj(int node){
if(viz[node]) return false;
viz[node] = true;
for(auto x : gr[node]){
if(boss_left[x] == 0){
boss_left[x] = node;
boss_right[node] = x;
return true;
}
}
for(auto x : gr[node]){
if(cuplaj(boss_left[x])){
boss_left[x] = node;
boss_right[node] = x;
return true;
}
}
return false;
}
int main(){
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, m, e;
f >> n >> m >> e;
for(int i = 1; i <= e; ++i){
int a, b;
f >> a >> b;
gr[a].push_back(b);
}
bool ok = true;
while(ok){
ok = false;
memset(viz, 0, sizeof viz);
for(int i = 1; i <= n; ++i){
if(boss_right[i] == 0)
ok |= cuplaj(i);
}
}
int cnt = 0;
for(int i = 1; i <= n; ++i){
if(boss_right[i]) ++ cnt;
}
g << cnt << '\n';
for(int i = 1; i <= n; ++i){
if(boss_right[i]) g << i << ' ' << boss_right[i] << '\n';
}
return 0;
}