题目链接:

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. (扫码二维码不显示)php微信扫码支付sdk不能用了

    解决方案: img标签中src 该为qrcode.php的绝对路径,如: 备注:微信端这个生成二维码的路径老是改,之前是http://paysdk.weixin.qq.com/example/qrco ...

  2. [hdu 1671] Phone List - Trie

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  3. java整理(二)

    1.this关键字.this表示属性,表示方法和当前对象. this.属性调用属性. 方法分为两类,构造方法和普通方法,他们的调用形式是不同的.调用本类的方法this.方法()(这是调用普通方法)如果 ...

  4. UVA307 Sticks

    题意:一组等长的木棒,将它们随机的砍掉,得到若干根小木棍, 每一节小棍的长度都不超过50个单位.然后想把这些木棍拼接起来,恢复到裁剪前的状态, 但忘记了初始时有多少木棒以及木棒的初始长度.计算木棒的可 ...

  5. 《算法竞赛进阶指南》1.4Hash

    137. 雪花雪花雪花 有N片雪花,每片雪花由六个角组成,每个角都有长度. 第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,-,ai,6. 因为雪花的形状是封闭的环形,所以从任何一 ...

  6. java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page

    出现这个错误,首先看配置mybatis-config.xml中的<plugins> <plugin interceptor="com.github.pagehelper.P ...

  7. jsp 文件上传操作

    文件上传 1:完成一个文件上传的功能 index.jsp 注意更换form表单的enctype enctype就是encodetype就是编码类型的意思. multipart/form-data是指表 ...

  8. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  9. CSS(一)清除浮动

    问题1:关于清除浮动 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  10. 1089 Insert or Merge(25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...