HDOJ-----4751---Divide Groups二分判断

来源:互联网 发布:炫光制作软件 编辑:程序博客网 时间:2024/05/16 13:40

Divide GroupsTime Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

SubmitStatus

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
给出一群人,问能不能分成两组,每组的成员间两两互相认识,a认识b,b也认识a才算互相认识

存在两两互相认识关系的人,无论加入哪一边都没有影响,而两两单向认识或者不认识的检查是否满足二分图,进行染色

原来是个模板题,二分图染色模板。

#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<map>#include<queue>#include<algorithm>#include<cmath>#include<vector>#define PI acos(-1.0)#define INF 0x3f3f3f3f#define CL(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long LL;const int maxn = 2e2+10;const int MOD = 1e9+7;int link[maxn];bool mp[maxn][maxn];vector <int > G[maxn];bool judge(int u, int color){    link[u] = color;    for(int i = 0; i < G[u].size(); i++){        int v = G[u][i];        if(link[v] == color || (link[v] == -1 && !judge(v, !color))) return false;    }    return true;}int main(){    int a, t, n, kcase = 1;    while(scanf("%d", &t) == 1){        for(int i = 1; i <= t; i++) G[i].clear();        CL(mp, false);        for(int i = 1; i <= t; i++){            while(scanf("%d", &a) == 1 && a) mp[i][a] = true;        }        for(int i = 1; i <= t; i++){            for(int j = i+1; j <= t; j++){                if(!mp[i][j] || !mp[j][i]){                    G[i].push_back(j); G[j].push_back(i);//两两不互相认识的建边                }            }        }        bool flag = true;        CL(link, -1);        for(int i = 1; i <= t; i++){            if(link[i] == -1 && !judge(i, 0)){                flag = false;                break;            }        }        printf(flag ? "YES\n" : "NO\n");    }    return 0;}


0 0