hdu1272 小希的迷宫【并查集】
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
简单的并查集
先在输入的时候判断一下是不是已经在一个集合里了 如果是就是NO
全部输入完成之后还要判断一下集合数是不是多于1因为题目里是 有且仅有【这个没有判断WA了一次】
所以输入的时候需要标记各个节点 因为输入没有给节点数 所以不能通过每个节点都访问过来判断
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 100005;
int t, n, m;
int ran[maxn], parent[maxn];
bool vis[maxn];
void init()
{
for(int i = 0; i < maxn; i++){
ran[i] = 0;
parent[i] = i;
vis[i] = false;
}
}
int fin(int x)
{
if(x == parent[x]) return x;
int t = parent[x];
parent[x] = fin(parent[x]);
return parent[x];
}
void mer(int x, int y)
{
int tx = fin(x);
int ty = fin(y);
if(tx != ty){
parent[tx] = ty;
ran[tx] = ran[ty];
}
}
int main()
{
int a, b;
bool flag = false;
while(1){
bool can = true;
init();
while(scanf("%d%d", &a, &b) && a != 0 && b != 0){
if(a == -1 && b == -1){
flag = true;
break;
}
int fa = fin(a);
int fb = fin(b);
if(fa == fb){
can = false;
}
else{
mer(a, b);
vis[a] = vis[b] = true;
}
}
if(flag)break;
int cnt = 0;
for(int i = 0; i < maxn; i++){
if(vis[i] && parent[i] == i)
cnt++;
}
if(cnt > 1) can = false;
if(can){
printf("Yes\n");
}
else{
printf("No\n");
}
}
return 0;
}
hdu1272 小希的迷宫【并查集】的更多相关文章
- hdu1272 小希的迷宫(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目: 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU1272小希的迷宫–并查集
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...
- hdu-1272 小希的迷宫---并查集或者DFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目大意: Problem Description 上次Gardon的迷宫城堡小希玩了很久(见 ...
- HDU-1272小希的迷宫,并查集?其实不用并查集;
小希的迷宫 ...
- HDU1272 小希的迷宫 并查集
参考网址:http://blog.sina.com.cn/s/blog_6827ac4a0100nyjy.html 解题思路: 由于这里出现的数字不一定连续的数字都会出现,所以设一个mark来标记数字 ...
- hdu1272小希的迷宫(并查集判断回路和是否连通)
传送门 迷宫中不能有回路,还要连通 如果最后集合数是一个那就是连通,否则不联通 要合并的两个顶点在相同集合内,表示出现了回路 输入时注意一下 #include<bits/stdc++.h> ...
- HDU 1272 小希的迷宫 并查集
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 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 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
- TZOJ 2648 小希的迷宫(并查集)
描述 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道 ...
随机推荐
- [Bayes] What is Sampling
Ref: http://blog.csdn.net/xianlingmao/article/details/7768833 通常,我们会遇到很多问题无法用分析的方法来求得精确解,例如由于式子特别,真的 ...
- ios的AutoresizingMask【转】
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. enum { UIViewAutoresi ...
- 5 -- Hibernate的基本用法 --4 4 数据库方言
Hibernate底层依然使用SQL语句来执行数据库操作,虽然所有关系数据库都支持使用标准SQL语句,但所有数据库都对标准SQL进行了一些扩展,所以在语法细节上存在一些差异.因此,Hibernate需 ...
- WSGI简介
当我们实现一个Web应用(application)的时候,通常不会考虑如何接受HTTP请求.解析HTTP请求.发送HTTP响应等等,我们只关心处理逻辑,而不用去关心HTTP规范的细节. 之所以有这层透 ...
- CouchDB 未授权访问漏洞
0x00 CouchDB安装 CouchDB官方网站:http://couchdb.apache.org/ 一路Next直到完成安装,打开浏览器并访问以下链接:http://127.0.0.1:598 ...
- 使用 urllib 发送请求
urllib.request.urlopen(url, data=None, timeout=n) 用于发送HTTP请求并得到响应内容 In []: import urllib.request In ...
- lua总则
lua官方英文文档:http://www.lua.org/manual/5.2/ lua中国开发者网址:http://bbs.luaer.cn/ <lua程序设计(第二版)>(闭合函数和闭 ...
- windowsError错误码详解
WindowsError的错误代码详解 0操作成功完成. 1功能错误. 2系统找不到指定的文件. 3系统找不到指定的路径. 4系统无法打开文件. 5拒绝访问. 6句柄无效. 7存储控制块被损坏. 8存 ...
- linux C 调用shell程序执行
#include<stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h ...
- 执行Batch批处理遇到的问题
1.务必关掉自动提交 增强执行效率 conn.setAutoCommit(false); 2.executeBatch失效问题 <1>务必将语句pstmt = conn.prepareSt ...