【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 ...
随机推荐
- 【POJ2136】Vertical Histogram(简单模拟)
比较简单,按照样例模拟就好!~ #include <iostream> #include <cstdlib> #include <cstdio> #include ...
- shell惊鸿
显示当前用户uid
- 【案例】舒邑:一个女装品牌的奇葩打法-@i黑马
[案例]舒邑:一个女装品牌的奇葩打法-@i黑马 [案例]舒邑:一个女装品牌的奇葩打法
- Entify Framewrok - LINQ简单使用
1.如何使用Join: http://www.devcurry.com/2011/01/join-example-in-linq-and-c.html
- js+css实现模态层效果
在做web前端的时候,有些时候会涉及到模态层,在此提供一种实现思路.希望对大家实用.先贴效果吧: 模态层效果 以下说说在写模态层的时候的思路:通过可配置的參数width,height,title以及c ...
- java foreach循环为什么不能赋值
直接上代码 public class test4 { public static void main(String args[]){ int [] a=new int[3]; for(int j:a) ...
- neural style论文解读
相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 概述 在艺术领域,艺术家 ...
- samba常用命令
1.# smbstatusSamba version 3.6.23PID Username Group Machine ---------------------------------------- ...
- document.write 向文档中写内容,包括文本、脚本、元素之类的,但是它在什么时候执行不会覆盖当前页面内容尼?
当你打开一个页面,浏览器会 调用 document.open() 打开文档 document.write(...) 将下载到的网页内容写入文档 所有内容写完了,就调用 document.close() ...
- 诡异的SpriteKit 游戏查错
在Endless Runner 游戏中,做了一些atlas后,发现有个问题,当player跳跃起来的时候,发现他没有动画了,被默认的X图片代替.原来的图像是这样的. 在增加了一些动画后,我的效果就成这 ...