[bzoj4195][Noi2015]程序自动分析

题目大意:有$n(n\leqslant10^6)$个变量,有若干限制,形如$x_l$与$x_r$必须相等或不相等,问是否有解

题解:并查集,把相同的塞在一个集合里,最后判一下不相等的是否在一个集合内,是则无解

卡点:当成了元素非$0$即$1$

 

C++ Code:

#include <algorithm>
#include <cstdio>
#define maxn 1000010

int Tim, n, tot;
int v[maxn << 1], f[maxn], l[maxn], r[maxn], e[maxn];

int find(int x) { return x == f[x] ? x : (f[x] = find(f[x])); }
inline void merge(int a, int b) { f[find(a)] = find(b); }
int main() {
	scanf("%d", &Tim);
	while (Tim --> 0) {
		scanf("%d", &n); tot = 0;
		for (int i = 0; i < n; ++i) {
			scanf("%d%d%d", l + i, r + i, e + i);
			v[tot++] = l[i], v[tot++] = r[i];
		}
		tot = (std::sort(v, v + tot), std::unique(v, v + tot) - v);
		for (int i = 0; i < tot; ++i) f[i] = i;
		for (int i = 0; i < n; ++i) {
			l[i] = std::lower_bound(v, v + tot, l[i]) - v;
			r[i] = std::lower_bound(v, v + tot, r[i]) - v;
			if (e[i]) merge(l[i], r[i]);
		}

		bool check = true;
		for (int i = 0; i < n; ++i) if (!e[i]) check &= find(l[i]) != find(r[i]);
		puts(check ? "YES" : "NO");
	}
	return 0;
}

  

posted @ 2019-02-11 21:17  Memory_of_winter  阅读(64)  评论(0编辑  收藏  举报