zoukankan      html  css  js  c++  java
  • BZOJ 4195: [Noi2015]程序自动分析 [并查集 离散化 | 种类并查集WA]

    题意:

    给出若干相等和不等关系,判断是否可行


    woc NOI考这么傻逼的题飞快打了一个种类并查集交上了然后爆零...

    发现相等和不等看错了异或一下再叫woc90分

    然后发现md$a eq b, a eq c,不能得到b = c$

    老老实实的把所有相等关系加并查集然后不等关系来判断吧,唉

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N=2e6+5;
    typedef long long ll;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    
    int n, mp[N], m;
    struct meow{
        int x, y, e;
        bool operator <(const meow &a) const {return e<a.e;}
    } a[N];
    int fa[N], val[N];
    int find(int x) {return x==fa[x] ? x : fa[x]=find(fa[x]);}
    
    int main() {
        freopen("in","r",stdin);
        int T=read();
        while(T--) {
            n=read(); m=0;
            for(int i=1; i<=n; i++) 
                mp[++m]=a[i].x=read(), mp[++m]=a[i].y=read(), a[i].e=read()^1;
            sort(mp+1, mp+1+m); m = unique(mp+1, mp+1+m) - mp - 1;
            for(int i=1; i<=m; i++) fa[i]=i, val[i]=0;
            int flag=1, i;
            sort(a+1, a+1+n);
            for(i=1; i<=n && !a[i].e; i++) { 
                a[i].x = lower_bound(mp+1, mp+1+m, a[i].x) - mp;
                a[i].y = lower_bound(mp+1, mp+1+m, a[i].y) - mp;
                fa[find(a[i].x)] = find(a[i].y);
            }
            for(; i<=n; i++) {
                a[i].x = lower_bound(mp+1, mp+1+m, a[i].x) - mp;
                a[i].y = lower_bound(mp+1, mp+1+m, a[i].y) - mp;
                if(find(a[i].x) == find(a[i].y) ) {puts("NO"); flag=0; break;}
            }
            if(flag) puts("YES");
        }
    }
  • 相关阅读:
    Linux
    python中元类
    POJ 1182 食物链
    POJ 1733 Parity game
    top 介绍
    周记 2014.8.31
    windows10配置python
    oracle执行update时卡死问题的解决办法
    An Easy Introduction to CUDA C and C++
    super()
  • 原文地址:https://www.cnblogs.com/candy99/p/6596126.html
Copyright © 2011-2022 走看看