hdu 1272(并查集)

小希的迷宫

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

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

Author

Gardon

Source

做这题需要注意的几点:

1.空树也是树

2.输入的点不是连续的

注意了这两点只要离散化(或者标记)然后判断是否有环和连通分量的个数就OK

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = 100005;
struct Edge{
    int s,e;
}edge[N];
int father[N];
int Hash[N]; ///因为输入的树的编号不连续,离散化一下
int _find(int x){
    if(x==father[x]) return x;
    return _find(father[x]);
}
int Union(int a,int b){
    int x = _find(a);
    int y = _find(b);
    if(x==y) return 0;
    father[x]=y;
    return 1;
}
int main()
{
    int x,y;
    while(scanf("%d%d",&x,&y)!=EOF){
        if(x==-1&&y==-1) break;
        if(x==0&&y==0) { ///空树也是树
            printf("Yes\n"); continue;
        }
        for(int i=1;i<N;i++) father[i]=i;
        memset(Hash,-1,sizeof(Hash));
        int k=1,m=1;
        if(Hash[x]==-1) Hash[x]=k++;
        if(Hash[y]==-1) Hash[y]=k++;
        edge[m].s = Hash[x];
        edge[m++].e = Hash[y];
        while(scanf("%d%d",&x,&y)!=EOF){
            if(x==0&&y==0) break;
            if(Hash[x]==-1) Hash[x]=k++;
            if(Hash[y]==-1) Hash[y]=k++;
            edge[m].s = Hash[x];
            edge[m++].e = Hash[y];
        }
        bool flag = true;
        for(int i=1;i<m;i++){
            if(Union(edge[i].s,edge[i].e)==0){
                flag = false;
                break;
            }
        }
        int ans = 0;
        for(int i=1;i<k;i++){
            if(father[i]==i) ans++;
        }
        if(ans==1&&flag) printf("Yes\n");
        else printf("No\n");
    }
}
时间: 2024-08-25 07:24:12

hdu 1272(并查集)的相关文章

杭电1272 并查集找环+判断连通

杭电1272 并查集找环+判断连通 E - E Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1272 Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b

HDU 3938 并查集

求小于L的路径点对数(路上的最大值),按权值排序,从小到大并查集建图,有点kruskal的意思. /** @Date : 2017-09-22 17:30:11 * @FileName: HDU 3938 并查集 离线.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h>