hdu4751

来源:互联网 发布:java lang包 编辑:程序博客网 时间:2024/06/10 14:39
C - Divide Groups
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description


  This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos. 
  After carefully planning, Tom200 announced his activity plan, one that contains two characters: 
  1. Whether the effect of the event are good or bad has nothing to do with the number of people join in. 
   2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
  The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one's energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large. 
  Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.
 

Input

  The input contains several test cases, terminated by EOF. 
  Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event. 
  N lines follow. The i-th line contains some integers which are the id 
of students that the i-th student knows, terminated by 0. And the id starts from 1.
 

Output

  If divided successfully, please output "YES" in a line, else output "NO". 
 

Sample Input

33 01 01 2 0
 

Sample Output

YES
题意:有个人要开两个party,来的一些人有的之间认识有的之间不认识,要让在一个party里的所有人都相互认识,能不能办得到,可以利用二分图判断解决,不认识的人之间建边,判断二分图:
ac代码:
#include <iostream>#include <stdio.h>#include <vector>#include <string.h>#include <queue>using namespace std;const int maxn=1000;vector<int>G[maxn];int col[maxn];bool bfs(int s){    col[s]=1;    queue<int>que;    que.push(s);    while(!que.empty())    {        int u=que.front();        que.pop();        for(int i=0; i<G[u].size(); i++)        {            int v=G[u][i];            if(col[v]==-1)            {                que.push(v);                col[v]=!col[u];            }            if(col[v]==col[u])                return false;        }    }    return true;}int map[maxn][maxn];int main(){    int n;    while(~scanf("%d",&n))    {        memset(map,0,sizeof(map));        for(int i=1; i<=n; i++)        {            int x;            scanf("%d",&x);            while(x!=0)            {                map[i][x]=1;                scanf("%d",&x);            };        }        for(int i=1;i<=n;i++)        {            for(int j=i+1;j<=n;j++)            {                if(map[i][j]==0||map[j][i]==0)                {                    G[i].push_back(j);                    G[j].push_back(i);                }            }        }         memset(col,-1,sizeof(col));        for(int i=1;i<=n;i++)        {           if(col[i]==-1&&!bfs(i))            {                printf("NO\n");                goto endW;            }        }        puts("YES");        endW:;         for(int i=1;i<=n;i++)            G[i].clear();    }    return 0;}



0 0