【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=4195
【算法】
并查集
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 1000010 struct info { int u,v,w; } a[MAXN]; int i,n,len,T; int l[MAXN],r[MAXN],c[MAXN],fa[MAXN<<1],t[MAXN<<1]; bool flag; inline int get_root(int x) { if (fa[x] == x) return x; return fa[x] = get_root(fa[x]); } inline void merge(int u,int v) { int sx = get_root(u), sy = get_root(v); fa[sx] = sy; } inline bool cmp(info a,info b) { return a.w > b.w; } int main() { scanf("%d",&T); while (T--) { scanf("%d",&n); len = 0; for (i = 1; i <= n; i++) { scanf("%d%d%d",&l[i],&r[i],&c[i]); t[++len] = l[i]; t[++len] = r[i]; } sort(t+1,t+len+1); len = unique(t+1,t+len+1) - t - 1; for (i = 1; i <= n; i++) { a[i].u = lower_bound(t+1,t+len+1,l[i]) - t; a[i].v = lower_bound(t+1,t+len+1,r[i]) - t; a[i].w = c[i]; } sort(a+1,a+n+1,cmp); for (i = 1; i <= len; i++) fa[i] = i; for (i = 1; i; i++) { if (a[i].w == 1) merge(a[i].u,a[i].v); else break; } flag = false; for (; i <= n; i++) { if (get_root(a[i].u) == get_root(a[i].v)) { flag = true; break; } } if (flag) printf("NO "); else printf("YES "); } return 0; }