hdu-1272 小希的迷宫---并查集或者DFS
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1272
题目大意:

整个文件以两个-1结尾。
5 6 0 0
7 4 7 8 7 6 0 0
5 3 5 6 5 2 0 0
#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的更多相关文章
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
- HDU 1272 小希的迷宫 并查集
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质
小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...
- (step5.1.6)hdu 1272(小希的迷宫——并查集)
题目大意:输入一系列的点,判断这些点组成的图符不符合小希的思路(无环.连通) 解题思路: 1)如果两个节点的根节点相同,那么在这两个节点之间添加1条边以后,这个图肯定有环路. 2)孤立节点:被使用过& ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- HDU 1272小希的迷宫(裸并查集,要判断是否构成环,是否是连通图)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1272 小希的迷宫(并查集+最小生成树+队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1272 小希的迷宫 (并查集)
小希的迷宫 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/L Description 我们的小伙伴Bingo身为大二学长,他乐于 ...
- HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
随机推荐
- (扫码二维码不显示)php微信扫码支付sdk不能用了
解决方案: img标签中src 该为qrcode.php的绝对路径,如: 备注:微信端这个生成二维码的路径老是改,之前是http://paysdk.weixin.qq.com/example/qrco ...
- [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 ...
- java整理(二)
1.this关键字.this表示属性,表示方法和当前对象. this.属性调用属性. 方法分为两类,构造方法和普通方法,他们的调用形式是不同的.调用本类的方法this.方法()(这是调用普通方法)如果 ...
- UVA307 Sticks
题意:一组等长的木棒,将它们随机的砍掉,得到若干根小木棍, 每一节小棍的长度都不超过50个单位.然后想把这些木棍拼接起来,恢复到裁剪前的状态, 但忘记了初始时有多少木棒以及木棒的初始长度.计算木棒的可 ...
- 《算法竞赛进阶指南》1.4Hash
137. 雪花雪花雪花 有N片雪花,每片雪花由六个角组成,每个角都有长度. 第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,-,ai,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 ...
- jsp 文件上传操作
文件上传 1:完成一个文件上传的功能 index.jsp 注意更换form表单的enctype enctype就是encodetype就是编码类型的意思. multipart/form-data是指表 ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- CSS(一)清除浮动
问题1:关于清除浮动 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 1089 Insert or Merge(25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...