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

一看就是离散化,先去满足相等的条件,相等即为两点联通,或者说在同一个集合内。再看不相等,只有两元素在同一集合才不满足。裸的disjoint-set直接上,常数巨大什么的不管啦。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #define dbg(x) cerr<<#x<<" = "<<x<<endl
 8 #define ddbg(x,y) cerr<<#x<<" = "<<x<<"   "<<#y<<" = "<<y<<endl
 9 using namespace std;
10 typedef long long ll;
11 template<typename T>inline char MIN(T&A,T B){return A>B?A=B,1:0;}
12 template<typename T>inline char MAX(T&A,T B){return A<B?A=B,1:0;}
13 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
14 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
15 template<typename T>inline T read(T&x){
16     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
17     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
18 }
19 const int N=100000+7;
20 struct Remilia_Scarlet{
21     int x,y,d;
22     inline bool operator <(const Remilia_Scarlet&A)const{
23         return d>A.d;
24     }
25 }q[N];
26 int A[N<<1],fa[N<<1];
27 int T,n,m,fx,fy,flag;
28 inline int Get(int x){return fa[x]^x?fa[x]=Get(fa[x]):x;}
29 
30 int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
31     read(T);while(T--){
32         read(n);m=0;flag=0;
33         for(register int i=1;i<=n;++i)A[++m]=read(q[i].x),A[++m]=read(q[i].y),read(q[i].d);
34         sort(A+1,A+m+1);m=unique(A+1,A+m+1)-A-1;sort(q+1,q+n+1);
35         for(register int i=1;i<=m;++i)fa[i]=i;
36         for(register int i=1;i<=n;++i){
37             if(q[i].d){
38                 fx=Get(lower_bound(A+1,A+m+1,q[i].x)-A),fy=Get(lower_bound(A+1,A+m+1,q[i].y)-A);
39                 if(fx^fy)fa[fx]=fy;//ddbg(fx,fy);
40             }
41             else{
42                 fx=Get(lower_bound(A+1,A+m+1,q[i].x)-A),fy=Get(lower_bound(A+1,A+m+1,q[i].y)-A);
43                 if(fx^fy)continue;
44                 flag=1;break;
45             }
46         }
47         flag?printf("NO
"):printf("YES
");
48     }
49     return 0;
50 }
原文地址:https://www.cnblogs.com/saigyouji-yuyuko/p/10623048.html