Pagini recente » Cod sursa (job #2569360) | Cod sursa (job #2911485) | Cod sursa (job #2394041) | Cod sursa (job #847152) | Cod sursa (job #1254358)
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
const int N=100000;
vector<int>g[N+1];
bool vis[N+1];
int father[N+1];
int l[N+1];
int res,n1,n2,m;
bool pairup(int dad){
if(vis[dad])
return 0;
vis[dad]=true;
for(int i=0;i<g[dad].size();i++){
int son=g[dad][i];
if(!father[son]){
l[dad]=son;
father[son]=dad;
return 1;
}
}
for(int i=0;i<g[dad].size();i++){
int son=g[dad][i];
if(pairup(son)){
l[dad]=son;
father[son]=dad;
return 1;
}
}
return 0;
}
int main(){
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d%d%d",&n1,&n2,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
bool f=true;
while(f){
f=false;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n1;i++)
if(!l[i])
if(pairup(i))
f=true;
}
for(int i=1;i<=n1;i++)
if(l[i]>0)
res++;
printf("%d\n",res);
for(int i=1;i<=n1;i++)
if(l[i]>0)
printf("%d %d\n",i,l[i]);
return 0;
}