hdu1814 Peaceful Commission(2-sat)

Peaceful Commission
Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 9
Problem Description
The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.

The Commission has to fulfill the following conditions:
1.Each party has exactly one representative in the Commission,
2.If two deputies do not like each other, they cannot both belong to the Commission.

Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .

Task
Write a program, which:
1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
2.decides whether it is possible to establish the Commission, and if so, proposes the list of members,
3.writes the result in the text file SPO.OUT.
 

Input
In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other. There are multiple test cases. Process to end of file.
 

Output
The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence.
 

Sample Input
  
  
3 2 1 3 2 4
 

Sample Output
1
4
5

2-sat原理可以参考一个大神的博客 http://blog.csdn.net/pi9nc/article/details/11849843
这道题的2-sat大概是:
1.枚举每一对兄弟节点(2-sat())
  (1)先对一个兄弟节点进行dfs
  (2)如果这个兄弟节点能找到着正确的路,则继续找下一对兄弟节点
  (3)否则,对他另一个兄弟节点进行dfs(在这之前,还要删除他兄弟节点搜索时标记的mark)
2.dfs(x)
   (1)如果x的兄弟节点已经加入到路了,那么说明整条路都是错误的
   (2)如果x已经加入到路中,则无需再向下搜索
   (3)再根据x所连的边来dfs


这里有几组数据可以帮助了解代码
4 3
1 4
2 3
7 3

4 3
1 4
1 3
7 3

4 4
1 4
1 8
2 3
7 3

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define MAXN 8010
using namespace std;

int n,m;
int sta[MAXN*2],top;
int mark[MAXN*2];  //mark[x]标记x这个点有没有加入到匹配
vector<int> map[MAXN*2];

//2-sat()每一次调用只进行一次搜索
int dfs(int x)  //从x寻找下一个点,此时x假设已经被选中
{
	if(mark[x^1])return 0;  //如果他的兄弟已被选中过了,说明此时x不能选择,该次整条路的匹配是错误的
	if(mark[x])return 1;   //如果它已经被选择过了,则那说明下面一定匹配成功,无需再向下搜索
	mark[x]=1;    //若x,x^1与其兄弟都没被加入匹配过,则x加入,并向下搜索有没有路
	sta[top++]=x;    //用于记录这条路被标记的点,用于这后如果这条路搜索失败后,将该路上mark的标记删除
	for(int i=0;i<map[x].size();i++)
	{
		if(!dfs(map[x][i]))  return 0;   //如果下面搜索失败的话(即搜索到了两个兄弟节点),说明整条路的搜索都是错误的
	}
	return 1;

}

int Twosat()
{
	memset(mark,0,sizeof(mark));
	for(int i=0;i<n;i+=2)
	{
		if(mark[i]==0&&mark[i^1]==0)   //枚举每一对尚未确定的Ai, Ai‘
		{
			top=0;
			if(dfs(i)==0)    //如果从i点出发找不到路,即匹配失败,则从他的兄弟点找i^1,进行一次搜索
			{
				while(top) mark[sta[--top]]=0;  //将mark数组返回原来的状态
				if(dfs(i^1)==0) return 0;   //如果他的兄弟点也匹配失败,则无法匹配,因为Ai Ai^1这一对无法加入到已匹配队伍中
			}
		}
	}
	return 1;
}

int main()
{
	int i,a,b,ans,t;
	t=0;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		n=n*2;
		for(i=0;i<n;i++)map[i].clear();
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&a,&b);
			a--;
			b--;
			map[a].push_back(b^1);
			map[b].push_back(a^1);
		}
		if(Twosat())
		{
			for(i=0;i<n;i+=2)
			{
				if(mark[i])
					printf("%d\n",i+1);
				else
					printf("%d\n",(i^1)+1);
			}
		}
		else
		{
			printf("NIE\n");
		}
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值