【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;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值