题目链接:点击打开链接

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;
}

最新文章

  1. android xml 布局错误
  2. 转:MyBean的安装
  3. bash
  4. keepalived+mysql主主
  5. DiskFileItemFactory类---分析及运用
  6. Windows打印体系结构之Print Spooler概念与架构
  7. JavaScript的核心
  8. apache代理转发
  9. JAVA NIO 主要概念
  10. ##2.基础服务(SQl,RabbitMQ)-- openstack pike
  11. button点击切换,获取按钮ID
  12. 字符串a-b
  13. 4、Flutter 采坑记录篇二_依赖库不兼容
  14. [20171205]uniq命令的输入输出.txt
  15. innosetup 安装前、卸载前判断是否有进程正在运行&lt;转&gt;
  16. 18.2 不同用户 不同颜色光标 redis
  17. Chart控件使用初步
  18. GitHub已将持续集成服务器Janky开源
  19. [翻译] PPiAwesomeButton
  20. SimpleDateFormat实现String与Date之间的转换

热门文章

  1. VS2019(Windows+Mac)编辑文件模板
  2. Struts2 - action通配符映射
  3. 省选/NOI刷题Day1
  4. BZOJ4756:[USACO2017JAN]Promotion Counting
  5. 洛谷【P1601】A+B Problem(高精)
  6. Linux 系统通过 Squid 配置实现代理上网
  7. HDOJ1728(限制转弯的迷宫问题)
  8. canvas线条笔帽及连接
  9. 查看,检查,修复pg的命令
  10. TS学习之枚举