loj#2129. 「NOI2015」程序自动分析

题目链接

loj#2129. 「NOI2015」程序自动分析

题解

额...
考你会不会离散化优化常数

代码

#include<queue> 
#include<cstdio> 
#include<cstring> 
#include<algorithm> 

inline int read() {
	int x = 0,f = 1; 
	char c = getchar(); 
	while(c < '0' || c > '9')c = getchar(); 
	while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar(); 
	return x * f; 
} 
const int maxn = 2000007; 
int n; 
int x[maxn],y[maxn],z[maxn]; 
int tmp[maxn]; 
int fa[maxn]; 
int find(int x ) { 
	if(fa[x] != x) fa[x] = find(fa[x]); 
	return fa[x]; 
} 
void solve() { 
	n = read();
	int tot = 0;  
	for(int i = 1;i <= n;++ i) { 
		x[i] = read(),y[i] = read();z[i] = read(); 
		tmp[++ tot] = x[i],tmp[++ tot] = y[i]; 
	} 
	std::sort(tmp + 1,tmp + tot + 1); 
	tot = std::unique(tmp + 1,tmp + tot + 1) - tmp - 1; 
	for(int i = 1;i <= tot;++ i) fa[i] = i; 
	for(int i = 1;i <= n;++ i) {  
		x[i] = std::lower_bound(tmp + 1,tmp + tot + 1,x[i]) - tmp, 
		y[i] = std::lower_bound(tmp + 1,tmp + tot + 1,y[i]) - tmp; 
		if(z[i]) fa[find(x[i])] = find(y[i]); 
	}  
	//for(int i = 1;i <= n;++ i) if(z[i]) fa[find(x[i])] = find(y[i]); 
	for(int i = 1;i <= n;++ i) 
		if(!z[i] && find(x[i]) == find(y[i]))  { 
			puts("NO"); 
			return; 
		} 
	puts("YES"); 
} 
int main() { 
	int t = read(); 
	for(int i = 1;i <= t;++ i)  
		solve();  
} 
posted @ 2018-08-31 21:58  zzzzx  阅读(157)  评论(0编辑  收藏  举报