Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14525    Accepted Submission(s): 3232

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.
 
Source
 
 
判断一棵树:  (1)无环 ,(2)入度为0点有且仅有一个,其余点入度仅为1。(3)n个节点,必然有n-1条边
 
提供的一些数据:
/*
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 2 3 2 4 2 5 2 0 0
1 2 3 4 4 3 0 0
0 0
1 1 0 0
1 2 2 1 0 0
1 2 2 3 3 4 4 1 0 0
1 2 2 3 3 1 5 6 0 0
2 3 0 0
-1 -1
*/

答案:

/*
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
Case 4 is not a tree.
Case 5 is not a tree.
Case 6 is a tree.
Case 7 is not a tree.
Case 8 is not a tree.
Case 9 is not a tree.
Case 10 is not a tree.
Case 11 is a tree. */

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
bool hasp[];
struct node{
int father,rank;
};
node root[];
void init(){
int i;
root[].rank=;
root[].father=;
for(i=;i<;i++){
root[i].father=i;
root[i].rank=;
}
}
int find(int a){
while(a!=root[a].father)
a=root[a].father;
return a;
}
void Union(int a,int b){
/*a->b*/
root[b].father=a;
root[a].rank+=root[b].rank;
}
int main(){
freopen("test.in","r",stdin);
//system("call test.in");
int a,b,cnt,x,y,cas=,tem;
bool flag;
while(){
flag=false; //初始化为无环
memset(hasp,,sizeof(hasp));
tem=cnt=;
init();
while(scanf("%d%d",&a,&b)&&(a+b!=)){
if(a+b<) return ;
if(!flag)
{
x=find(a);
y=find(b);
if(x==y) flag=;
else Union(a,b);
if(tem==) tem=a;
if(!hasp[a]) hasp[a]= , cnt++ ;
if(!hasp[b]) hasp[b]= , cnt++ ;
}
}
/* cnt记录了点的个数 */
if(root[find(tem)].rank==cnt&&!flag)
printf("Case %d is a tree.\n",cas++);
else
printf("Case %d is not a tree.\n",cas++);
}
return ;
}

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) To ...

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

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

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

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

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

  5. HDU 5606 tree 并查集

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

  6. tree(并查集)

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

  7. hdu 5652 India and China Origins 并查集

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

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

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

  9. CF109 C. Lucky Tree 并查集

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

  10. 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. 逐个后移,匹配符合要求的选项,ie7有bug

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. PHP中的替代语法(冒号、endif、endwhile、endfor)

    我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如: <?php if(empty($GET_['a'])): ?> <font color=" ...

  3. FJNU 1154 Fat Brother And His Love(胖哥与女神)

    FJNU 1154 Fat Brother And His Love(胖哥与女神) Time Limit: 2000MS   Memory Limit: 257792K [Description] [ ...

  4. python_way day10 python和其他语言的作用域 、 python2.7多继承和3.5多继承的区别 、 socket 和 socketserver源码(支持并发处理socket,多进程,多线程)

    python_way day10 1.python的作用域和其他语言的作用域 2.python2.7多继承和3.5多继承的区别 3.socket和socketserver源码(并发处理socket) ...

  5. LINQ 如何实现 in 与 not in

    T-SQL的IN: Select ProductID, ProductName, CategoryID From dbo.Products Where CategoryID , ) T-SQL的NOT ...

  6. T-SQL排名函数

    提到排名函数我们首先可能想到的是order by,这个是排序,不是排名,排名需要在前面加个名次序号的,order by是没有这个功能的.还可能会想到identity(1,1),它也给了一个序号,但是不 ...

  7. elcipse 安装svn插件 转载

    1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java EE Developers)版    如果没有安装的请到这里下载安装:http://ecli ...

  8. Scrum Meeting---Two(2015-10-26)

    这次会议主要有两个方面 一.讨论项目 经过我们团队的激烈讨论,我们团队决定专注于做二手交易这一块.即将之前决定要做的学习经验交流以及校园交由这两块删除. 二.后两天的任务规划 以下便是我们的任务规划: ...

  9. [转载] 深入理解 docker ulimit

    深入理解docker ulimit 2015年7月23日 10:00 阅读 12778 [编者的话]Docker大规模应用后,如果你没踩过坑,说出去肯定没人信.昨天就遇到一个ulimit的经典问题:业 ...

  10. Spring集成JPA提示Not an managed type

    在做Spring与JPA集成时,出现问题如下: Caused by: java.lang.IllegalArgumentException: Not an managed type: class co ...