Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2119 Accepted Submission(s): 767
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
Source
Recommend
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int head[110];
struct node
{
int to,next;
}edge[110*110];
int num,map[1100][1100],vis[1100];
void add(int u,int v)
{
edge[num].to=v;
edge[num].next=head[u];
head[u]=num++;
}
void init()
{
num=0;
memset(head,-1,sizeof(head));
}
bool dfs(int u,int col)
{
vis[u]=col;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;//判断两个不认识的人是否在同一组
if(vis[v]!=-1)
{
if(vis[v]==col)//如果在同一组,
return false;
continue;
}
if(!dfs(v,!col))return false;//如果也不在另一组
}
return true;
}
int main()
{
int n,m,i,j,a,b;
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
while(scanf("%d",&a),a)
{
map[i][a]=1;
}
}
init();
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(map[i][j]==0||map[j][i]==0)
{
add(i,j);
add(j,i);
}
}
}
memset(vis,-1,sizeof(vis));
bool flag = true;
for(int i = 1;i <= n;i++)
if(vis[i] == -1 && dfs(i,0) == false)
{
flag = false;
break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}