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

Problem Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.  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. 
 
Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist 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. 
 
Output
For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1). 
 
Sample Input
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
 
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

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

  1. 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 ...

  2. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  3. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  4. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  5. tree(并查集)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  6. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  7. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  8. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  9. 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条臭 ...

随机推荐

  1. AngularJs angular.uppercase、angular.lowercase、angular.fromJson、angular.toJson

    angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...

  2. 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 ...

  3. Beta版本冲刺第六天 12.12

    一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 邮箱验证和用户评价的填写 用户评价的查看以 ...

  4. 强大的css3

    强大的css3 我们知道,这几年来智能手机的高速发展使得人们使用移动端上网的时间和人数已经超过了PC端.例如在2015年,就中国电商而言,各电商平台在移动端持续发力,移动端购物占比不断攀升,双11期间 ...

  5. wpf的毛边窗体效果 前台代码

    <Window x:Class="wpfwindowsmove.毛边窗体"        xmlns="http://schemas.microsoft.com/w ...

  6. break; continue; goto; return在循环中的应用

    1. break表示跳出循环,程序指向循环体后的第一条语句: ; ) { ) break; console.writeline("{0}",i++); } console.read ...

  7. C#------DateTime自定义格式

    var text = Convert.ToString(DateTime.Now.ToString("yyyy/MM/dd"));

  8. 使用Newtonsoft JsonConvert反序列化Json数据到DataTable

    //JsonStr为Json字符串 JArray array = JsonConvert.DeserializeObject(JsonStr) as JArray;//反序列化为数组 ) { Stri ...

  9. 9月23日JavaScript作业----用DIV做下拉列表

    例题二.用div做下拉列表 <title>无标题文档</title> <style type="text/css"> *{ margin:0px ...

  10. 20145212 《Java程序设计》第2周学习总结

    20145212 <Java程序设计>第2周学习总结 教材学习内容总结 第二周主要学习了Java的基本语法,在第一周熟悉了Java基本操作的基础上我们可以自行的利用这些语法编译具有实用价值 ...