首页 > 搜索 > DFS搜索 > HDU 1814 Peaceful Commission-DFS-[解题报告] C++
2013
12-23

HDU 1814 Peaceful Commission-DFS-[解题报告] C++

Peaceful Commission

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

3 2
1 3
2 4

样例输出:

1
4
5

模版题,按给定边直接建图

若有解输出最小字典序解

 

注意此模版求的就是最小字典序解

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>

#define ll double
#define eps 1e-5

using namespace std;

inline ll Max(ll a,ll b){return a>b?a:b;}
inline ll Min(ll a,ll b){return a<b?a:b;}


#define N 8010*2
#define M 40000+5

struct Edge{
	int to, nex;
}edge[M];

int head[N], edgenum;
void addedge(int u, int v){
	Edge E = {v, head[u]};
	edge[edgenum] = E;
	head[u] = edgenum ++;
}

bool mark[N];
int Stack[N], top;
void init(){
	memset(head, -1, sizeof(head)); edgenum = 0;
	memset(mark, 0, sizeof(mark));
}

bool dfs(int x){
	if(mark[x^1])return false;//一定是拆点的点先判断
	if(mark[x])return true;

	mark[x] = true;
	Stack[top++] = x;

	for(int i = head[x]; i != -1; i = edge[i].nex)
		if(!dfs(edge[i].to)) return false;

	return true;
}

bool solve(int n){
	for(int i = 0; i < n; i+=2)
		if(!mark[i] && !mark[i^1])
		{
			top = 0;
			if(!dfs(i))//dfs(i) 假设i成立 
			{//当i不成立时,把所有因i成立的点都取消标记
				while( top ) mark[ Stack[--top] ] = false;
				if(!dfs(i^1))
					return false;//若i的对立面也不成立则i点无解
			}
		}
		return true;
}

int main(){
	int n, i, j, m;
	while(~scanf("%d%d",&n,&m)){
		n <<= 1;
		init();

		while(m--)
		{
			int u,v;
			scanf("%d %d",&u,&v); u--, v--;
			addedge(u,v^1);
			addedge(v,u^1);
		}

		if(solve(n))
		{
			for(i=0;i<n;i++)
				if(mark[i])printf("%d\n",i+1);
		}
		else
			printf("NIE\n");

	}
	return 0;
}

 

解题报告转自:http://blog.csdn.net/acmmmm/article/details/14169475


,
  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥

  2. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。