zoukankan      html  css  js  c++  java
  • HDU1325

    并查集方法
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<map>
    #include<algorithm>
    #include<memory.h> 
    using namespace std;
    int fa[100010],ru[100010];
    int s[100010],cnt,num;
    int _fa(int v)
    {
        if(v==fa[v]) return v;
        fa[v]=_fa(fa[v]);
        return fa[v];
    }
    int main()
    {
        int a,b;
        int faa,fab; 
        int Case=0;
        bool flag=true;
        while(1){
            flag=true;//更新 
            memset(fa,0,sizeof(fa));
            memset(ru,0,sizeof(ru));
            cnt=0;
            while(scanf("%d%d",&a,&b)&&a&&b){
                if(a<0||b<0) return 0;
                 if(fa[a]==0) {fa[a]=a;s[++cnt]=a;}//离散化 
                 if(fa[b]==0) {fa[b]=b;s[++cnt]=b;}
                 faa=_fa(a);
                 fab=_fa(b);
                 ru[b]++;
                 if(ru[b]>1)  flag=false;
                 if(faa==fab) flag=false;
                 fa[fab]=faa;
               }            
                faa=_fa(s[1]);//判断是否联通 
                for(int i=2;i<=cnt;i++){
                    fab=_fa(s[i]);
                    if(faa!=fab){
                        flag=false;
                        break;
                    }
                }
                if(flag) printf("Case %d is a tree.
    ",++Case);
                else printf("Case %d is not a tree.
    ",++Case);
        }
        return 0;
    }
    边顶关系
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int f[100005];
    int main()
    {
        int a,b,flag,i,j;
        int t=1;
        while(1)
        {
            j=0;
            i=0;
            flag=0;
            memset(f,0,sizeof(f));
            while(scanf("%d%d",&a,&b)&&a&&b)
            {
                 if(a<0||b<0)
                    return 0;
                if(f[b]-1==1)
                    flag=1;
                if(f[a]==0)
                    j++;
                if(f[b]==0)
                    j++;
                f[a]=1;f[b]=2;i++;
            }
            if(flag==0&&j==i+1)
                printf("Case %d is a tree.
    ",t++);
                else printf("Case %d is not a tree.
    ",t++);
        }
    }

     

  • 相关阅读:
    做了点数论的题
    2016日报总结
    2016在H3C
    2017春节小目标
    2016项目小总结
    明明的随机数
    计算字符个数
    字符串最后一个单词的长度
    字符串分隔
    考研回忆
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7632862.html
Copyright © 2011-2022 走看看