1156. Two Rounds

1156. Two Rounds

Time limit: 2.0 second Memory limit: 64 MB
There are two rounds in the Urals Championship. The competitors have to solve   N  problems on each round. The jury had been working hard and finally managed to prepare 2 Nproblems for the championship. But it appeared that among those problems there were some, which have the analogous solutions. One shouldn’t assign such a problems for the same round. Please, help the jury form sets of tasks for each of the rounds.

Input

First line contains two numbers:   N, the number of tasks for a round, and   M, the number of pairs of tasks which should not be assigned for one round (1 ≤   N  ≤ 50; 0 ≤ M  ≤ 100). Then   M  lines follow, each of them contains two numbers of analogous tasks.

Output

Output two lines, containing numbers of tasks assigned for each round. If there is no solution, output the only word “IMPOSSIBLE”. If there are more than one solution you may assume anyone of them.

Sample

input output
2 3
1 3
2 1
4 3
1 4
2 3
Problem Author: Eugene Bryzgalov Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round  
***************************************************************************************
At first thought of greed, and that a few days ago to do a similar problem, but wa many times, feel no vulnerability that could be thought not comprehensive (have to think about) ............ Ahhh, finally saw solution to a problem, say this problem is a two-dimensional deformation 01 backpack, but be filled with non-exclusive collections, looking for the key to the set (deep search) backpack full of just the presence of the solution, the overall structure of the solution is difficult to think of! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
***************************************************************************************
  . 1 #include <the iostream>
   2 #include < String >
   . 3 #include <algorithm>
   . 4 #include <CString>
   . 5 #include <Vector>
   . 6 #include <Queue>
   . 7 #include <cstdio>
   . 8  the using  namespace STD;
   . 9  int F [ 1001 ]; // save the array element belongs to which group (tag array) 
10 Vector < int > hate [ 1001 ]; // mutually exclusive array 
. 11  int the Choose [ 1001 ] [ 1001 ]; // store group data i and two sets of markers
12 is  int SEL [ 1001 ]; // chain to find a node belongs to which set 
13 is  int num1 [ 1001 ], num2 [ 1001 ]; // each set of data, the number of elements in the two sets; 
14  int ST, EN;
 15  int n-, m, I, J;
 16  BOOL DFS ( int X, int K) // deep search have several sets of data 
. 17  {
 18 is       IF (! F [X] = 0 )
 . 19       {
 20 is           IF (F [X] == K)
 21 is            return  to true ;
 22 is          return false;
 23      }
 24      f[x]=k;
 25      if(k>0)
 26        ++num1[k];
 27      else
 28        ++num2[-k];
 29      for(int is=0;is<hate[x].size();is++)
 30       {
 31           if(!dfs(hate[x][is],-k))
 32            return false;
 33       }
 34     return  true;
 35 
 36  }
 37  int main()
 38  {
 39      cin>>n>>m;
 40      for(i=1;i<=m;i++)
 41       {
 42           cin>>st>>en;
 43           hate[st].push_back(en);//存排斥数组
 44           hate[en].push_back(st);
 45       }
 46      memset(f,0,sizeof(f));
 47      memset(num1,0,sizeof(num1));
 48      memset(num2,0,sizeof(num2));
 49      memset(choose,0,sizeof(choose));
 50      int ry=1;
 51      int gs=1;
 52      for(i=1;i<=2*n;i++)
 53       if(f[i]==0)
 54        {
 55            if(!dfs(i,ry))
 56            {
 57                gs=0;
 58                break;
 59            }
 60 
 61            ry++;
 62        }
 63       if(gs==0)
 64        {
 65            cout<<"IMPOSSIBLE"<<endl;
 66            return 0;
 67        }
 68 
 69      m=ry-1;
 70      choose[0][0]=1;
 71      for(i=0;i<m;i++)
 72        {
 73 is            for (J = 0 ; J <= n-; J ++ )
 74             {
 75                 IF (the Choose [I] [J] == 0 )
 76                  Continue ;
 77                  the Choose [I + . 1 ] [J + num1 [I + . 1 ] ] = 1 ; // tag when the number j and j + num1 [i + 1] when the up and set the set of data indicating which set belongs. 
78                  the Choose [I + . 1 ] [J + num2 [I + . 1 ]] = - . 1 ; // ibid 
79              }
 80  
81        }
 82        IF(the Choose [m] [n-] == 0 )
 83         {
 84             COUT << " IMPOSSIBLE " << endl;
 85             return  0 ;
 86         }
 87         int K = n-;
 88        for (m = I; I> = . 1 ; I - )
 89         {
 90             SEL [I] = the Choose [I] [K]; // chain find and mark (so called I think more images) 
91 is             IF (the Choose [I] [K]> 0 )
 92              - K- = num1 [I];
 93             the else 
94             k-=num2[i];
 95        }
 96        for(i=1;i<=2*n;i++)//输出
 97         if((f[i]>0&&sel[f[i]]>0)||(f[i]<0&&sel[-f[i]]<0))
 98         cout<<i<<' ';
 99        cout<<endl;
100        for(i=1;i<=2*n;i++)
101         if(!(f[i]>0&&sel[f[i]]>0)&&!(f[i]<0&&sel[-f[i]]<0))
102         cout<<i<<' ';
103         cout<<endl;
104         return 0;
105  }
View Code

Perseverance, in order to win

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3257591.html

Guess you like

Origin blog.csdn.net/weixin_34301132/article/details/93432967