NOI 2015 程序自动分析

//要先处理=再判断!= 
//离散化后并的是地址而非数值 
//low_bound可查询特定数值所在的地址,和map差不多?
//思考查找其他离散化的方法 
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, t, tot;
int f[2*maxn], e[2*maxn];
struct node{
    int x, y, flg;
} a[maxn];

inline void getInit(){
    int nn = 2*n;
    for(int i = 0; i <= nn; i++)    f[i] = i;
    tot = 0;
}

inline int find(int x){
    if(x == f[x])   return x;
    return f[x] = find(f[x]);
}

inline void merge(int x, int y){
    f[find(x)] = f[find(y)];
}

inline bool ask(int x, int y){
    return find(x) == find(y);
}

int main(){
    scanf("%d", &t);
    while(t--){
        bool flag = 0;
        scanf("%d", &n);
        getInit();
        for(int i = 1; i <= n; i++){
            scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].flg);
            e[++tot] = a[i].x;
            e[++tot] = a[i].y;
            //if(a[i].flg)  merge(a[i].x, a[i].y);
        }
        sort(e+1, e+tot+1);
        for(int i = 1; i <= n; i++)
            if(a[i].flg)
                merge(lower_bound(e+1, e+tot+1, a[i].x)-e, lower_bound(e+1, e+tot+1, a[i].y)-e);
        for(int i = 1; i <= n; i++)
            if(!a[i].flg)
                if(ask(lower_bound(e+1, e+tot+1, a[i].x)-e, lower_bound(e+1, e+tot+1, a[i].y)-e)){
                    printf("NO\n");
                    flag = 1;
                    break;
                }
        if(!flag)   printf("YES\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值