[BZOJ4195] [NOI2015] 程序自动分析 - 并查集 + 离散化

辣鸡题啊QAQ然而我交了好几次

#include "algorithm" 
#include "iostream"
#include "stdio.h"
#define rep(f,a,b) for(f=a;f<=b;f++)

using namespace std;

const int N=1000005;
inline int read(){
	int v=0; char ch=getchar();
	while (ch<'0'||ch>'9') ch=getchar();
	while (ch<='9'&&ch>='0') { v=v*10+ch-'0'; ch=getchar();}
	return v;
}
int fa[N],n,a[N],b[N],e[N],c[2*N];
int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}

void work(){ 
	n=read(); int i;
	rep(i,1,2*n) fa[i]=i;
	rep(i,1,n) {
		c[2*i-1]=a[i]=read();
        c[2*i]=b[i]=read();
		e[i]=read(); 
	} int cl=0; i=1;
	sort(c+1,c+2*n+1);
	while (i<=2*n){ 
		c[++cl]=c[i++];
		while (c[i]==c[i-1]&&++i<=2*n);
	} int bot,top,mid;
	rep(i,1,n){ bot=1,top=cl;
		while (mid=(bot+top+1)>>1,bot<top){
			c[mid]>a[i]?top=mid-1:bot=mid;
		} mid=(bot+top)>>1;
        a[i]=mid; bot=1,top=cl;
		while (mid=(bot+top+1)>>1,bot<top){
			c[mid]>b[i]?top=mid-1:bot=mid;
		} mid=(bot+top)>>1;
        b[i]=mid;
	} int x,y;
	rep(i,1,n){
		x=find(a[i]),y=find(b[i]);
		if (x!=y && e[i]) fa[x]=y;
	}
	rep(i,1,n){
		x=find(a[i]),y=find(b[i]);
		if (x==y && !e[i]){ puts("NO"); break;}
    }
	if(i>n) puts("YES");
}

int main(){
	int T; T=read();
	while(T--) work();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值