程序自动分析

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring> 
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn=1e6+7;
 8 ll x[maxn],y[maxn],tmp[maxn],opt[maxn],fa[maxn];
 9 int tr[maxn],fs[maxn];
10 int t,n,num1,num2,num;
11 int find(int x){
12     if(x==fa[x]) return x;
13     else return fa[x]=find(fa[x]);
14 }
15 void merge(int x,int y){
16     int fx=find(x),fy=find(y);
17     fa[fx]=fy;
18     return;
19 }
20 int main(){
21     cin>>t;
22     while(t--){
23         bool flag=true;num1=0;num2=0;
24         memset(x,0,sizeof(x));
25         memset(fa,0,sizeof(fa)); 
26         memset(tmp,0,sizeof(tmp));
27         memset(tr,0,sizeof(tr));
28         memset(fs,0,sizeof(fs));
29         cin>>n;
30         for(int i=1;i<=n;i++){
31             cin>>x[i]>>y[i]>>opt[i];
32             tmp[++num]=x[i];tmp[++num]=y[i];
33             if(opt[i]==1) tr[++num1]=i;
34             if(opt[i]==0) fs[++num2]=i;
35         } 
36         sort(tmp+1,tmp+num+1);
37         num=unique(tmp+1,tmp+num+1)-(tmp+1);
38         for(int i=1;i<=n;i++){
39             x[i]=lower_bound(tmp+1,tmp+num+1,x[i])-tmp;
40             y[i]=lower_bound(tmp+1,tmp+num+1,y[i])-tmp;
41         } 
42         for(int i=1;i<=10*n;i++) fa[i]=i;
43         for(int i=1;i<=num1;i++){
44             int u=tr[i];
45             int fx=find(x[u]);int fy=find(y[u]);
46             if(fx!=fy) merge(fx,fy); 
47         } 
48         for(int i=1;i<=num2;i++){
49             int u=fs[i];
50             int fx=find(x[u]);int fy=find(y[u]);
51             if(fx==fy) {flag=false;}
52         }
53         if(flag==false) cout<<"NO"<<endl;
54         else cout<<"YES"<<endl;
55     }
56     return 0;
57 } 

学会离散化......

unique......

posted @ 2018-08-26 08:06  lcan  阅读(166)  评论(0编辑  收藏  举报