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

inputoutput
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>