bzoj4195: [Noi2015]程序自动分析
离散化+并查集。
为了离散化而离线后,我发现可以先把=的连在一起,然后再判不等于。这样做很清晰简洁。
1A真开心。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int n; struct node { int x,y,opt; }a[1100000]; bool cmp(node n1,node n2) { return n1.opt>n2.opt; } int ls[2100000],lslen; void LSH() { lslen=0; for(int i=1;i<=n;i++) { ls[++lslen]=a[i].x; ls[++lslen]=a[i].y; } sort(ls+1,ls+lslen+1); lslen=unique(ls+1,ls+lslen+1)-ls-1; for(int i=1;i<=n;i++) { a[i].x=lower_bound(ls+1,ls+lslen+1,a[i].x)-ls; a[i].y=lower_bound(ls+1,ls+lslen+1,a[i].y)-ls; } } int fa[2100000]; int findfa(int x) { if(fa[x]==x)return x; fa[x]=findfa(fa[x]);return fa[x]; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].opt); LSH(); sort(a+1,a+n+1,cmp); for(int i=1;i<=lslen;i++)fa[i]=i; bool bk=false; for(int i=1;i<=n;i++) { if(a[i].opt==1) { int fx=findfa(a[i].x),fy=findfa(a[i].y); if(fx!=fy) fa[fx]=fy; } else { int fx=findfa(a[i].x),fy=findfa(a[i].y); if(fx==fy) {bk=true;break;} } } if(bk==false)printf("YES\n"); else printf("NO\n"); } return 0; }
pain and happy in the cruel world.