首页 > 搜索 > BFS搜索 > HDU 4751-Divide Groups-BFS-[解题报告]HOJ
2015
09-17

HDU 4751-Divide Groups-BFS-[解题报告]HOJ

Divide Groups

问题描述 :

Count The Pairs

  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.

输入:

  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.

输出:

  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.

样例输入:

3
3 0
1 0
1 2 0

样例输出:

YES

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=110;
int e[maxn][maxn];
int vis[maxn],n;
int bfs(int x)//从x点开始分组。 
{
	queue<int>q;
	q.push(x);
	vis[x]=1;
	int i,j,k,u,v;
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		for(i=1;i<=n;i++)
		{
			if(e[u][i]==1||u==i)continue;
			if(vis[i]==-1)
			{
				vis[i]=1-vis[u];
				q.push(i);
			}
			else if(vis[i]==vis[u])return 0;
		}
	}
	return 1;
}
int main()
{
	while(cin>>n)
	{
		int i,j,k,a;
		memset(e,0,sizeof(e));
		memset(vis,-1,sizeof(vis));
		for(i=1;i<=n;i++)
		{
			while(cin>>a&&a!=0)
			e[i][a]=1;
		}
		//改成无向图 
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				if(!e[i][j])e[j][i]=0;
		for(i=1;i<=n;i++)
		{
			if(vis[i]!=-1)continue;
			if(!bfs(i))break;
		}
		if(i<=n)cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	return 0;
}
/*
	题意:将n个人分成两组,使得每组中的每个人都认识剩余所有人。
	用二维数组e记录关系,e[i][j]==0表示i不认识j。
	vis[i]表示组别,分别为1和0。则可知若e[i][j]==0 ,则i和j必定是一个是0,一个是1。
bfs一遍,当出现矛盾的时候就不能分组。
	由于枚举所有点作起点,且起点为1组内。又e[i][j]=0,e[j][i]=1和e[i][j]=e[j][i]=0情况相同。
为避免起点为1造成的初始化错误,所以讲有向不认识图,改成无向图。例:不认识关系:3->1->2->4,
开始枚举1,则vis[1]=1,vis[2]=0,vis[4]=1,后枚举3,vis[3]=1,vis[1]=0,矛盾,可情况是能分成(3,2)和(1,4)的。 
*/

参考:http://blog.csdn.net/a601025382s/article/details/11873627


, ,