• poj1308【并查集】


    = =、如果输入的两个数相等。就不是一颗树啊,不能自己指向自己。
    水。(瞎开的数组)。

    //#include <bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    const double eps=1e-6;
    const double pi=acos(-1.0);
    const int mod=998244353;
    const int INF=0x3f3f3f3f;
    
    const int N=1e5+8;
    
    bool vis[N];
    int pre[N];
    
    int Find(int x)
    {
        int r=x;
        while(r!=pre[r]){
            r=pre[r];
        }
        int i=x,j;
        while(pre[i]!=r)
        {
            j=pre[i];
            pre[i]=r;
            i=j;
        }
        return r;
    }
    
    int main()
    {
        int aa,bb;
        int a,b,flag,flat;
        int t;
        int tmax,tmin;
        t=1;
        while(1){
            scanf("%d%d",&a,&b);
            if(a==-1&&b==-1){
                break;
            }
            if(!a&&!b){
                printf("Case %d is a tree.
    ",t++);
                continue;
            }
            for(int i=1;i<=5000;i++)
                pre[i]=i;
            memset(vis,0,sizeof(vis));
            flag=flat=0;
            tmin=INF;tmax=-1;
            if(a==b){
                flag=1;
            }
            else{            
                vis[a]=vis[b]=1;
                aa=Find(a);
                bb=Find(b);
                tmin=min(tmin,min(a,b));
                tmax=max(tmax,max(a,b));
                if(aa!=bb){
                    pre[aa]=bb;
                }
            }
            while(scanf("%d%d",&a,&b))
            {
                if(!a&&!b) break;
                if(flag)
                    continue;
                if(a==b) flag=1;
                else{
    
                    tmin=min(tmin,min(a,b));
                    tmax=max(tmax,max(a,b));
                    aa=Find(a);
                    bb=Find(b);
                    vis[a]=vis[b]=1;
                    if(aa!=bb){
                        pre[aa]=bb;
                    }
                    else{
                        flag=1;
                    }
                }
            }
            if(!flag){
                int xx=Find(tmin);
                for(int i=tmin;i<=tmax;i++){
                    if(vis[i]&&xx!=Find(i)){
                        flat=1;
                        break;
                    }
                }
                if(flat){
                    printf("Case %d is not a tree.
    ",t++);
                }
                else
                    printf("Case %d is a tree.
    ",t++);
            }
            else{
                printf("Case %d is not a tree.
    ",t++);
            }
        }
    }
    
    
    
  • 相关阅读:
    adjacent_diffenerce
    数值算法速食食谱
    accumulate
    平面分割
    进制转换
    奖学金
    谁考了第k名
    奇数单增序列
    病人排序
    灯的开关状态
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934426.html
Copyright © 2020-2023  润新知