Pagini recente » Rating Rotaru Alexandru (Rotaru_Alexandru_Andrei_325CC) | Cod sursa (job #147342) | Cod sursa (job #1239373) | Cod sursa (job #847698) | Cod sursa (job #796729)
Cod sursa(job #796729)
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
int n,m,e;
vector< int > G[10005];
int st[10005] ,dr[10005];
bool u[10005];
bool cuplaj(int nod)
{ if(u[nod]) return false;
u[nod]=true;
for(int i=0;i<G[nod].size();i++){
if(dr[G[nod][i]]==0)
{ st[nod]=G[nod][i];dr[G[nod][i]]=nod;return true;
}
}
for(int i=0;i<G[nod].size();i++){
if(cuplaj(dr[G[nod][i]])){
st[nod]=G[nod][i];dr[G[nod][i]]=nod;return true;
}
}
return false;
}
int main(){
int x,y;
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d %d %d",&n,&m,&e);
for(int i=0;i<e;i++){
scanf("%d %d",&x,&y);
G[x].push_back(y);
}
bool change=true;
while(change){
change=false;
memset(u,0,sizeof(u));
for(int i=1;i<=n;i++){
if(!st[i])
change|=cuplaj(i);
}
}
int c=0;
for(int i=1;i<=n;i++){
if(st[i]!=0) c++;
}
printf("%d\n",c);
for(int i=1;i<=n;i++){
if(st[i]!=0)
printf("%d %d\n",i,st[i]);
}
return 0;
}