HDU1272-小希迷宫
题目链接:点击打开链接
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
思路:这道题看了半天没发现怎么做,思考了一会儿,直接拓扑判环,最后才知道那个是判定有向无环图的。可以用并查集判断无向图是否有环。即看两个点的祖先是否一样。!!!!!!!!但是有个坑点。(用题目中的最大值初始化)不要用自己写的稍微大的那个初始化!!!
AC代码:
#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
const int MAX = 100010;
const int INF = 0X3f3f3f;
int n, m;
int father[MAX];
bool vis[MAX];
void init() {//初始化!!!!!!!!一定注意 初始不要多。 我初始MAX大小 直接错
for(int i = 0; i <= 100000; i++) {
father[i] = i;
vis[i] = false;
}
}
int findfather(int x) {
int a = x;
while(x != father[x]) {
x = father[x];
}
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findfather(a);
int faB = findfather(b);
if(faA > faB)
father[faA] = faB;
else
father[faB] = faA;
}
int main() {
int u, v;
while(scanf("%d %d", &u, &v) != EOF) {
if(u == -1 && v == -1)
break;
// if(u == 0 && v == 0) {
// cout << "Yes" << endl;
// continue;
// }
init();
int flag = 0;
while(1) {
if(u == 0 && v == 0)
break;
if(findfather(u) == findfather(v))//如果相等就判1
flag = 1;
Union(u, v);
vis[u] = true, vis[v] = true;
scanf("%d %d", &u, &v);
}
if(flag == 1)
printf("No\n");
else {
int sum = 0;
for(int i = 0; i <= 100000; i++) {// 这里也用题目中的最大值。!!!
if(vis[i] == true && father[i] == i)
sum++;
}
if(sum > 1)
printf("No\n");
else
printf("Yes\n");
}
}
return 0;
}
HDU1272-小希迷宫的更多相关文章
- hdu1272 小希的迷宫
Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该 ...
- hdu1272 小希的迷宫(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目: 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU1272小希的迷宫–并查集
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...
- hdu1272 小希的迷宫【并查集】
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...
- HDU-1272 小希的迷宫 (并查集、判断图是否为树)
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- hdu-1272 小希的迷宫---并查集或者DFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目大意: Problem Description 上次Gardon的迷宫城堡小希玩了很久(见 ...
- HDU1272小希的迷宫
小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...
- HDU-1272小希的迷宫,并查集?其实不用并查集;
小希的迷宫 ...
- hdu1272小希的迷宫(并查集判断回路和是否连通)
传送门 迷宫中不能有回路,还要连通 如果最后集合数是一个那就是连通,否则不联通 要合并的两个顶点在相同集合内,表示出现了回路 输入时注意一下 #include<bits/stdc++.h> ...
- HDU1272 小希的迷宫 并查集
参考网址:http://blog.sina.com.cn/s/blog_6827ac4a0100nyjy.html 解题思路: 由于这里出现的数字不一定连续的数字都会出现,所以设一个mark来标记数字 ...
随机推荐
- python模块inspect.py
inspect模块用来检查对象的类型(函数,属性,类,抽象基类,方法,模块等等) 是一个封装好的非常有用的模块. ]) ]: cls = :]: content = ] = lines[].lstri ...
- codeforces 707B B. Bakery(水题)
题目链接: B. Bakery 题意: 是否存在一条连接特殊和不特殊的边,存在最小值是多少; 思路: 扫一遍所有边: AC代码: #include <iostream> #include ...
- codeforces 629D D. Babaei and Birthday Cake (线段树+dp)
D. Babaei and Birthday Cake time limit per test 2 seconds memory limit per test 256 megabytes input ...
- leetcode 1 Two Sum(查找)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- Mybatis generator配置文件及说明
项目采用sring mvc + mybatis 组合,这里简单介绍下mybatis的应用: 我的IDE是STS(Spring + Tool + Suite), 安装Mybatis Generator插 ...
- log4net调试
public delegate void LogReceivedEventHandler(object source, LogReceivedEventArgs e); public sealed c ...
- BZOJ4545: DQS的trie
BZOJ4545: DQS的trie https://lydsy.com/JudgeOnline/problem.php?id=4545 分析: 对trie用dfs建sam复杂度是\(O(n^2)\) ...
- ACM学习历程—POJ3565 Ants(最佳匹配KM算法)
Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees ...
- bzoj 1441: Min 裴蜀定理
题目: 给出\(n\)个数\((A_1, ... ,A_n)\)现求一组整数序列\((X_1, ... X_n)\)使得\(S=A_1*X_1+ ...+ A_n*X_n > 0\),且\(S\ ...
- bzoj 3232: 圈地游戏 01分数规划
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3232 题解: 首先我们看到这道题让我们最优化一个分式. 所以我们应该自然而然地想到01分 ...