现将输入排序,把merge排在前面 ,避免冗余计算
1 n=rd(); 2 FOR(i,1,n) 3 { 4 s[i].x=rd(),a[++tot]=s[i].x, 5 s[i].y=rd(),a[++tot]=s[i].y, 6 s[i].b=rd(); 7 } 8 sort(s+1,s+n+1); 9 sort(a+1,a+tot+1); 10 tot=unique(a+1,a+tot+1)-a-1; 12 FOR(i,1,tot) fa[i]=i; 13 //map<hhh,int> mp; 14 bool flag=1; 15 FOR(i,1,n) 16 { 17 int xx=lower_bound(a+1,a+tot+1,s[i].x)-a; 18 int yy=lower_bound(a+1,a+tot+1,s[i].y)-a; 19 int gx=get(xx),gy=get(yy); 20 if(s[i].b==1) fa[gx]=gy;
26 else if(gx==gy)
27 {flag=0;cout<<"NO"<<endl;break;}
33 if(flag) cout<<"YES"<<endl;