有以下坑点:

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?(并查集基础题)的更多相关文章

  1. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  2. 【HDU1232】畅通工程(并查集基础题)

    裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  3. 【HDU1856】More is better(并查集基础题)

    裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...

  4. 【HDU1272】小希的迷宫(并查集基础题)

    仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...

  5. 【HDU2120】Ice_cream's world I(并查集基础题)

    查环操作,裸题.一次AC. #include <iostream> #include <cstring> #include <cstdlib> #include & ...

  6. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  7. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

  8. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  9. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

随机推荐

  1. C 编程调试集

    gcc rw.c rw.c:75:6: warning: conflicting types for ‘process_conn_server’ void process_conn_server(in ...

  2. Quartz定时调度CronTrigger时间配置格式说明与实例

    1.   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...

  3. zoj 3811 Untrusted Patrol(bfs或dfs)

    Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a l ...

  4. 一张图解析如何让img垂直居中对齐

    测试代码: <!DOCTYPE html> <html> <head> <style> .dd { background-color: gray; po ...

  5. 黑马程序员——String类

    ------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...

  6. (转)修改IIS默认的localhost名称

    看惯了http://localhost在本地测试的域名,想换种写法行不行,比如说想变成http://www.ceo.com,但网页访问仍然是原来http://localhost的内容,如何做到呢? 可 ...

  7. SQL Server 2005中的分区表(三):将普通表转换成分区表

    在设计数据库时,经常没有考虑到表分区的问题,往往在数据表承重的负担越来越重时,才会考虑到分区方式,这时,就涉及到如何将普通表转换成分区表的问题了. 那么,如何将一个普通表转换成一个分区表 呢?说到底, ...

  8. OOM总结

    本文主要信息是来自互联网,我只是自己做了一点总结和摘要. OOM发生的原因 简单的说通过不同的内存分配方式对不同的对象进行操作,会因为android系统版本的差异而产生不同的行为.主要是2.0和4.0 ...

  9. unity——使用角色控制器组件+射线移动

    首先要导入unity标准资源包Character Controllers 这个标准资源包,为了方便,还添加了两外一个资源包Scripts,后者包含了一些基本的脚本个摄像机脚本. 没错,这次我们要使用其 ...

  10. asp.net mvc 删除栏目、栏目下又有子栏目的处理方式