hdu1272 小希的迷宫 基础并查集
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std; const int M = ;
int a, b;
int father[M]; //记录父节点
bool circle; //判断是否存在环
bool visit[M]; //用来记录顶点数
int edgenum, vnum; //分别表示边数,顶点数 void initial()
{
for (int i = ; i<M; i++)
father[i] = i, visit[i] = false;
circle = false;
edgenum = vnum = ;
} //(查)
int find(int x)
{
return x == father[x] ? x : father[x] = find(father[x]); //找祖先节点 + 路径压缩
} //(并)
void merge(int a, int b)
{
if (a == b)
circle = true;
int x, y;
x = find(a);
y = find(b);
if (x != y)
{
father[x] = y;
edgenum++; //引出一条边
}
else
circle = true; //x==y,说明他们是同一个祖先,一旦连通便与祖先3者成环
} int main()
{
while (true)
{
initial();
scanf("%d%d", &a, &b);
if (a == && b == )
{ //为空树
printf("Yes\n");
continue;
}
if (a == - && b == -)
break;
visit[a] = true;
visit[b] = true;
merge(a, b);
while (true)
{
scanf("%d%d", &a, &b);
if (a == && b == )
break;
visit[a] = true;
visit[b] = true;
merge(a, b);
}
for (int i = ; i < M; i++)
{
if (visit[i])
vnum++;
}
if (!circle && edgenum + == vnum)
printf("Yes\n");
else
printf("No\n");
}
return ;
}
hdu1272 小希的迷宫 基础并查集的更多相关文章
- hdu1272 小希的迷宫【并查集】
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...
- HDU-1272 小希的迷宫 (并查集、判断图是否为树)
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- 【HDU1272】小希的迷宫(并查集基础题)
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...
- HDU1272:小希的迷宫(并查集)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 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来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- ACM学习历程—HDU 1272 小希的迷宫(并查集)
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- HDU——1272小希的迷宫(并查集+拓扑排序)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU 1272: 小希的迷宫(并查集)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
随机推荐
- 2015-03-12---外观模式,建造者模式(附代码),观察者模式(附代码),boost库应用
今天白天主要看了boost库的应用,主要是经常使用的一些库,array,bind,function,regex,thread,unordered,ref,smartpointers库,晚上看了看设计模 ...
- Python - colour-science库
http://nbviewer.jupyter.org/github/colour-science/colour-ipython/blob/master/notebooks/colour.ipynb# ...
- 修改flash builder注释里的@author
在flash builder里,按Ctrl+Shift+D可以很方便在添加AsDoc注释.可是有些生成的@author是系统的用户名(如:administor),怎么修改这个为自己的名字呢? Step ...
- Windows7和Ubuntu12.04无法选择系统
Windos7 旗舰版 Ubuntu12.04LTS 64位版本号 硬件挂载两个硬盘 SSD+机械 Windows7和Ubuntu12.04都装在SSD上.眼下先装好了Windows7,打算装Ubun ...
- HDOJ1006
#include <cstdio>#include <algorithm>using namespace std;const double UB=43200;const dou ...
- Html.Partial
老革命永远都在遇上各种似是而非的老问题. 这次,是这个Html.Partial,分部页. Html.Partial与Html.Action有啥区别呢?区别就是,Html.Partial只有一个视图,而 ...
- hadoop rsync
1 rsync用来同步配置文件 rsync用来同步两个文件夹,它拷贝的是二者的差异,因此速度很快.在hadoop脚本中,rsync用来同步配置文件. 2 HADOOP_SLAVE_SLEEP的用途 大 ...
- java中Math常用方法
public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//计算平方根 *Math.cbrt()//计算立 ...
- codeforces 691D D. Swaps in Permutation(dfs)
题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...
- 【HDU 1754】 I Hate It
[题目链接] 点击打开链接 [算法] 树状数组的最值查询 详见这篇文章 : https://blog.csdn.net/u010598215/article/details/48206959 [代码] ...