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
  • 相关阅读:
    vue入门:(方法、侦听器、计算属性)
    vue入门:(模板语法与指令)
    前端资源
    CSS3总结七:变换(transform)
    CSS总结六:动画(一)ransition:过渡、animation:动画、贝塞尔曲线、step值的应用
    设计模式(一):简单工厂
    正则命令积累
    命令模式 & 策略模式 & 模板方法
    抽象工厂:简单游戏角色
    简单工厂模式—>工厂模式
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3237062.html
Copyright © 2011-2022 走看看