首页 > 代码库 > HDU 4751 Divide Groups

HDU 4751 Divide Groups

Divide Groups

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4751
64-bit integer IO format: %I64d      Java class name: Main
 

  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

Source

2013 ACM/ICPC Asia Regional Nanjing Online
 
解题:隐藏得比较深的二分图判定。
 
首先是两个人i和j,记住,关系在此不能传递,如果i不认识j,j也不认识i,那么i,j必然在不同的集合中,如果i认识j,但是j不认识i,i、j也应该在不同的集合中,求两场聚会,二分图判断。
 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 110;18 int g[maxn][maxn],color[maxn],n;19 bool bfs(int v) {20     queue<int>q;21     color[v] = 0;22     q.push(v);23     while(!q.empty()) {24         int u = q.front();25         q.pop();26         for(int i = 1; i <= n; ++i) {27             if(i == u || g[u][i] == 2) continue;28             if(color[i] == -1) {29                 color[i] = color[u]^1;30                 q.push(i);31             } else if(color[i] == color[u]) return false;32         }33     }34     return true;35 }36 int main() {37     while(~scanf("%d",&n)) {38         memset(g,0,sizeof(g));39         memset(color,-1,sizeof(color));40         for(int i = 1; i <= n; ++i) {41             int j = 0;42             while(scanf("%d",&j),j) {43                 g[i][j]++;44                 g[j][i]++;45             }46         }47         bool flag = true;48         for(int i = 1; i <= n; ++i)49             if(color[i] == -1) {50                 flag = bfs(i);51                 if(!flag) break;52             }53         if(flag) puts("YES");54         else puts("NO");55     }56     return 0;57 }
View Code

 

HDU 4751 Divide Groups