Pagini recente » Cod sursa (job #384636) | Cod sursa (job #2145740) | Cod sursa (job #1191156) | Cod sursa (job #1721882) | Cod sursa (job #1650442)
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#define nmax 10005
#define pb push_back
using namespace std;
int st[nmax],dr[nmax];
int n1,n2,m1,nrcomp=0;
vector<int> m[nmax];
bitset<nmax> u;
int comb(int nod)
{
if(u[nod]==1) return 0;
u[nod]=1;
for(vector<int>::iterator it=m[nod].begin();it!=m[nod].end();it++)
if(!dr[*it])
{
dr[*it]=nod;
st[nod]=*it;
nrcomp++;
return 1;
}
for(vector<int>::iterator it=m[nod].begin();it!=m[nod].end();it++)
if(comb( dr[*it] ))
{
dr[*it]=nod;
st[nod]=*it;
return 1;
}
return 0;
}
int main()
{
int v1,v2,i;
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d%d%d",&n1,&n2,&m1);
for(;m1;m1--)
{
scanf("%d%d",&v1,&v2);
m[v1].pb(v2);
}
int change=1;
while(change)
{
u.reset(); change=0;
for(i=1;i<=n1;i++) if(!st[i]) change|=comb(i);
}
printf("%d\n",nrcomp);
for(i=1;i<=n1;i++)
if(st[i]) printf("%d %d\n",i,st[i]);
fclose(stdin);
fclose(stdout);
return 0;
}