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;
    }

  • 相关阅读:
    hdu 2196 树形dp
    codeforces 1A
    [日常摸鱼]bzoj1218[HNOI2003]激光炸弹-二维前缀
    [日常摸鱼]bzoj2724蒲公英-分块
    [日常摸鱼]关于离散化
    [OI笔记]后缀自动机
    [日常摸鱼]poj1509Glass Beads-SAM
    [日常摸鱼]bzoj1083[SCOI2005]繁忙的都市-最小生成树
    [日常摸鱼]bzoj2038[2009国家集训队]小Z的袜子-莫队算法
    [日常摸鱼]三分法
  • 原文地址:https://www.cnblogs.com/372465774y/p/2427410.html
Copyright © 2011-2022 走看看