home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume17 / contest-prog / part02 / prob16.c < prev    next >
C/C++ Source or Header  |  1989-02-06  |  1KB  |  72 lines

  1. main(){
  2.     int ii,jj,kk,a,b,M,N,i,j,k;
  3.     int abin,bbin,nbin[200],bin[200][30];
  4.  
  5.     scanf("%d%d",&M,&N);
  6.     for(i=1;i<=N;i++){
  7.         bin[i][1]=i;
  8.         nbin[i]=1;
  9.     }
  10.     for(kk=1;kk<=M;kk++){
  11.         if(scanf("%d%d",&a,&b)!=2)exit(1);
  12.         ii=jj=0;
  13.         for(i=1;i<=N;i++){
  14.             if(nbin[i]<1)continue;
  15.             for(j=1;j<=nbin[i];j++)
  16.                 if(bin[i][j]==a){
  17.                     ii=i;
  18.                     jj=j;
  19.                 }
  20.         }
  21.         if(ii==0||jj==0){
  22.             printf("oops a=%d b=%d ii=%d jj=%d i=%d j=%d nbin[ii]=%d\n"
  23.                 ,a,b,ii,jj,i,j,nbin[ii]);
  24.             exit(2);
  25.         }
  26.         /*bin[ii][jj]=0;*/
  27.         abin=ii;
  28.         ii=jj=0;
  29.         for(i=1;i<=N;i++){
  30.             if(nbin[i]<1)continue;
  31.             for(j=1;j<=nbin[i];j++)
  32.                 if(bin[i][j]==b){
  33.                     ii=i;
  34.                     jj=j;
  35.                     break;
  36.                 }
  37.         }
  38.         if(ii==0||jj==0){
  39.             printf("oops ii=%d jj=%d i=%d j=%d nbin[ii]=%d\n"
  40.             ,ii,jj,i,j,nbin[ii]);
  41.             exit(3);
  42.         }
  43.         bbin=ii;
  44.         for(j=1;j<=nbin[abin];j++)bin[bbin][++nbin[bbin]]=bin[abin][j];
  45.         nbin[abin]=0;
  46.     }
  47.     for(i=1;i<=N;i++)
  48.     {
  49.         k=nbin[i];
  50.         if(k<1)continue;
  51.         if(k>=2){
  52.             int l,m;
  53.             for(l=1;l<k;l++)for(m=l+1;m<=k;m++)if(bin[i][l]>bin[i][m]){
  54.                 int t; 
  55.                 t=bin[i][l]; 
  56.                 bin[i][l]=bin[i][m]; 
  57.                 bin[i][m]=t;
  58.             }
  59.         }
  60.     }
  61.     for(i=1;i<=N;i++)
  62.     for(kk=1;kk<=N;kk++)
  63.     if(nbin[kk]<1)continue; else{
  64.         if(bin[kk][1]==i){
  65.             for(j=1;j<=nbin[kk];j++) printf(" %d ",bin[kk][j]);
  66.             puts("");
  67.  
  68.         }
  69.         }
  70.  
  71. }
  72.