首页 > 其他 > 详细

HDU1272 小希的迷宫 (并查集)

时间:2014-07-29 17:47:32      阅读:356      评论:0      收藏:0      [点我收藏+]

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

注意问题:

1、不能成环,即每次输入的两个数的根节点不能相同;

2、只有一个迷宫,即根节点数目唯一;

3、注意当只输入“0 0” 时要输出"Yes";

4、状态压缩用递归回栈溢出。

参考代码:

#include<stdio.h>
int fa[100001];
int find(int u)
{
    int s;
    s=u;
     while (fa[u]!=u)u=fa[u];
     while (fa[s]!=s){fa[s]=u;s=fa[s];}
      return u;
}
int main()
{
    int u,v,x,y,n,m,i,k;
    bool bo;
    scanf("%d%d",&u,&v);
    while (u!=-1)
    {
       if (u!=0)
         {
          for (i=1;i<=100000;i++) fa[i]=i;
           bo=true;
             while (u)
               {
                 x=find(u);y=find(v);
                if (x==y) {bo=false;}
                else fa[x]=y;
                scanf("%d%d",&u,&v);
               }
            if (!bo) printf("No\n");
              int j;
                if(bo)
                  {
                    for (i=1;i<=100000;i++)
                      if (find(i)!=i) break;
                    for (j=i+1;j<=100000;j++)
                      if ((find(j)!=j)&&(find(j)!=find(i)))
                        {
                           printf("No\n");
                             bo=false;
                           break;
                        }
                  }
            if (bo) printf("Yes\n");
        }
        else printf("Yes\n");
      scanf("%d%d",&u,&v);
    }
   return 0;
}<strong>
</strong>

同类题请参考:http://blog.csdn.net/yzj577/article/category/2432227


HDU1272 小希的迷宫 (并查集),布布扣,bubuko.com

HDU1272 小希的迷宫 (并查集)

原文:http://blog.csdn.net/yzj577/article/details/38273957

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!