BZOJ4195 程序自动分析

离散化+并查集

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 5;
int prt[maxn], a[maxn*2];
struct node
{
    int x, y, op;
    bool operator < (const node &m) const {
        return op > m.op;
    }
} tmp[maxn];
int getpa(int x)
{
    return prt[x] == x ? x : prt[x] = getpa(prt[x]);
}
int main()
{
    bool ans;
    int t; scanf("%d",&t);
    int n, x, y, op, cnt, tx, ty;
    while(t--){
        ans = true; cnt = 0;
        scanf("%d",&n);
        for(int i = 1; i <= 2*n; i++) prt[i] = i;
        for(int i = 1; i <= n; i++){
            scanf("%d%d%d",&x,&y,&op);
            tmp[i].x = x; tmp[i].y = y; tmp[i].op = op;
            a[++cnt] = x;
            a[++cnt] = y;
        }
        sort(a+1,a+cnt+1);
        sort(tmp+1,tmp+n+1);
        cnt = unique(a+1,a+cnt+1)-a-1;
        for(int i = 1; i <= n; i++){
            if(tmp[i].op == 1){
                tx = lower_bound(a+1,a+cnt+1,tmp[i].x)-a;
                ty = lower_bound(a+1,a+cnt+1,tmp[i].y)-a;
                prt[getpa(max(tx,ty))] = getpa(min(tx,ty));
            }
            else{
                tx = lower_bound(a+1,a+cnt+1,tmp[i].x)-a;
                ty = lower_bound(a+1,a+cnt+1,tmp[i].y)-a;
                if(getpa(tx) == getpa(ty)){
                    ans = false;
                    break;
                }
            }
        }
        if(ans) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/kkjy_00/article/details/87286761