【BZOJ1997】【HNOI2010】Planar

【题目链接】

  • 点击打开链接

【思路要点】

  • 补档博客,无题解。

【代码】

#include
using namespace std;
#define MAXN	20005
int f[MAXN], x[MAXN], y[MAXN], home[MAXN], value[MAXN];
bool circle[MAXN];
int F(int x) {
	if (f[x] == x) return x;
	else return f[x] = F(f[x]);
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= m; i++)
			scanf("%d%d", &x[i], &y[i]);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &value[i]);
			home[value[i]] = i;
		}
		for (int i = 1; i <= m; i++) {
			x[i] = home[x[i]];
			y[i] = home[y[i]];
			if (x[i] > y[i]) swap(x[i], y[i]);
			if (x[i] + 1 == y[i] || (x[i] == 1 && y[i] == n)) circle[i] = true;
			else circle[i] = false;
		}
		if (m > 3 * n) {
			printf("NO\n");
			continue;
		}
		for (int i = 1; i <= 2 * m + 1; i++)
			f[i] = i;
		for (int i = 1; i <= m; i++){
			if (circle[i]) continue;
			for (int j = i + 1; j <= m; j++) {
				if (circle[j]) continue;
				if (x[i] == x[j] || x[i] == y[j] || y[i] == x[j] || y[i] == y[j]) continue;
				if ((x[j] > x[i] && x[j] < y[i]) ^ (y[j] > x[i] && y[j] < y[i])) {
					int tmp = i * 2, tnp = j * 2;
					f[F(tmp)] = F(tnp ^ 1);
					f[F(tnp)] = F(tmp ^ 1);
				}
			}
		}
		bool flg = true;
		for (int i = 1; i <= m; i++)
			if (F(i * 2) == F(i * 2 ^ 1)) {
				flg = false;
				break;
			}
		if (flg) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

你可能感兴趣的:(【OJ】BZOJ,【类型】做题记录,【算法】2-SAT,【算法】平面图相关)