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


    题目大意:有$n(nleqslant10^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;
    }
    

      

  • 相关阅读:
    Git配置
    第一次作业
    第二次作业
    python目前最好用的IDE——pycharm
    九九乘法表
    python语言的优点和缺点
    Python高效编程的19个技巧
    Python中 filter | map | reduce | lambda的用法
    Python 代码优化常见技巧
    求逆序对
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/10363225.html
Copyright © 2020-2023  润新知