HDU4751Divide Groups【判断二分图】

Divide Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1141    Accepted Submission(s): 414


Problem 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
3 3 0 1 0 1 2 0
 
Sample Output
YES
 
Source
 
 

大意:有n个人,现在告诉你每个人认识哪些人,问你能不能把这些人分成两个集合,使每个集合中的人之间都相互认识(认识不可传递)

思路:用二分图来做:

我们从二分图的定义下手,首先要有两个集合,并且这两个集合之内的元素之间没有边

那么我们可以这么建边:

若a认识b但b不认识a则ab之间建一条边

若b认识a但是a不认识b则ab之间建一条边

若ab不认识那么建一条边

这样建边,只有ab相互认识没有边,只要判断改图是否是二分图即可

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6 
 7 const int maxn = 105;
 8 int G[maxn][maxn];
 9 vector<int> mat[maxn];
10 int color[maxn];
11 
12 bool is_bi(int u) {
13     for(int i = 0; i < mat[u].size(); i++) {
14         int v = mat[u][i];
15         if(color[v] == color[u]) return false;
16         if(color[v] == 0) {
17             color[v] = 3 - color[u];
18             if(!is_bi(v)) return false;
19         }
20     }
21     return true;
22 }
23 
24 bool solve(int n) {
25     for(int i = 1; i <= n; i++) {
26         memset(color, 0, sizeof(color));
27         color[i] = 1;
28         if(!is_bi(i)) return false;
29     }
30     return true;
31 }
32 
33 int main() {
34     int n;
35     int id;
36     while(EOF != scanf("%d",&n)) {
37         memset(G, 0, sizeof(G));
38         memset(mat, 0, sizeof(mat));
39         for(int i = 1; i <= n; i++) {
40             mat[i].clear();
41             while(scanf("%d",&id) && id) {
42                 G[i][id] = 1; 
43             }
44         }
45         for(int i = 1; i <= n; i++) {
46             for(int j = 1; j <= n; j++) {
47                 if(i == j) continue;
48                 if(G[i][j] + G[j][i] != 2) {
49 //                    printf("%d %d
",i, j);
50                     mat[i].push_back(j);
51                     mat[j].push_back(i);
52                 }
53             }
54         }
55         if(solve(n)) puts("YES");
56         else puts("NO");
57     }
58     return 0;
59 }
View Code
原文地址:https://www.cnblogs.com/zhanzhao/p/3899028.html