Hdu.1325.Is It A Tree?(并查集)
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16702 Accepted Submission(s): 3761
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.



5 6 0 0
7 4 7 8 7 6 0 0
5 3 5 6 5 2 0 0
#include<stdio.h>
#include<string.h>
const int M = + ;
int f[M] ;
int path[M] ;
int in[M] ;
bool vis[M] ; int Union (int x)
{
return x == f[x] ? x : f[x] = Union (f[x]) ;
}
int main ()
{
freopen ("a.txt" , "r" , stdin ) ;
int u , v ;
int cas = ;
while (~ scanf ("%d%d" , &u , &v)) {
if (u < || v < ) break ;
for (int i = ; i < M ; i ++) f[i] = i ;
memset (vis , , sizeof(vis));
memset (in , , sizeof(in)) ;
int tot = ;
if (!vis[u]) {
vis[u] = ;
path[tot ++] = u ;
}
if (!vis[v]) {
vis[v] = ;
path[tot ++] = v ;
}
in[v] ++ ;
// printf ("%d ----> %d\n" , u , v);
int x = Union (u) , y = Union (v) ;
f[y] = x ;
if (u != && v != ) {
while () {
scanf ("%d%d" , &u , &v) ;
// printf ("%d ----> %d\n" , u , v);
if (u == && v == ) break ;
if (!vis[u]) {
vis[u] = ;
path[tot ++] = u ;
}
if (!vis[v]) {
vis[v] = ;
path[tot ++] = v ;
}
in[v] ++ ;
int x = Union (u) , y = Union (v) ;
f[y] = x ;
}
for (int i = ; i < tot ; i ++) Union (path[i]) ;
// for (int i = 0 ; i < tot ; i ++) printf ("%d " , path[i]) ; puts ("") ;
// for (int i = 0 ; i < tot ; i ++) printf ("%d " , in[path[i]]); puts ("") ;
// for (int i = 0 ; i < tot ; i ++) printf ("%d " , f[path[i]]) ; puts ("") ;
bool flag = ;
int cnt = ;
int father = f[path[]] ;
for (int i = ; i < tot && !flag; i ++)
if (f[path[i]] != father )
flag = ; for (int i = ; i < tot && !flag ; i ++) {
if (in[path[i]] > ) flag = ;
if (in[path[i]] == ) cnt ++ ;
if (cnt > ) flag = ;
}
if (cnt == ) flag = ;
// printf ("flag = %d\n" , flag );
if (flag) printf ("Case %d is not a tree.\n" , cas ++);
else printf ("Case %d is a tree.\n" , cas ++) ;
}
else printf ("Case %d is a tree.\n" , cas ++) ;
}
return ;
}
检查入度,和每个结点的祖先。
终于解决了并查集压缩路径的不完全的问题。hahahaha。。。
另外没有结点,也被认为是树。
Hdu.1325.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)Tot ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- 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 - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
随机推荐
- Guava 集合框架
在本系列中我们首先来学习一些Guava的集合框架,也就是这个package:com.google.common.collect 在这个包下面有一些通用的集合接口和一些相关的类. 集合类型: BiM ...
- AngularJs angular.Module模块接口配置
angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...
- 如何判断ios设备中是否安装了某款应用
URL Schemes关键字研究一下即可 常见得URL Schemes见http://www.cnblogs.com/huangzs/p/4491286.html if ([[UIApplicatio ...
- Linux爆新漏洞,长按回车键70秒即可获得root权限
漏洞来源这个安全问题来源于Cryptsetup存在的一个漏洞(CVE-2016-4484).Cryptsetup是在Linux统一密钥设置(Linux Unified Key Setup, LUKS) ...
- 理解Docker单机容器网络
在” 理解Docker单机容器网络 “一文中,还有一个Docker容器网络的功能尚未提及,那就是Docker容器的端口映射.即将容器的服务端口P’ 绑定到宿主机的端口P上,最终达到一种效果:外部程序通 ...
- JavaWeb---总结(十八)JSP属性范围
所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...
- Saltstack远程执行(四)
Saltstack远程执行 语法例:salt '*' cmd.run 'w' - 命令:salt - 目标:'*' - 模块:cmd.run,自带150+模块,也可以自己写模块 - 返回:执行 ...
- [转]图片中的字符分割提取(基于opencv)
http://blog.csdn.net/anqing715/article/details/16883863 源图片 像这些图片的字符就比较好操作,每个字符都独立,不连在一起,所以轮廓检测最好了.所 ...
- 深入JVM-常用Java虚拟机参数
一.跟踪调试参数 1.1 跟踪垃圾回收-读懂虚拟机日志 Java的一大特色就是支持自动的垃圾回收(GC),但是有时候,如果垃圾回收频繁出现,或者占用了太长的CPU时间,就不得不引起重视.此时,就需要一 ...
- Java递归算法——汉诺塔问题
//================================================= // File Name : Tower_demo //-------------------- ...