Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1856 Accepted Submission(s): 663
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.
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
思路:因为恰好是两个聚会,所以用判断二分图的思路来写,首先就是如果两个人不是相互认识或者完全不认识的,那么给他们连边,然后在建立的图上面进行判断是否能形成二分图,不过有一点是这题不能取特殊点从1开始一个judge就完事儿,因为有可能这个图本身就是不连通的,所以还得要遍历完所有的点。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
bool tu[200][200],tutu[200][200];
int color[200];
bool judge(int x,int n)
{
for(int i=1; i<=n; i++)
if(tu[x][i])
{
if(color[i]==-1)
{
color[i]=(color[x]+1)%2;
if(!judge(i,n))
return 0;
}
else if(color[i]==color[x])
return 0;
}
return 1;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(color,-1,sizeof(color));
memset(tutu,0,sizeof(tutu));
memset(tu,0,sizeof(tu));
for(int i=1; i<=n; i++)
{
int t;
while(~scanf("%d",&t)&&t)
tutu[i][t]=1;
}
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
if(!tutu[i][j]||!tutu[j][i])
tu[i][j]=tu[j][i]=1;
int flag=1;
for(int i=1; i<=n; i++)
if(color[i]==-1)
{
color[i]=0;
if(!judge(i,n))
{
flag=0;
break;
}
}
if(!flag)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}