【HDU1325】Is It A Tree?(并查集基础题)
有以下坑点:
1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值。
2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树。
(无环路也可用树的性质:结点数 = 边树 + 1 来取代)
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <numeric>
using namespace std; const int maxn = ; int father[maxn];
int eage[maxn];
bool vis[maxn], flag = ;
int sum = ; int getFather (int x) {
while (father[x] != x) {
x = father[x];
}
return x;
} void Union (int p, int q) {
int x = getFather (p);
int y = getFather (q);
if (x != y) {
father[y] = x;
sum ++;
} else {
flag = ;
}
} int main () {
int x, y, cur = ;
while (cin >> x >> y) {
if (x < && y < ) break;
if (x == && y == ) {
printf("Case %d is a tree.\n", ++ cur);
continue;
} else {
flag = ;
memset(vis, , sizeof(vis));
memset(eage, , sizeof(eage));
for (int i = ; i < maxn; ++ i) {
father[i] = i;
}
vis[x] = vis[y] = ;
Union(x, y);
eage[y] ++;
while (cin >> x >> y) {
if (x + y == ) break;
vis[x] = vis[y] = ;
Union(x, y);
eage[y] ++;
}
sort(eage, eage + maxn, greater<int>());
int xx = ;
if (eage[] > ) flag = ;
for (int i = ; i < maxn; ++ i) {
if (vis[i] && father[i] == i) {
xx ++;
if (xx > ) {flag = ; break;}
}
}
/*for (int i = 1 ; i < maxn; ++ i) {
cout << vis[i] << " " ;
}*/ if (flag) printf("Case %d is a tree.\n", ++ cur);
else printf("Case %d is not a tree.\n", ++ cur);
}
}
return ;
}
【HDU1325】Is It A Tree?(并查集基础题)的更多相关文章
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- 【HDU1232】畅通工程(并查集基础题)
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- 【HDU1856】More is better(并查集基础题)
裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...
- 【HDU1272】小希的迷宫(并查集基础题)
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...
- 【HDU2120】Ice_cream's world I(并查集基础题)
查环操作,裸题.一次AC. #include <iostream> #include <cstring> #include <cstdlib> #include & ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
随机推荐
- 漫谈MySQL primaryKey
主键没有着明确的概念定义,其是索引的一种,并且是唯一性索引的一种,且必须定义为“PRIMARY KEY”,是只可意会不可言传的东西.下面让我用通俗,甚至有些低俗的语言为您简单介绍一下MySQL的主键. ...
- 图片实时预览JSP加js
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Collections.sort()
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...
- [hihocoder 1033]交错和 数位dp/记忆化搜索
#1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1 ...
- 奔五的人学IOS:swift练手与csdn,最近学习总结
早在五月份就准备開始学习ios开发,当时还是oc,学习了几天,最终不得其法.到了ios8开放,再加swift的出现.从10月份開始.最终找到了一些技巧,学习起来还算略有心得. 今天把我在学习swift ...
- eclipse 404以及tomcat failed to start错误
eclipse中的servlet项目有时会不编译,不编译可能就会出现404错误,因为在build path的输出目录并没有class文件,然而如果在输出目录引入之前编译的class文件,就可能出现cl ...
- 将html导出到excel或word
本质是将html写成word或excel支持的html格式. 如何将html写成word或excel支持的格式? 只需打开计算机上任意一个word或excel文档,打开文件->另存为,选择文件类 ...
- SQL cmd 实用工具学习 -1
启动 sqlcmd 实用工具并连接到 SQL Server 的默认实例 在"开始"菜单上,单击"运行". 在"打开"框中,键入 cmd,然后 ...
- Oracle语句块PL/SQL循环判断
- --pl/sql Procedural Language /sql --被数据库编译保存,由用户调用 --程序块 /* 语法 Declare – 声明变量 --声明变量 Age int; //没有 ...
- sql 创建临时表
declare @channelid varchar(100) set @channelid='''WH00026'',''WH00083''' declare @sql varchar(1000) ...