• hdu 1257 小希的迷宫 并查集


    小希的迷宫

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=1272

    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

    HINT

    题意

    题解:

    用并查集来判断是否都在一个集合里面,是否成环,就好了

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)  
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int fa[maxn];   
    bool mark[maxn];  
      
    int fi(int x)    
    {    
        int t=x;
        while(fa[t]!=t)
            t=fa[t];
        return t;    
    }    
      
    bool merge(int x,int y)    
    {    
        int fx=fi(x),fy=fi(y);    
        if(fx!=fy)
        {  
            fa[fx]=fy;  
            return true;  
        }  
        return false;  
    }    
      
    int main()  
    {  
        //test;
        int a,b,i,flag,cnt;  
        while(scanf("%d%d",&a,&b)&&(a!=-1 && b!=-1))  
        {  
              
            flag=1;cnt=0;  
             if(a==0 && b==0)  
            {  
                printf("Yes
    ");  
                continue;  
            }  
      
            for(i=0;i<100010;i++)  
            {  
                fa[i]=i;mark[i]=0;  
            }  
            while(a||b)  
            {  
                mark[a]=1;mark[b]=1;  
               if(merge(a,b)==false)  
                  flag=0;  
               scanf("%d%d",&a,&b);  
            }  
              
            if(flag==0)  
                printf("No
    ");  
            else  
            {  
                for(i=0;i<100010;i++) 
                if(mark[i] && fa[i]==i)  
                    cnt++;
                if(cnt==1)  
                    printf("Yes
    ");  
                else  
                    printf("No
    ");  
            }  
        }  
        return 0;  
    }  
  • 相关阅读:
    POJ 3187 Backward Digit Sums
    POJ 3669 Meteor Shower BFS
    POJ 1979 Red and Black DFS
    不想切题、
    企业管理信息系统成功实施的四个阶段
    linux下memcached的安装
    Apache开启伪静态示例
    网页设计基础知识
    memcached安装说明
    伪静态.htaccess的练习
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4547393.html
Copyright © 2020-2023  润新知