Guest User

Untitled

a guest
May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8.     int graph[51][51];
  9.  
  10.     int cases;
  11.     int parts;
  12.     int a,b;
  13.     int base;
  14.     int root;
  15.     int prec;
  16.     scanf("%d",&cases);
  17.  
  18.     int cnt=1;
  19.     int out_i;
  20.  
  21.     while(cases > 0)
  22.     {
  23.         int out[1010][2];
  24.         out_i=0;
  25.  
  26.         scanf("%d",&parts);
  27.         for(int i=0;i<51;i++)
  28.             for(int j=0;j<51;j++)
  29.                 graph[i][j] = 0;
  30.  
  31.         for(int k=0;k<parts;k++)
  32.         {
  33.  
  34.             scanf("%d %d",&a,&b);
  35.  
  36.             if(k==0)
  37.             {
  38.                 base=a;
  39.                 root=a;
  40.             }
  41.             graph[a][b]++;
  42.             graph[b][a]++;
  43.         }
  44.  
  45.         bool solvable = true;
  46.         int counter=0;
  47.  
  48.         for(int i=0;i<51;i++)
  49.         {
  50.             for(int j=0;j<51;j++)
  51.             {
  52.                 if(graph[i][j]!=0)
  53.                 {
  54.                     counter +=graph[i][j];
  55.                 }
  56.             }
  57.             if(counter % 2 != 0)
  58.                 solvable = false;
  59.         }
  60.  
  61.         /*
  62.         for(int i=0;i<51;i++)
  63.         {
  64.             for(int j=0;j<51;j++)
  65.             {
  66.                 if(graph[i][j]!=0)
  67.                     counter ++;
  68.             }
  69.             if(counter == 0)
  70.                 solvable = false;
  71.         }
  72.         */
  73.  
  74.         printf("Case #%d\n",cnt);
  75.         if(solvable)
  76.         {
  77.             for(int l=0;l<1001;l++)
  78.             {
  79.  
  80.                 for(int i=0;i<51;i++)
  81.                 {
  82.                     while(graph[base][base]>0)
  83.                     {
  84.                                 out[out_i][0]=base;
  85.                                 out[out_i][1]=base;
  86.                                 out_i ++;
  87.                                 //printf("%d %d\n",base, base);
  88.                                 graph[base][base] -=2;
  89.  
  90.                     }
  91.  
  92.                     if((graph[base][i])!=0)
  93.                     {
  94.  
  95.                             //printf("%d %d\n",base,i);
  96.                             out[out_i][0]=base;
  97.                             out[out_i][1]=i;
  98.                                 out_i ++;
  99.  
  100.                             graph[base][i]-=1;
  101.                             graph[i][base]-=1;
  102.                             //prec=base;
  103.                             base=i;
  104.  
  105.  
  106.                     }
  107.  
  108.                 }
  109.             }
  110.         }
  111.  
  112.         if(out_i  == parts)
  113.         {
  114.  
  115.             for(int l=0;l<out_i;l++)
  116.                 printf("%d %d\n",out[l][0],out[l][1]);
  117.  
  118.             if(cases >1)
  119.                 printf("\n");
  120.         }
  121.  
  122.         else
  123.         {   if(cases >1)
  124.                 printf("some beads may be lost\n\n");
  125.             else
  126.                 printf("some beads may be lost\n");
  127.         }
  128.         cases --;
  129.         cnt++;
  130.     }
  131.  
  132.  
  133.     return 0;
  134. }
Add Comment
Please, Sign In to add comment