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 ...
随机推荐
- mac系统下为emacs设置中文字体,解决乱码问题
近期换了个系统,如今用mac系统. 当打开emacs后,中文支持的不是非常好.有的地方能显示.在.el文件的凝视里显示为口口口口口口口口这种框.例如以下图所看到的 找了半天.是由于中文字体的问题.仅仅 ...
- Python开发【迭代器】
1.迭代器 1.1.迭代器创建:指定数据创建迭代器(使用iter()和next() ) x = [1, 2, 3] #定义一个列表:<class 'list'> y = iter(x) # ...
- HDU 3249 Test for job (有向无环图上的最长路,DP)
解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...
- huawei校招测试题
三道题两小时. 第一题,圈住所有点的长方形,很简单略过. 第二题:奇偶排序. 奇偶排序 描述: 输入若干(不超过1000个)非负整数数字,请先取出为奇数的数字按从大到小排序,再取出偶数从小到大进行排序 ...
- HDOJ1004 数组还要自己初始化
#include <iostream> #include <stdio.h> #include "string.h"using namespace std; ...
- 【bzoj4653】[Noi2016]区间
离散化+线段树 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstri ...
- Flume-ng-sdk源码分析
Flume 实战(2)--Flume-ng-sdk源码分析 - mumuxinfei - 博客园 http://www.cnblogs.com/mumuxinfei/p/3823266.html
- 使用Android Studio查看API文档
在使用Android Studio编码时,若要查看某个类或函数的释义, 只需将光标移动至要查看释义的代码处,然后按下Ctrl+Q,便会弹出文档描述. 然而,有时候会出现如下状况: 因为默认查看的是在线 ...
- char* strcpy( char* dest, const char* src ), int binary_search(int *arr, int key, int n), 可能的实现
#include <stdio.h> char* stringCopy( char* dest, const char* src ) { size_t i = 0; while (dest ...
- .Net之路(十四)com组件、OLEDB导入EXCEL
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/chenfanglincfl/article/details/30546777 .NET com组件 ...