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来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
随机推荐
- Go:定时执行任务time.sleep和time.tick的优劣
golang 写循环执行的定时任务,常见的有以下三种实现方式:1.time.Sleep方法: for { time.Sleep(time.Second) fmt.Println("我在定时执 ...
- CodeForces 116C 【BFS】
思路: 求所有树的最大高度? 注意:所有树从树根开始? #include <bits/stdc++.h> using namespace std; typedef long long LL ...
- PJzhang:英国通信总部GCHQ开源产品-网络瑞士军刀CyberChef
猫宁!!! 参考链接:https://www.4hou.com/info/news/981.html 这个产品免费开源易用,如果称之为网络瑞士军刀,没什么异议. github地址:https://gi ...
- php数组处理函数
array_reverse()数组反向排序,$arr=array_reverse($arr)
- 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 ...
- 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 ...
- maven插件: shade, assembly
shade插件的作用: 通过版本的exclution无法解决jar冲突的问题, 解决方案是把依赖的包打到本model的jar中,打包的时候由mvn plugin自动修改代码中的依赖jar包名 relo ...
- jdbc 操作步骤详解
package com.itheima.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql ...
- 初学struts2-入门案列
1.所需类库 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactI ...
- 关于安卓visualizer的用法
看别人显示播放wav文件显示频谱写的代码都是断断续续的,在这里我贴了完整的代码,给有需要的人做参考,显示频谱还没有完成,不知道怎么弄,已经可以得到byte[] fft数据了,参考别人的写法也可以开方取 ...