HNOI2010平面图判定(2-SAT + 平面图判定定理)

 

思路:

手动模拟可以发现,除了环上的边,对于两条边来说,如果在环内相交了,我们可以将其中一条边翻到环外,于是我们有一个暴力的思路,枚举两条不在环上的边,如果相交,我们将其按照2_SAT的建图方式建图,判断是否能一个在环内一个在环外.

然后暴力枚举是O(m^2)的.此时我们可以根据一个平面图的前提条件

m\leq 3n - 6,其中m为边数,n为点数,将不可能形成平面图的情况筛除,然后枚举边就成了O(n^2)的了.

代码:

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
#define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 2e6 + 10, M = 410, L = 3e4 + 5;
int n, m, x[L], y[L], pos[M];
int h[L], v[N], to[N], tot;
int dfn[L], low[N], tim, cnt, instk[L], stk[L], c[L], top, circle[M][M], C[L], Ex[L], Ey[L];
void tarjan(int x) {
	dfn[x] = low[x] = ++tim;
	stk[++top] = x; instk[x] = 1;
	for (int i = h[x], y; i; i = to[i])
		if (!dfn[y = v[i]]) {
			tarjan(y);
			low[x] = min(low[x], low[y]);
		}else if (instk[y]) low[x] = min(low[x], dfn[y]);
	if (dfn[x] == low[x]) {
		++cnt; int y;
		do {
			y = stk[top--];
			instk[y] = 0;
			c[y] = cnt;
		} while (x != y);
	}
}
void add(int a, int b) {
	v[++tot] = b, to[tot] = h[a], h[a] = tot;
}
void clear() {
	mem(h, 0), mem(dfn, 0), mem(circle, 0);
	tot = tim = top = cnt = 0;
}
void solve() {
	clear();
	qio >> n >> m;
	for (int i = 1; i <= m; ++i) {
		qio >> x[i] >> y[i];
		if (x[i] > y[i]) swap(x[i], y[i]);
	}
	for (int i = 1; i <= n; ++i) {
		qio >> C[i], pos[C[i]] = i + 1;
		if (i > 1) circle[C[i]][C[i - 1]] = circle[C[i - 1]][C[i]] = 1;
	}
	circle[C[1]][C[n]] = circle[C[n]][C[1]] = 1;
	if (m > 3 * n - 6) return qio << "NO\n", void(0);
	int tc = 0;
	for (int i = 1; i <= m; ++i) 
		if (!circle[x[i]][y[i]]) Ex[++tc] = x[i], Ey[tc] = y[i];
	m = tc;
	for (int i = 1; i < m; ++i) {
		for (int j = i + 1; j <= m; ++j) {
			int u = pos[Ex[i]], v = pos[Ey[i]], x = pos[Ex[j]], y = pos[Ey[j]];
			if (u > v) swap(u, v); if (x > y) swap(x, y);
			if ((u < x && x < v && v < y) || (x < u && u < y && y < v)) 
				add(i, j + m), add(j + m, i), add(i + m, j), add(j, i + m);
		}
	}
	for (int i = 1; i <= m * 2; ++i)
		if (!dfn[i]) tarjan(i);
	for (int i = 1; i <= m; ++i) 
		if (c[i] == c[i + m]) return qio << "NO\n", void(0);
	qio << "YES\n";
}
signed main() {
	int T;
	qio >> T;
	while (T--) solve();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值