Pagini recente » Cod sursa (job #851682) | Cod sursa (job #1425807) | Cod sursa (job #2405983) | Cod sursa (job #917371) | Cod sursa (job #1647603)
#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]) return 0;
u[nod]=1;
vector<int>::iterator it;
for(it=m[nod].begin();it!=m[nod].end();it++)
if(!dr[*it])
{
if(!st[nod]) nrcomp++;
st[nod]=*it;
dr[*it]=nod;
return 1;
}
for(it=m[nod].begin();it!=m[nod].end();it++)
if( comb(dr[*it]))
{
if(!st[nod]) nrcomp++;
st[nod]=*it;
dr[*it]=nod;
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)
{
change=0;
u.reset();
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;
}