Pagini recente » Cod sursa (job #2686328) | Cod sursa (job #1632882) | Cod sursa (job #614368) | Cod sursa (job #2179857) | Cod sursa (job #2416407)
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#define N 10005
using namespace std;
int n,m,e,x,y;
vector <int> g[N];
vector <pair<int,int>> sol;
int st[N], dr[N];
bitset <N> viz;
bool cuplaj(int nod){
if(viz[nod])
return 0;
viz[nod] = 1;
for(int i : g[nod])
if(!dr[i]){
dr[i] = nod;
st[nod] = i;
return 1;
}
for(int i : g[nod])
if(cuplaj(dr[i])){
dr[i] = nod;
st[nod] = i;
return 1;
}
return 0;
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d%d%d", &n,&m,&e);
for(int i = 0; i < e; ++i){
scanf("%d%d", &x,&y);
g[x].push_back(y);
}
bool ok = 1;
while(ok){
ok = 0;
viz.reset();
for(int i = 1; i <= n; ++i)
if(!st[i])
ok |= cuplaj(i);
}
for(int i = 1; i <= n; ++i)
if(st[i])
sol.push_back({i,st[i]});
cout<<sol.size()<<"\n";
for(auto i : sol)
cout<<i.first<<" "<<i.second<<"\n";
return 0;
}