HDU1325 Is It A Tree? 【并查集】
Is It A Tree?
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.



In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
题意:给定一个图,推断是否为树。
题解:须要推断森林,推断回路,推断入度是否大于1.
#include <stdio.h>
#include <string.h>
#define maxn 10002 int in[maxn], pre[maxn], count;
bool vis[maxn]; int ufind(int k)
{
int a = k;
while(pre[k] != -1) k = pre[k];
int b;
while(pre[a] != -1){
b = pre[a];
pre[a] = k;
a = b;
}
return k;
} int main()
{
//freopen("stdin.txt", "r", stdin);
int a, b, ok = 1, cas = 1;
memset(pre, -1, sizeof(pre));
while(scanf("%d%d", &a, &b)){
if(a < 0) break;
if(a == 0){
printf("Case %d is ", cas++);
if(count != 1) ok = 0;
printf(ok ? "a tree.\n" : "not a tree.\n");
ok = 1; count = 0;
memset(in, 0, sizeof(in));
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
continue;
}
if(!ok) continue;
if(!vis[a]){ ++count; vis[a] = 1; }
if(!vis[b]){ ++count; vis[b] = 1; }
if(++in[b] > 1) ok = 0;
a = ufind(a); b = ufind(b);
if(a == b) ok = 0;
else { pre[a] = b; --count; }
}
return 0;
}
HDU1325 Is It A Tree? 【并查集】的更多相关文章
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- hdu 1325 Is It A Tree? 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- NEON简单介绍
个128位四字寄存器Q0-Q15,32个64位双字寄存器D0-D31,两个寄存器是重叠的,在使用的时候须要特别注意,不小心就会被覆盖掉. NEON的数据类型:无符号整数.有符号整数.未指定类型的整数. ...
- cocos2dx-2.x CCFileUtils文件管理分析(2)
于1于,我只是对整体结构进行了分析,然后,2于,我会在一些我们经常使用的分析功能. //获取给定文件名称的全路径 //以下这非常长一段凝视.通过举样例,像我们说明cocos2dx获取文件全路径的规则. ...
- 【Android基础】listview控件的使用(1)------最简单的listview的使用
listview控件是项目开发中最常用的空间之一,我将慢慢推出关于listview的一系列的文章,先从最简单的,系统自带的listview开始吧! 先上效果图: activity_one.xml &l ...
- 求n阶勒让德多项式
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 161 Solved: 105 [Submit][Status][Web Board] Descrip ...
- Linux查找多个类似,但不同的名称和重命名文件
受试者被认为是百度侧面问题,Linux称号:寻找core.1.core.2....形式命名的文件,然后改变这些文件的名称bak.core.1.bak.core.2,...... 首先,你应该找到这些文 ...
- Tuple
Tuple(组元)是C# 4.0引入的一个新特性,编写的时候需要基于.NET Framework 4.0或者更高版本. 在以前编程中,当需要返回多个值得方法中,常常需要将这些值放置到一个结构体或者对象 ...
- C#使用xpath找到一个节点
Xpath这是非常强大.但对比是一个更复杂的技术,希望上面去博客园特别想看看一些专业职位.下面是一些简单Xpath的语法和示例,给你参考 <?xml version="1.0" ...
- WP8关于对地图开发的改进
原文:WP8关于对地图开发的改进 微软在2012年6月21日 发布了 Windows Phone 8的更新.带来大量的功能更新和全新的SDK.作为重头戏的部分是引入了 C++ 和 DirectX,支持 ...
- 经验36--C#无名(大事,物...)
有时候,方便代码,它会使用匿名的东西. 1.匿名事件 args.CookieGot += (s, e) => { this ...
- mapxtreme演示V1.3
mapxtreme演示V1.3 mapxtreme地图相关基本功能的演示其中包括 鹰眼地图,图层控制,发达,缩小,平移地图,地图模糊查询,中点工具,距离测量工具,面积测量工具,图元信息查看工具,各 ...