【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 ...
随机推荐
- 选择Comparable接口还是Comparator
个人理解: 如果我本身知道这个类的对象我要用来比较,那么就拿这个类实现Comparable接口(compareTo(Object o) 方法).如果我本身没有预料到我要比较这个类的对象,那么,我可以建 ...
- HTML--鼠标事件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- HTTP协议具体解释
HTTP是一个属于应用层的面向对象的协议.因为其简捷.高速的方式.适用于分布式超媒体信息系统. 它于1990年提出,经过几年的使用与发展,得到不断地完好和扩展.眼下在WWW中使用的是HTTP/1.0的 ...
- IOS 6.0+ Autolayout — UITableViewCell 高度调整
要实现的效果 要求: storyboard上的准备工作 建好cell自定义AutoCell 类,关联好控件,事先准备好一些数据源 实现tableview的委托方法 核心部分,HeightForRow方 ...
- javasscript学习笔记 之 数组学习二 数组的所有方法
1.push() 和 pop() 栈的方法 后进先出 push() 该方法是向数组末尾添加一个或者多个元素,并返回新的长度. push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返 ...
- JS属性读写操作+if判断注意事项
js中不允许出现“ - ” 页面中改变文字大小-案例: <!doctype html> <html lang="en"> <head> < ...
- WPF XAML之bing使用StringFormat(转)
释义 BindingBase.StringFormat 属性 获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式. 命名空间: System.Windows ...
- Windows 不能在 本地计算机 启动 SQL Server(MSSQLSERVER)。错误码126
结合自己的解决方案和网络上搜到的内容,现总结如下: 首先你要知道问题出在了什么地方才能针对性处理. 1.打开事件查看器 计算机右击——管理 右侧会出现错误列表,在其中找到SQL server有关的查看 ...
- 如何制作windows服务安装包
以下转自:http://blog.csdn.net/chainan1988/article/details/7087006 Window服务的安装有两个方式: 一.命令安装 通过命令 ...
- web 调用OPC HRESULT:0x80070005 (E_ACCESSDENIED))
除了配置DCOM外,还需要在web.config里面添加设置系统管理员权限的帐号和密码<identity impersonate="true" userName=" ...