码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4751 Divide Groups 2—sat问题 还是未理解

时间:2015-04-29 10:07:17      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:hdu4751   divide groups   dfs   二分匹配   

Divide Groups

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


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
 
感觉这道题我做的无意义。
事后再仔细研究一下2—sat问题吧;
代码:
#include <cstdio>
#include <queue>
#include <cstring>
#define SIZE 110
using namespace std ;
bool map[SIZE][SIZE] ;
int color[SIZE] , n ;
bool bfs(int pos)
{
	queue<int> que ;
	que.push(pos) ;
	color[pos] = 1 ;
	while(!que.empty())
	{
		int now = que.front() ;
		que.pop() ;
		for(int i = 1 ; i <= n ; ++i)
		{
			if(map[now][i])
			{
				if(color[now] == color[i])
					return false ;
				else if(color[i] == 0)
				{
					color[i] = -color[now] ;
					que.push(i) ;
				}
			}
		}
	}
	return true ;
}

int main()
{
	while(~scanf("%d",&n))
	{
		for(int i = 0 ; i <= n ; ++i)
			for(int j = 0 ; j <= n ; ++j)
				map[i][j] = true ;
		for(int i = 1 ; i <= n ; ++i)
		{
			int id ;
			while(scanf("%d",&id) && id)
			{
				map[i][id] = false ;
			}
		}
		for(int i = 1 ; i <= n ; ++i)
			map[i][i] = false ;
		for(int i = 1 ; i <= n ; ++i)
		{
			for(int j = 1 ; j <= n ; ++j)
			{
				if(map[i][j])
					map[j][i] = true ;
			}
		}
		bool flag = false ;
		memset(color,0,sizeof(color)) ;
		for(int i = 1 ; i <= n ; ++i)
		{
			if(color[i] == 0)
				if(!bfs(i))
				{
					flag = true ;
					break ;
				}
		}
		if(flag)
			puts("NO") ;
		else
			puts("YES") ;
	}
	return 0 ;
}

与君共勉

hdu 4751 Divide Groups 2—sat问题 还是未理解

标签:hdu4751   divide groups   dfs   二分匹配   

原文地址:http://blog.csdn.net/lionel_d/article/details/45343301

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!