zoukankan      html  css  js  c++  java
  • 1106. Two Teams

    1106. Two Teams

    Time limit: 1.0 second Memory limit: 64 MB
    The group of people consists of N members. Every member has one or more friends in the group. You are to write program that divides this group into two teams. Every member of each team must have friends in another team.

    Input

    The first line of input contains the only number N (N ≤ 100). Members are numbered from 1 to N. The second, the third,…and the (N+1)th line contain list of friends of the first, the second, …and the Nth member respectively. This list is finished by zero. Remember that friendship is always mutual in this group.

    Output

    The first line of output should contain the number of people in the first team or zero if it is impossible to divide people into two teams. If the solution exists you should write the list of the first group into the second line of output. Numbers should be divided by single space. If there are more than one solution you may find any of them.

    Sample

    inputoutput
    7
    2 3 0
    3 1 0
    1 2 4 5 0
    3 0
    3 0
    7 0
    6 0
    
    4
    2 4 5 6
    
    Problem Author: Dmitry Filimonenkov Problem Source: Tetrahedron Team Contest May 2001
    **********************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<cctype>
     8 #include<queue>
     9 #include<stack>
    10 using namespace std;?//贪心
    11 int map[101][101];
    12 int group[101];
    13 int i,j,k,n;
    14 int data;
    15 int flag;
    16 int main()
    17 {
    18     cin>>n;
    19     if(n<2)
    20     {
    21         cout<<'0'<<endl;
    22         return 0;
    23     }
    24     memset(map,0,sizeof(map));
    25     memset(group,-1,sizeof(group));
    26     for(i=1;i<=n;i++)
    27       {
    28         while(cin>>k&&k)
    29          {
    30              map[i][k]=1;
    31              map[k][i]=1;
    32          }
    33       }
    34 
    35     for(i=1;i<=n;i++)
    36      {
    37          if(group[i]==-1)
    38          {
    39              group[i]=0;
    40              for(j=1;j<=n;j++)
    41               if(map[i][j]==1&&group[j]==-1)
    42                 group[j]=(group[i]+1)%2;
    43          }
    44      }
    45      data=0;
    46      for(i=1;i<=n;i++)
    47       if(group[i]==0)
    48         data++;
    49     cout<<data<<endl;
    50     cout<<"1 ";
    51     for(i=2;i<=n;i++)
    52       if(group[i]==0)
    53         cout<<i<<' ';
    54     cout<<endl;
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    jsp页面数据分页模仿百度分页效果
    java EL表达式
    服务器端javascript——Rhino和Node
    HBase协处理器
    Hbase 计数器
    javascript正则表达式(二)——方法
    javascript正则表达式(一)——语法
    javascript模块化
    使用sqoop工具从oracle导入数据
    HBASE API操作问题总结
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3237062.html
Copyright © 2011-2022 走看看