Pagini recente » Cod sursa (job #1166089) | Cod sursa (job #1149861) | Cod sursa (job #1488250) | Cod sursa (job #2480431) | Cod sursa (job #3184018)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
vector <int> G[10005];
bitset <10005> uz;
int st[10005];
int dr[10005];
int cuplaj(int nod){
int i,t;
uz[nod] = 1;
for(auto x : G[nod]){
if(!dr[x]){
st[nod] = x;
dr[x] = nod;
return 1;
}
}
for(auto x : G[nod]){
if(!uz[dr[x]] && cuplaj(dr[x])){
st[nod] = x;
dr[x] = nod;
return 1;
}
}
return 0;
}
int main()
{
int n,i,j,m,e,u,v,ok,M = 0;
fin >> n >> m >> e;
for(i = 1; i <= e; i++){
fin >> u >> v;
G[u].push_back(v);
}
do{
uz.reset();
ok = 0;
for(i = 1; i <= n; i++){
if(!st[i] && cuplaj(i)) ok = 1;
}
}while(ok);
for(i = 1; i <= n; i++){
if(st[i]) M++;
}
fout << M << "\n";
for(i = 1; i <= n; i++){
if(st[i]) fout << i << " " << st[i] << "\n";
}
return 0;
}