hdu4751(BFS)

Divide Groups

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


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
 

Recommend
liuyiding
        本题要判断所给的图能不能分成两个完全图。
        由于题目说的是有向图,我们这里可以缩成无向图,即两点之间有双向边的连两条无向边,也就是删除只有单向边的边。然后判断是否能分成两个完全图。我们必须懂的这样一个原理,若顶点i属于g1,i 与j之间为边,则顶点j属于g2;若u与j无边,则u属于g1,即u,i属于同一完全图,我们可以判断一下它们之间是否有边,若无则不能分成两个完全图,若有则继续判断。具体的我们可以搜索做。
        看着别人的代码写的,所以写成转载的。谢大牛。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int MAXN=100+10;
int visited[MAXN];
int edg[MAXN][MAXN];
int n;

bool bfs(int s)
{
	queue<int>Q;
	Q.push(s);
	visited[s]=1;
	while(!Q.empty())
	{
		int now=Q.front();
		Q.pop();
		for(int i=1;i<=n;i++)
		{
			if(now==i||edg[now][i])
				continue;
			if(visited[i]==-1)
			{
				visited[i]=1-visited[now];
				Q.push(i);
			}
			else if(visited[now]==visited[i])
				return 0;
		}
	}
	return 1;
}

int main()
{
	int i,t,j;
	while(~scanf("%d",&n))
	{
		memset(edg,0,sizeof(edg));
		memset(visited,-1,sizeof(visited));
		for(i=1;i<=n;i++)
		{
			while(scanf("%d",&t),t)
				edg[i][t]=1;
		}

		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(!edg[i][j])
					edg[j][i]=0;
			}
		}

		for(i=1;i<=n;i++)
		{
			if(visited[i]!=-1)
				continue;
			if(!bfs(i))
				break;
		}

		if(i>n)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值