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来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
随机推荐
- Spark Checkpoint 概述
有时候,Transformation 的 RDD 非常多或者具体 Transformation 产生的 RDD 本身计算特别复杂和耗时,此时我们必须考虑对计算结果数据进行持久化.与 persist 不 ...
- springBoot2.0 配置 mybatis+mybatisPlus+redis
一.Idea新建springBoot项目 next到完成,然后修改使用自己的maven 等待下载包 二.pom.xml文件 <?xml version="1.0" encod ...
- E 聪明的“KK”
Description 非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国不断变换和绚丽多彩的自然风光与城市风貌.展馆由五部分组成,馆内影院播放名为<一眨眼的瞬间>的 ...
- javascript中的Set和Map数据结构
Set数据结构 类似数组,所有的数据都是唯一的,没有重复值,它本身是一个构造函数 size 数据长度 add() 添加一个数据 delete() 删除一个数据 has() 查找一个数据 clear() ...
- Unittest组织用例的姿势
本文我们将会讲解Python Unittest 里组织用例的5种姿势. 环境准备: python 3.0以上 python requests库 小编的环境: python 3.6.4 一.TestLo ...
- Android中常用的几种加密
1.数字摘要 是指通过算法将长数据变为短数据,通常用来标识数据的唯一性,是否被修改,常用的加密算法有md5和sha1两种,如Android的App签名也是用的这两种算法. md5具有不可逆性,也可用来 ...
- A -- A. Quailty and Playing Cards 模拟 + 思考
http://www.ifrog.cc/acm/problem/1036?contest=1005&no=0 分类,考虑Q神出的是第一张或者是第二张,然后对手出那些牌来应付. Q神出第一张和第 ...
- ubuntu网络已禁用解决办法
1.首先停掉network-manager这个服务 sudo service network-manager stop 2.删除掉NetworkManager.state这个文件 sudo rm /v ...
- mac-profile
Mac 中定义与Linux一样的profile.d 首先Mac是没有profile.d的 在/etc/profile文件中添加 for sh in /etc/profile.d/*sh; do [ - ...
- Java 多线程的实现方法
package com.jckb; /**多线程实现的两种方法 * * @author gx * */ public class Test2 { public static void main(Str ...