1128. Partition into Groups

1128. Partition into Groups

Time limit: 0.5 second Memory limit: 64 MB
There are   N  children in the kindergarten. Unfortunately, the children quarrel though not often. Each child has not more than three adversaries. Is it possible to partition the children into two groups (possibly not equal), so that each child would have not more than one adversary in his or her group?

Input

The first line contains an integer   N, 0 <   N  ≤ 7163. The next   N  lines contain lists of adversaries of each child. A line starts with the amount of the corresponding child's adversaries, then the numbers of the adversaries follow. The numbers in each line are separated with a space.

Output

The first line contains the number of children in the smaller group. The next line contains the list of children in the group. The numbers in the second line are separated with a space. If the groups are of the same size then you are to describe the group that contains the child number one. Note that the output may contain the only number 0. If there are several possible partitions it's sufficient to output an arbitrary one. If there's no possible partition you are to output the only string “NO SOLUTION”.

Sample

input output
8
3 2 3 7
3 1 3 7
3 1 2 7
1 6
0
2 4 8
3 1 2 3
1 6
4
1 2 5 6
Problem Author: Dmitry Filimonenkov Problem Source: VI Ural State University Collegiate Programming Contest (21.10.2001)
***************************************************************************************
用vector不好使,wa8,可能爆栈啦,不得已用链表,存储前向星,贪心&&dfs,由题意可证明无(NO  solution)的情况
***************************************************************************************
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<cstdio>
  7 #include<queue>
  8 #include<vector>
  9 #include<stack>
 10 using namespace std;
 11 struct node//用指针链表
 12 {
 13     int to;
 14     node *next;
 15 } *gr[600001],nodea[600001];
 16 int nump;
 17 int col[60005];
 18 int  n,i,j,k,num,m,h,y;
 19 void  add(int x,int rt)//存储
 20  {
 21      nodea[nump].to=rt;
 22      nodea[nump].next=gr[x];
 23      gr[x]=&nodea[nump];
 24      nump++;
 25  }
 26 void dfs(int s,int t)//dfs贪心
 27  {
 28      node *mp;
 29      for(mp=gr[s];mp!=NULL;mp=mp->next)
 30       {
 31           if(col[mp->to]==0)
 32            {
 33                col[mp->to]=t;
 34                dfs(mp->to,3-t);
 35            }
 36       }
 37  }
 38 int main()
 39 {
 40     while(scanf("%d",&n)!=EOF)
 41     {
 42       for(i=0;i<=n;i++)//初始化
 43        {
 44            col[i]=0;
 45            gr[i]=NULL;
 46        }
 47         nump=1;
 48      for(i=1;i<=n;i++)
 49      {
 50          cin>>m;
 51          for(j=1;j<=m;j++)
 52           {
 53               cin>>h;
 54               add(i,h);
 55           }
 56      }
 57      memset(col,0,sizeof(col));
 58      for(i=1;i<=n;i++)
 59       if(col[i]==0)
 60        {
 61           col[i]=2;
 62           dfs(i,1);
 63        }
 64      node *mp;
 65      for(i=1;i<=n;i++)
 66       {
 67           num=0;
 68           for(mp=gr[i];mp!=NULL;mp=mp->next)
 69             if(col[i]==col[mp->to])
 70                num++;
 71           if(num>=2)//重了变换
 72            col[i]=3-col[i];
 73       }
 74      num=0;
 75      for(i=1;i<=n;i++)
 76       if(col[i]==2)
 77        num++;
 78     if(num>n/2)//输出时处理
 79      {
 80          num=n-num;
 81          y=1;
 82      }
 83     else
 84        y=2;
 85     bool fs=true;
 86     cout<<num<<endl;
 87     for(i=1;i<=n;i++)
 88      if(col[i]==y)
 89      {
 90          if(fs==true)
 91          {
 92              cout<<i;
 93              fs=false;
 94          }
 95          else
 96            cout<<' '<<i;
 97      }
 98 
 99     cout<<endl;
100     }
101 
102     return 0;
103 }
View Code

转载于:https://www.cnblogs.com/sdau--codeants/p/3272538.html

猜你喜欢

转载自blog.csdn.net/weixin_34090643/article/details/93433173
今日推荐