题目链接:

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

题目大意:

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
 
解题思路:
其实就是判断无向图是不是树。
可以用dfs判断连通性,只要所有点连通并且点数=边数+1,就满足条件
或者用并查集判断有没有环,只要无环并且点数=边数+1,也满足条件
注意:只输入0 0也满足条件,为空树 
 
DFS版:
 #include<bits/stdc++.h>
using namespace std;
int T, n, m;
const int maxn = + ;
vector<int>G[maxn];
bool vis[maxn];
void init()
{
for(int i = ; i < maxn; i++)G[i].clear(), vis[i] = ;
}
void dfs(int u)
{
if(vis[u])return;
//cout<<u<<endl;
vis[u] = ;
for(int i = ; i < G[u].size(); i++)
{
dfs(G[u][i]);
}
}
int main()
{
int x, y, c;
while(cin >> x >> y && (x + y >= ))
{
m = ;
n = ;
set<int>s;
s.insert(x);
s.insert(y);
if(x == && y == )
{
cout<<"Yes"<<endl;
continue;
}
init();
G[x].push_back(y);
G[y].push_back(x);
while(cin >> x >> y && (x + y))
{
s.insert(x);
s.insert(y);
G[x].push_back(y);
G[y].push_back(x);
m++;
c = x;
}
dfs(c);
for(set<int>::iterator it = s.begin(); it != s.end(); ++it)
{
if(vis[*it])n++;
}
if(n == m + )cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return ;
}

并查集版:

 #include<bits/stdc++.h>
using namespace std;
int T, n, m;
const int maxn = + ;
int cases;
int p[maxn];
void init()
{
for(int i = ; i < maxn; i++)p[i] = i;
}
int Find(int x)
{
return x == p[x] ? x : p[x] = Find(p[x]);
}
int main()
{
int x, y, c;
while(cin >> x >> y && (x + y >= ))
{
m = ;
n = ;
set<int>s;
s.insert(x);
s.insert(y);
if(x == && y == )
{
cout<<"Yes"<<endl;
continue;
}
init();
int flag = ;
p[y] = x;
if(x == y)flag = ;
while(cin >> x >> y && (x + y))
{
s.insert(x);
s.insert(y);
m++;
if(Find(x) == Find(y))//判断是否存在环
{
flag = ;
}
p[y] = Find(x);
}
if(s.size() == m + && flag)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return ;
}

hdu-1272 小希的迷宫---并查集或者DFS的更多相关文章

  1. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

  2. HDU 1272 小希的迷宫 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

  4. (step5.1.6)hdu 1272(小希的迷宫——并查集)

    题目大意:输入一系列的点,判断这些点组成的图符不符合小希的思路(无环.连通) 解题思路: 1)如果两个节点的根节点相同,那么在这两个节点之间添加1条边以后,这个图肯定有环路. 2)孤立节点:被使用过& ...

  5. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  6. HDU 1272小希的迷宫(裸并查集,要判断是否构成环,是否是连通图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. hdu 1272 小希的迷宫(并查集+最小生成树+队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)     ...

  8. HDU 1272 小希的迷宫 (并查集)

    小希的迷宫 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/L Description 我们的小伙伴Bingo身为大二学长,他乐于 ...

  9. HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏

    Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...

随机推荐

  1. Go:定时执行任务time.sleep和time.tick的优劣

    golang 写循环执行的定时任务,常见的有以下三种实现方式:1.time.Sleep方法: for { time.Sleep(time.Second) fmt.Println("我在定时执 ...

  2. CodeForces 116C 【BFS】

    思路: 求所有树的最大高度? 注意:所有树从树根开始? #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  3. PJzhang:英国通信总部GCHQ开源产品-网络瑞士军刀CyberChef

    猫宁!!! 参考链接:https://www.4hou.com/info/news/981.html 这个产品免费开源易用,如果称之为网络瑞士军刀,没什么异议. github地址:https://gi ...

  4. php数组处理函数

    array_reverse()数组反向排序,$arr=array_reverse($arr)

  5. 74th LeetCode Weekly Contest Valid Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  6. ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret = 2) from /usr/local/etc/php-fpm.conf at line WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf'

    Building from source is not easy if something is a bit different, and I had a hard time with some di ...

  7. maven插件: shade, assembly

    shade插件的作用: 通过版本的exclution无法解决jar冲突的问题, 解决方案是把依赖的包打到本model的jar中,打包的时候由mvn plugin自动修改代码中的依赖jar包名 relo ...

  8. jdbc 操作步骤详解

    package com.itheima.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql ...

  9. 初学struts2-入门案列

    1.所需类库 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactI ...

  10. 关于安卓visualizer的用法

    看别人显示播放wav文件显示频谱写的代码都是断断续续的,在这里我贴了完整的代码,给有需要的人做参考,显示频谱还没有完成,不知道怎么弄,已经可以得到byte[] fft数据了,参考别人的写法也可以开方取 ...