前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[bzoj4195][Noi2015]程序自动分析

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

作者头像
全栈程序员站长
发布2021-07-01 10:32:26
3070
发布2021-07-01 10:32:26
举报
文章被收录于专栏:全栈程序员必看

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

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

卡点:当成了元素非01

C++ Code:

代码语言:javascript
复制
#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;
}

转载于:https://www.cnblogs.com/Memory-of-winter/p/10363225.html

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101131.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档