zoukankan      html  css  js  c++  java
  • YbtOj例题: 并查集2 程序自动分析

    这道题不用写手写哈希,可以直接用STL里的map。但是NOI C艹不能用unordered_map(效率比较高)  所以我们需要在能省时间的地方尽量地省时间

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e6+5;
    int T,n,tot;
    int fa[N];
    map <int,int> S;
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') 
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x;
    }
    struct node{
        int a,b,e;
    }num[N];
    int get(int x)
    {
        if(S.count(x)==0) S[x]=++tot;
        return S[x]; 
    }
    int find(int x)
    {
        if(x==fa[x]) return x;
        return fa[x]=find(fa[x]);
    }
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            tot=0;S.clear();
            for(int i=1;i<=n*2;i++) fa[i]=i;
            for(int i=1;i<=n;i++) 
            {
                int a=read(),b=read(),e=read();
                num[i]={get(a),get(b),e};
            }
            bool flag=true;
            for(int i=1;i<=n;i++)
             if(num[i].e) 
             {
                 int A=find(num[i].a),B=find(num[i].b);
                 fa[A]=B;
             }
            for(int i=1;i<=n;i++)
             if(!num[i].e)
            {
                int A=find(num[i].a),B=find(num[i].b);
                if(A==B) 
                {
                    flag=false;
                    break;
                }
            }
            if(flag) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    在Win8下无法打开 hlp 帮助文件的问题
    ftp 终端命令
    为 Macbook 增加锁屏热键技巧
    苹果系统直接读写 ntfs 磁盘
    div 绝对布局居中
    2015-1-11
    unable to load default svn client
    ubuntu eclipse 安装svn
    centos mysq table is read only
    centos ssh 乱码
  • 原文地址:https://www.cnblogs.com/smartljy/p/13490869.html
Copyright © 2011-2022 走看看