zoukankan      html  css  js  c++  java
  • 并查集Is It A Tree?hdu 1325

    题目连接http://acm.hdu.edu.cn/showproblem.php?pid=1325

    http://poj.org/problem?id=1308

    在POJ上WA了,看来还是有问题的呀

    #include <iostream>
    #include <cstdio>
    #include <string.h>
    using namespace std;
    int
    hr[100003],hs[100003];
    int
    mer[100003];
    int
    main()
    {

       // freopen("in.txt","r",stdin);
        int n,m,i,flag,t=1,cn;

        while
    (scanf("%d%d",&n,&m))
        {

             if
    (n<0&&m<0)
               break
    ;
             if
    (n==0&&m==0)
               {
    printf("Case %d is a tree.\n",t++);continue;}
             flag=1;cn=0;
            memset(hr,0,sizeof(hr));
            memset(hs,0,sizeof(hs));
            memset(mer,0,sizeof(mer));
             if
    (hr[n]==0)
                cn++;
             if
    (hr[m]==0)
               cn++;
                hr[n]=1;
                hr[m]=1;
                hs[m]++;
                mer[m]=n;
                cn--;
            while
    (scanf("%d%d",&n,&m),n||m)
            {

                if
    (flag==0)
                  continue
    ;
             if
    (hr[n]==0)
                cn++;
             if
    (hr[m]==0)
               cn++;
                hr[n]=1;
                hr[m]=1;
                hs[m]++;
                if
    (hs[m]>1)
                  {
    flag=0;continue;}
                while
    (mer[n]>0)
                  n=mer[n];
                while
    (mer[m]>0)
                  m=mer[m];
                if
    (m==n)
                  {
    flag=0;continue;}
                mer[m]=n;
                cn--;
            }

            if
    (flag&&cn!=1)
              flag=0;
              if
    (flag)
                  printf("Case %d is a tree.\n",t++);
              else

                  printf("Case %d is not a tree.\n",t++);
        }

        return
    0;
    }

    //去POJ看了下,发现自己漏掉了一种情况(其实有想过这种情况,就是自己以为不会出现,唉)

    #include <iostream>
    #include <cstdio>
    #include <string.h>
    using namespace std;
    int hr[100003],hs[100003];
    int mer[100003];
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n,m,i,flag,t=1,cn;

        while(scanf("%d%d",&n,&m))
        {
             if(n<0&&m<0)
               break;
             if(n==0&&m==0)
               {printf("Case %d is a tree.\n",t++);continue;}
             flag=1;cn=0;
            memset(hr,0,sizeof(hr));
            memset(hs,0,sizeof(hs));
            memset(mer,0,sizeof(mer));
             if(hr[n]==0)
                cn++;
             if(hr[m]==0)
               cn++;
             if(m==n)//开始少了这一步、、、树不能自己指向自己呀
               flag=0;
                hr[n]=1;
                hr[m]=1;
                hs[m]++;
                mer[m]=n;
                cn--;
            while(scanf("%d%d",&n,&m),n||m)
            {
                if(flag==0)
                  continue;
             if(hr[n]==0)
                cn++;
             if(hr[m]==0)
               cn++;
                hr[n]=1;
                hr[m]=1;
                hs[m]++;
                if(hs[m]>1||m==n)//还有这里,说明hduOJ测试数据还是不怎么强的
                  { flag=0;continue;}
                while(mer[n]>0)
                  n=mer[n];
                while(mer[m]>0)
                  m=mer[m];
                if(m==n)
                  { flag=0;continue;}
                mer[m]=n;
                cn--;
            }
            if(flag&&cn!=1)
              flag=0;
              if(flag)
                  printf("Case %d is a tree.\n",t++);
              else
                  printf("Case %d is not a tree.\n",t++);
        }
        return 0;
    }

  • 相关阅读:
    在当前页面中弹出新的标签页
    宝塔面板使用PM2命令提示Command Not Found解决方案
    python安装一些第三包的办法
    使用git时将部分文件写入.gitignore依旧上传的问题
    iOS APP上架各种被拒"悲剧"2021-6-29更新
    openststry(二)
    openresty
    kubectl explain IngressClass
    kubernetes edit Error
    微服务架构中的NGINX
  • 原文地址:https://www.cnblogs.com/372465774y/p/2427410.html
Copyright © 2011-2022 走看看