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.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.
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条臭 ...
随机推荐
- AngularJs angular.uppercase、angular.lowercase、angular.fromJson、angular.toJson
angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...
- Appium for IOS testing on Mac
一:环境 1.Mac OS X 10.9.1 2.Xcod 5.0.2 3.Appium 1.3.6 下载地址:https://bitbucket.org/appium/appium.app/down ...
- Beta版本冲刺第六天 12.12
一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 邮箱验证和用户评价的填写 用户评价的查看以 ...
- 强大的css3
强大的css3 我们知道,这几年来智能手机的高速发展使得人们使用移动端上网的时间和人数已经超过了PC端.例如在2015年,就中国电商而言,各电商平台在移动端持续发力,移动端购物占比不断攀升,双11期间 ...
- wpf的毛边窗体效果 前台代码
<Window x:Class="wpfwindowsmove.毛边窗体" xmlns="http://schemas.microsoft.com/w ...
- break; continue; goto; return在循环中的应用
1. break表示跳出循环,程序指向循环体后的第一条语句: ; ) { ) break; console.writeline("{0}",i++); } console.read ...
- C#------DateTime自定义格式
var text = Convert.ToString(DateTime.Now.ToString("yyyy/MM/dd"));
- 使用Newtonsoft JsonConvert反序列化Json数据到DataTable
//JsonStr为Json字符串 JArray array = JsonConvert.DeserializeObject(JsonStr) as JArray;//反序列化为数组 ) { Stri ...
- 9月23日JavaScript作业----用DIV做下拉列表
例题二.用div做下拉列表 <title>无标题文档</title> <style type="text/css"> *{ margin:0px ...
- 20145212 《Java程序设计》第2周学习总结
20145212 <Java程序设计>第2周学习总结 教材学习内容总结 第二周主要学习了Java的基本语法,在第一周熟悉了Java基本操作的基础上我们可以自行的利用这些语法编译具有实用价值 ...