Pagini recente » Cod sursa (job #1024937) | Cod sursa (job #2287684) | Cod sursa (job #2152371) | Cod sursa (job #361390) | Cod sursa (job #862225)
Cod sursa(job #862225)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
using std::vector;
short inc[20005];
int nc=0;
bool seen[20005];
vector<short> v[20005];
bool dfs (short i)
{
if(seen[i])
return 0;
seen[i]=1;
for(vector<short>::iterator it=v[i].begin();it!=v[i].end();it++)
if(!inc[*it]){
nc++;
inc[*it]=i;
inc[i]=*it;
return 1;
}
for(vector<short>::iterator it=v[i].begin();it!=v[i].end();it++)
if(dfs (inc[*it])){
inc[*it]=i;
inc[i]=*it;
return 1;
}
return 0;
}
int main()
{
freopen ("cuplaj.in","r",stdin);
#ifdef INFOARENA
freopen ("cuplaj.out","w",stdout);
#endif
int n,m,e;
scanf ("%d%d%d",&n,&m,&e);
for(int i=0;i<e;i++){
short x,y;
scanf ("%hd%hd",&x,&y);
y+=n;
v[x].push_back (y);
}
bool ok=1;
while(ok){
ok=0;
memset (seen, 0, sizeof seen);
for(int i=1;i<=n;i++)
if(!inc[i]&&dfs (i))
ok=1;
}
printf ("%d\n",nc);
for(int i=1;i<=n;i++)
if(inc[i])
printf ("%d %d\n",i,inc[i]-n);
return 0;
}