Pagini recente » Cod sursa (job #1751787) | Cod sursa (job #723123) | Cod sursa (job #3181889) | Cod sursa (job #1601216) | Cod sursa (job #487713)
Cod sursa(job #487713)
#include <stdio.h>
#include <vector>
#define Nmax 255+2
#define Mmax 7000+2
#define pb push_back
using namespace std;
vector< int > v[Nmax];
int L[Nmax],R[Nmax],use[Nmax],beg[Nmax],LL[Nmax];
int N,M;
int cupleaza(int i){
vector< int >:: iterator it;
use[i]=1;
for(it=v[i].begin(); it!=v[i].end(); ++it)
if( !R[*it] || ( !use[R[*it]] && cupleaza(R[*it]) )){
L[i]=*it; R[*it]=i;
return 1;
}
return 0;
}
int main(){
int i,still,x,y,nr=0,wh;
freopen("strazi.in","r",stdin);
freopen("strazi.out","w",stdout);
scanf("%d%d",&N,&M);
for(i=1;i<=M;++i){
scanf("%d%d",&x,&y);
v[x].pb(y);
}
for( still=1; still; ){
still=0;
memset(use,0,sizeof(use));
for(i=1;i<=N;++i)
if( !L[i] && cupleaza(i) )
still=1;
}
for(i=1;i<=N;++i)
if( ! L[i] ) ++nr;
printf("%d\n",--nr);
for(i=1;i<=N;++i)
if( !L[i] ){
beg[i]=i;
while( R[beg[i]] ) beg[i]=R[beg[i]];
}
for(i=1;i<=N && nr;++i)
if( !L[i] ){
printf("%d ",i); wh=i;
for(++i; L[i]; ) ++i;
printf("%d\n",beg[i]);
LL[wh]=beg[i];
--i; --nr;
}
for(i=1;i<=N;++i)
if( L[i] ) LL[i]=L[i];
for(i=1; LL[i]; ){
printf("%d ",i);
i=LL[i];
}
printf("%d\n",i);
fclose(stdin); fclose(stdout);
return 0;
}