Bzoj1997: [Hnoi2010]Planar

题面

Bzoj

Sol

平面图的性质:边数小于等于3n-6
然后就是裸\(2-SAT\)

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2005);
const int __(2e6 + 5);

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, first[_], cnt, num, a[_], b[_], id[_];
int S[_], vis[_], dfn[_], low[_], Index, col[_];
struct Edge{
	int to, next;
} edge[__];

IL void Add(RG int u, RG int v){
	edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

IL void Tarjan(RG int u){
	vis[u] = 1, dfn[u] = low[u] = ++Index, S[++S[0]] = u;
	for(RG int e = first[u]; e != -1; e = edge[e].next){
		RG int v = edge[e].to;
		if(!dfn[v]) Tarjan(v), low[u] = min(low[u], low[v]);
		else if(vis[v]) low[u] = min(low[u], dfn[v]);
	}
	if(dfn[u] != low[u]) return;
	RG int v = S[S[0]--]; col[v] = ++num, vis[v] = 0;
	while(v != u) v = S[S[0]--], col[v] = num, vis[v] = 0;
}

IL int Cross(RG int l1, RG int r1, RG int l2, RG int r2){
	if(l1 > r1) swap(l1, r1);
	if(l2 > r2) swap(l2, r2);
	if(l1 > l2) swap(l1, l2), swap(r1, r2);
	return l1 < l2 && r1 > l2 && r1 < r2;
}

int main(RG int argc, RG char* argv[]){
	for(RG int T = Input(); T; --T){
		n = Input(), m = Input();
		if(m > 3 * n - 6){
			puts("NO");
			for(RG int i = 1; i <= m; ++i) Input(), Input();
			for(RG int i = 1; i <= n; ++i) Input();
			continue;
		}
		Fill(first, -1), Fill(dfn, 0), cnt = num = Index = 0;
		for(RG int i = 1; i <= m; ++i) a[i] = Input(), b[i] = Input();
		for(RG int i = 1; i <= n; ++i) id[Input()] = i;
		for(RG int i = 1; i < m; ++i){
			if(abs(id[a[i]] - id[b[i]]) == 1) continue;
			for(RG int j = i + 1; j <= m; ++j){
				if(abs(id[a[j]] - id[b[j]]) == 1) continue;
				if(Cross(id[a[i]], id[b[i]], id[a[j]], id[b[j]]))
					Add(i, j + m), Add(j + m, i), Add(j, i + m), Add(i + m, j);
			}
		}
		RG int tmp = m + m, flg = 0;
		for(RG int i = 1; i <= tmp; ++i) if(!dfn[i]) Tarjan(i);
		for(RG int i = 1; i <= m; ++i)
			if(col[i] == col[i + m]){
				puts("NO"), flg = 1;
				break;
			}
		if(!flg) puts("YES");
	}
	return 0;
}

posted @ 2018-02-24 17:22  Cyhlnj  阅读(106)  评论(0编辑  收藏  举报