标签:scan namespace 数组 nod tps can flag unique close
这道题最刻骨铭心的是
。。。离散化数组要开大
https://www.luogu.com.cn/problem/P1955
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 typedef long long ll; 7 #define N 100050 8 int T,n; 9 int fa[N]; 10 ll b[N<<2]; 11 struct node{ 12 ll l,r; 13 int w; 14 bool operator <(const node &x)const{ 15 return w>x.w;} 16 }e[N]; 17 int find(int x){ 18 return fa[x]==x?x:fa[x]=find(fa[x]); 19 } 20 int main(){ 21 scanf("%d",&T); 22 while(T--){ 23 memset(b,0,sizeof(b)); 24 memset(e,0,sizeof(e)); 25 scanf("%d",&n); 26 int len=0,flag=0; 27 for(int i=1;i<=n;i++){ 28 scanf("%lld%lld%d",&e[i].l,&e[i].r,&e[i].w); 29 b[++len]=e[i].l;b[++len]=e[i].r; 30 } 31 sort(b+1,b+1+len);int m=unique(b+1,b+1+len)-b-1; 32 for(int i=1;i<=n;i++){ 33 e[i].l=lower_bound(b+1,b+1+m,e[i].l)-b; 34 e[i].r=lower_bound(b+1,b+1+m,e[i].r)-b; 35 } 36 for(int i=1;i<=m;i++)fa[i]=i; 37 sort(e+1,e+1+n); 38 for(int i=1;i<=n;i++){ 39 int fx=find(e[i].l),fy=find(e[i].r); 40 if(e[i].w){ 41 if(fx!=fy)fa[fx]=fy; 42 } 43 else{ 44 if(fx==fy){puts("NO");flag=1;break;} 45 } 46 } 47 if(!flag)puts("YES"); 48 } 49 return 0; 50 }
int len=0; int len=0,flag=0; for(int i=1;i<=n;i++){ scanf("%lld%lld%d",&e[i].l,&e[i].r,&e[i].w); b[++len]=e[i].l;b[++len]=e[i].r; } sort(b+1,b+1+len);int m=unique(b+1,b+1+len)-b-1; for(int i=1;i<=n;i++){ e[i].l=lower_bound(b+1,b+1+m,e[i].l)-b; e[i].r=lower_bound(b+1,b+1+m,e[i].r)-b; }
标签:scan namespace 数组 nod tps can flag unique close
原文地址:https://www.cnblogs.com/liukx/p/12535726.html