【SPOJ-KING】King【2-SAT】

http://www.spoj.com/problems/KING/

题意:

有n个人选国王,可以投支持或者不支持两种票,每个人可以投两票,问是否存在一种方案,使得每个人至少有一票满足。


裸的2-SAT啦。


(SPOJ第50题啦)


/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 2005, maxs = 10000;

int n, head[maxn], cnt, dfn[maxn], low[maxn], belong[maxn], tot, clo;
bool ins[maxn];

struct _edge {
	int v, next;
} g[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int cread() {
	char ch = getchar(); for(; ch != 'R' && ch != 'U'; ch = getchar());
	return ch == 'R';
}

inline void add(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

int sta[maxs], top;

inline void tarjan(int x) {
	dfn[x] = low[x] = ++clo;
	sta[++top] = x; ins[x] = 1;
	for(int i = head[x]; ~i; i = g[i].next) {
		int v = g[i].v;
		if(!dfn[v]) tarjan(v), low[x] = min(low[x], low[v]);
		else if(ins[v]) low[x] = min(low[x], dfn[v]);
	}
	if(dfn[x] == low[x]) {
		tot++;
		while(1) {
			int u = sta[top--];
			belong[u] = tot;
			ins[u] = 0;
			if(u == x) break;
		}
	}
}

inline bool check() {
	for(int i = (n << 1) - 1; i >= 0; i--)
		if(belong[i] == belong[i ^ 1])
			return 1;
	return 0;
}

int main() {
	while(1) {
		n = iread();
		if(n == 0) break;
		for(int i = 0; i < maxn; i++) dfn[i] = low[i] = belong[i] = ins[i] = 0, head[i] = -1; cnt = 0;
	
		for(int i = 0; i < n; i++) {
			int c1 = cread(), x = iread(), c2 = cread(), y = iread();
			if(c1 == 1)
				if(c2 == 1) add(x << 1, y << 1 | 1), add(y << 1, x << 1 | 1);
				else add(x << 1, y << 1), add(y << 1 | 1, x << 1 | 1);
			else
				if(c2 == 1) add(x << 1 | 1, y << 1 | 1), add(y << 1, x << 1);
				else add(x << 1 | 1, y << 1), add(y << 1 | 1, x << 1);
		}

		tot = top = clo = 0;
		for(int i = (n << 1) - 1; i >= 0; i--) if(!dfn[i]) tarjan(i);
	
		if(check()) printf("N\n");
		else printf("Y\n");
	}
	return 0;
}


你可能感兴趣的:(2-sat)