【BZOJ1997】[Hnoi2010]Planar【平面图判定】【2-SAT】

判断存在一条Hamilton回路的图是否是平面图。


和POJ3207几乎一样,只要把 判断相交的条件改成按回路编号比较就行了。

http://blog.csdn.net/braketbn/article/details/50834302


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

using namespace std;

const int maxm = 10005, maxn = maxm << 2, maxs = maxn, maxd = 205;

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

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

struct _pos {
	int x, y;
} p[maxm];

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 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 = (m << 1) - 1; i >= 0; i--)
		if(belong[i] == belong[i ^ 1])
			return 1;
	return 0;
}

int main() {
	int T = iread();
	while(T--) {
		n = iread(); m = iread();
		for(int i = 0; i < maxn; i++) ins[i] = dfn[i] = low[i] = 0, head[i] = -1; cnt = 0;

		for(int i = 0; i < m; i++) p[i] = (_pos){iread(), iread()};
		for(int i = 1; i <= n; i++) id[iread()] = i;

		for(int i = 0; i < m; i++) for(int j = i + 1; j < m; j++) {
			int x1 = id[p[i].x], y1 = id[p[i].y], x2 = id[p[j].x], y2 = id[p[j].y];
			if(x1 > y1) swap(x1, y1);
			if(x2 > y2) swap(x2, y2);
			if((x1 < x2 && x2 < y1 && y1 < y2) || (x2 < x1 && x1 < y2 && y2 < y1)) {
				add(i << 1, j << 1 | 1);
				add(i << 1 | 1, j << 1);
				add(j << 1, i << 1 | 1);
				add(j << 1 | 1, i << 1);
			}
		}

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值