HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

Divide Groups

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


Problem Description
HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

  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
 

 

Recommend
liuyiding
 

 

 

只有建图,

判断是否是二分图就可以了。

 

 1 /* ***********************************************

 2 Author        :kuangbin

 3 Created Time  :2013/9/21 星期六 12:22:50

 4 File Name     :2013南京网络赛\1004.cpp

 5 ************************************************ */

 6 

 7 #pragma comment(linker, "/STACK:1024000000,1024000000")

 8 #include <stdio.h>

 9 #include <string.h>

10 #include <iostream>

11 #include <algorithm>

12 #include <vector>

13 #include <queue>

14 #include <set>

15 #include <map>

16 #include <string>

17 #include <math.h>

18 #include <stdlib.h>

19 #include <time.h>

20 using namespace std;

21 const int MAXN = 110;

22 

23 int color[MAXN];

24 int head[MAXN];

25 struct Edge

26 {

27     int to,next;

28 }edge[MAXN*MAXN];

29 int tot;

30 void addedge(int u,int v)

31 {

32     edge[tot].to = v;

33     edge[tot].next = head[u];

34     head[u] = tot++;

35 }

36 void init()

37 {

38     tot = 0;

39     memset(head,-1,sizeof(head));

40 }

41 

42 

43 bool dfs(int u,int col)//染色判断二分图

44 {

45     color[u] = col;

46     for(int i = head[u];i != -1;i = edge[i].next)

47     {

48         int v = edge[i].to;

49         if(color[v] != -1)

50         {

51             if(color[v]==col)return false;

52             continue;

53         }

54         if(!dfs(v,!col))return false;

55     }

56     return true;

57 }

58 

59 int g[MAXN][MAXN];

60 int main()

61 {

62     //freopen("in.txt","r",stdin);

63     //freopen("out.txt","w",stdout);

64     int n;

65     while(scanf("%d",&n) == 1)

66     {

67         memset(g,0,sizeof(g));

68         int t;

69         for(int i = 1;i <= n;i++)

70         {

71             while(scanf("%d",&t) && t)

72             {

73                 g[i][t] = 1;

74             }

75         }

76         init();

77         for(int i = 1;i <= n;i++)

78             for(int j = i+1;j <= n;j++)

79                 if(g[i][j] == 0 || g[j][i] == 0)

80                 {

81                     addedge(i,j);

82                     addedge(j,i);

83                 }

84         memset(color,-1,sizeof(color));

85         bool flag = true;

86         for(int i = 1;i <= n;i++)

87             if(color[i] == -1 && dfs(i,0) == false)

88             {

89                 flag = false;

90                 break;

91             }

92         if(flag)printf("YES\n");

93         else printf("NO\n");

94     }

95     return 0;

96 }

 

 

 

 

 

 

 

你可能感兴趣的:(group)