Pagini recente » Cod sursa (job #1350563) | Cod sursa (job #939729) | Cod sursa (job #2548643) | Cod sursa (job #2573987) | Cod sursa (job #2155004)
#include <bits/stdc++.h>
#define N 10002
using namespace std;
bool u[N];
int r[N], l[N];
vector<int> G[N];
bool cuple(int node){
if(u[node]) return 0;
u[node] = 1;
for(auto next : G[node])
if(!r[next]) {r[next] = node; l[node] = next; return 1;}
for(auto next : G[node])
if(cuple(r[next])) {r[next] = node; l[node] = next; return 1;}
return 0;
}
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, x, y; i <= e; i ++){
scanf("%d%d", &x, &y);
G[x].push_back(y);
}
bool ok = 1;
while(ok){
ok = 0;
memset(u, 0, sizeof(u));
for(int i = 1; i <= n; i ++)
if(!u[i] && !l[i]) ok |= cuple(i);
}
int nr = 0;
for(int i = 1; i <= n; i ++)
if(l[i]) nr ++;
printf("%d\n", nr);
for(int i = 1; i <= n; i ++)
if(l[i]) printf("%d %d\n", i, l[i]);
}