zoukankan      html  css  js  c++  java
  • 小希的迷宫(HDU 1272 并查集判断生成树)

    小希的迷宫

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 38870    Accepted Submission(s): 11921


    Problem Description
    上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。 
     
    Input
    输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。 
    整个文件以两个-1结尾。
     
    Output
    对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出"Yes",否则输出"No"。
     
    Sample Input
     
     

    6 8 5 3 5 2 6 4
    5 6 0 0

    8 1 7 3 6 2 8 9 7 5
    7 4 7 8 7 6 0 0

    3 8 6 8 6 4
    5 3 5 6 5 2 0 0

    -1 -1

    Sample Output
    Yes
    Yes
    No
     

     当存在环或者连通分量不为1时则不满足,没想到的是一开始就可以输入0 0,此时就要输出Yes;然后脑残把continue写成了break WA了好几次

    在进行路径压缩时如果用return per[x]=find(per[x])会出现栈溢出,但#pragma comment(linker, "/STACK:1024000000,1024000000")可手动扩展。

    此代码使用的循环压缩路径
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <set>
     5 using namespace std;
     6 #define Max 100000+10
     7 int per[Max];
     8 int num[Max*2];
     9 int flag;
    10 void init()
    11 {
    12     for(int i=0;i<Max;i++)
    13         per[i]=i;
    14     return;
    15 }
    16 int find(int a)
    17 {
    18     if(a==per[a])
    19         return a;
    20     int root=a,t=a,tmp;
    21     while(root!=per[root])        //找到根节点
    22         root=per[root];
    23     while(per[t]!=root)
    24     {
    25         tmp=per[t];
    26         per[t]=root;
    27         t=tmp;
    28     }
    29     return root;
    30 }
    31 void unite(int a,int b)
    32 {
    33     a=find(a);
    34     b=find(b);
    35     if(a!=b)
    36         per[a]=b;
    37     else
    38         flag=1;        //判断成环操作
    39 }    
    40 int main()
    41 {
    42     int a,b;
    43     int i,j;
    44     freopen("in.txt","r",stdin);
    45     while(1)
    46     {
    47         init();
    48         set<int> s;
    49         flag=0;
    50         i=0;
    51         int k=0;
    52         scanf("%d%d",&a,&b);
    53         if(a==-1&&b==-1)
    54             break;
    55         if(a==0&&b==0)
    56         {
    57             printf("Yes
    ");
    58             continue;
    59         }
    60         unite(a,b);
    61         s.insert(a);
    62         s.insert(b);
    63         while(scanf("%d%d",&a,&b))
    64         {
    65             if(a==0&&b==0)
    66                 break;
    67             unite(a,b);
    68             s.insert(a);
    69             s.insert(b);
    70         }
    71         if(flag==1)
    72         {
    73             printf("No
    ");
    74             continue;
    75         }
    76         set<int> :: iterator it=s.begin();
    77         for(;it!=s.end();it++)
    78         {
    79             if(per[*it]==*it)
    80                 flag--;
    81         }
    82         if(flag==-1)
    83             printf("Yes
    ");
    84         else
    85             printf("No
    ");
    86     }
    87 }
  • 相关阅读:
    三、linux系统管理
    二、基本命令
    一、基本环境
    mysql-day4
    mysql-day3
    mysql-day2
    mysql-day1
    3、线性表的链式存储结构
    2、线性表之顺序表
    1、时间复杂度
  • 原文地址:https://www.cnblogs.com/a1225234/p/5180511.html
Copyright © 2011-2022 走看看