zoukankan      html  css  js  c++  java
  • bzoj4195 [Noi2015]程序自动分析——并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4195

    突然在这道大水题上WA了半天...

    思路很简单,离线处理询问,先把 = 的都加到并查集里,再暴力判断 != 的;

    然而WA了许多遍...对离散化还是太不熟悉了...

    1.fa[] 数组的预处理!平时写的手熟,上来就 for(i=1;i<=n;i++),完全忘了离散化后怎样怎样了啊!!

    2. unique 的时候需要 -1!

    3. lower_bound 时不要多 -1!

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int const maxn=2000005;
    int T,n,fa[maxn],head[maxn],ct,d1[maxn],d2[maxn],tmp[maxn],tot,cnt,e[maxn];
    struct N{
        int to,next;
        N(int t=0,int n=0):to(t),next(n) {}
    }edge[maxn<<1];
    int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
    void add(int x,int y){edge[++ct]=N(y,head[x]); head[x]=ct;}
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n); cnt=0; tot=0;
            memset(head,0,sizeof head); ct=0;
    //        for(int i=1;i<=n;i++)fa[i]=i;////
            for(int i=1;i<=n;i++)
                scanf("%d%d%d",&d1[i],&d2[i],&e[i]),tmp[++cnt]=d1[i],tmp[++cnt]=d2[i];
            sort(tmp+1,tmp+cnt+1);
            tot=unique(tmp+1,tmp+cnt+1)-tmp-1;//-1!!!
            for(int i=1;i<=tot;i++)fa[i]=i;//!!!
            for(int i=1,u,v;i<=n;i++)
            {
                u=lower_bound(tmp+1,tmp+tot+1,d1[i])-tmp;//不能 -tmp-1!
                v=lower_bound(tmp+1,tmp+tot+1,d2[i])-tmp;
                if(e[i]==1 && find(u)!=find(v)) fa[find(u)]=find(v);
                if(e[i]==0) add(u,v),add(v,u);
            }
            bool flag=0;
            for(int i=1;i<=tot;i++)
            {
                for(int j=head[i];j;j=edge[j].next)
                    if(find(i)==find(edge[j].to)){flag=1; break;}
                if(flag)break;
            }
            if(flag)printf("NO
    ");
            else printf("YES
    ");
        }
        return 0;
    }
  • 相关阅读:
    面向过程,面向对象三大特性
    JDBC连接数据库
    java线程
    ssm
    com组件方面的书籍
    剑雨
    JavaScript为元素动态添加事件之(attachEvent||addEventListener)
    Opacity多浏览器透明度兼容处理
    通过U盘安装Windows 7
    蜀门Online 简单打怪脚本(vbs)
  • 原文地址:https://www.cnblogs.com/Zinn/p/9218949.html
Copyright © 2011-2022 走看看