Pagini recente » Cod sursa (job #2953128) | Cod sursa (job #1824919) | Cod sursa (job #2921770) | Cod sursa (job #1154689) | Cod sursa (job #1449652)
#include <cstdio>
#include <vector>
#define nmax 10005
#include <cstring>
using namespace std;
vector <int> graf[nmax];
int l[nmax],r[nmax];
bool u[nmax];
int n1,n2;
bool imperecheaza(int n){
if(u[n]==true)return false;
u[n]=true;
for(vector <int>::iterator it=graf[n].begin();it!=graf[n].end();++it)if(r[*it]==false){
l[n] = *it;
r[*it] = n;
return true;
}
for(vector <int>::iterator it=graf[n].begin();it!=graf[n].end();++it)if(imperecheaza(r[*it])==true){
l[n] = *it;
r[*it] = n;
return true;
}
return false;
}
void citeste(){
int m,x,y;
scanf("%d %d %d ",&n1,&n2,&m);
while(m--){
scanf("%d %d ",&x,&y);
graf[x].push_back(y);
}
}
void rezolva(){
bool c;
int i;
do{
c=false;
memset(u,false,sizeof(u));
for(i=1;i<=n1;i++)if(!l[i])c=imperecheaza(i);
}while(c);
int res=0;
for(i=1;i<=n1;i++)if(l[i])res++;
printf("%d\n",res);
for(i = 1;i<=n1;i++)if(l[i])printf("%d %d\n",i,l[i]);
}
int main(){
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
citeste();
rezolva();
return 0;
}