[HDU 1814] Peaceful Commission【2-SAT】

  • 2018-01-12
  • 0
  • 0


Time Limit: 10000/5000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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 .


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.

Sample Input

3 2 1 3 2 4

Sample Output

1 4 5


POI 2001


威士忌   |   We have carefully selected several similar problems for you:  3622 1815 1816 3062 1811


这是一道 2-SAT 的模板题。

由于要求输出字典序最小的答案,一般使用建图后采用 DFS 暴搜的方法。

该方法较易理解并运用,故可以将其作为 2-SAT 的入门练习。

【2-SAT (2-satisfiability) 模型】

给出以 N 个布尔值组成的序列 A[1..N] ,以及一些限制,每个限制最多针对两个元素,求满足所有限制关系的 A[] 是否存在,以及求解出一组 A[]。

【2-SAT 的建图】

顶点 2 * i 表示第 i 个布尔值取 1 的情况,顶点 2 * i + 1 表示第 i 个布尔值取 0 的情况,那么顶点 i ^ 1 (一作 i XOR 1,下文简记为 i') 即为顶点 i 取反的情况。

我们用边 u -> v 表示取 u 之后必取 v 的限制,那么可以得到以下构图方法(只列出部分):

  1. u : u' -> u
  2. not(u) : u -> u'
  3. u and vu' -> u, v' -> v
  4. not(u and v)u -> v', v -> u'
  5. u or vu' -> v, v' -> u
  6. not(u or v)u -> u', v -> v'
  7. u xor vu -> v', u' -> v, v -> u', v' -> u
  8. not(u xor v)u -> v, u' -> v', v -> u, v' -> u'

这样每次若取点 u,就要取它的所有后代节点

【2-SAT 的 O(NM) 暴力算法寻找字典序最小解】


Wikipedia: https://en.wikipedia.org/wiki/2-satisfiability

Code: O(TNM) [2384K, 514MS]

using namespace std;

int N, M;

struct Edge{
	int np;
	Edge *nxt;

struct Graph{
	Edge *V[16002], E[40002];
	int tope;
	inline void clear() {tope = 0, memset(V, 0, sizeof(V));}
	inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];}
} G;

bool vis[16002];
int Stack[16002], tops;

inline bool dfs(int u){
	if(vis[u ^ 1]) return 0;
	if(vis[u]) return 1;
	Stack[++tops] = u, vis[u] = 1;
	for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
		if(!dfs(ne->np)) return 0;
	return 1;

inline bool violent(){
	for(register int i = 0; i < (N << 1); i++)
		if(!vis[i] && !vis[i ^ 1]){
			tops = 0;
			if(dfs(i)) continue;
			while(tops) vis[Stack[tops--]] = 0;
			if(!dfs(i ^ 1)) return 0;
	return 1;

int main(){
	while(scanf("%d%d", &N, &M) != EOF){
		G.clear(), memset(vis, 0, sizeof(vis));  // Beware of the initialization !!!
		for(register int i = 1; i <= M; i++){
			int u, v;
			scanf("%d%d", &u, &v);
			u--, v--;
			G.addedge(u, v ^ 1), G.addedge(v, u ^ 1);
			// If we choose u, we then mustn't choose v, but v ^ 1
			for(register int i = 0; i <= (N << 1); i++)
				if(vis[i]) printf("%d\n", i + 1);
		else puts("NIE");
	return 0;





OPEN AT 2017.12.10

If the code cannot be displayed normally, please refresh the page.


- Theme by Qzhai