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. 2012 #3 Flowers

    Flowers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  2. Maven 3.3.9在Windows上的安装

    开始学Maven了,可是我一个项目都木有做过.听过Maven 的大名,用来构建项目的. 下面记录下我安装Maven的过程 1.确认电脑上安装了JDK 在cmd下执行下列命令: java –versio ...

  3. Tomcat源码导入Eclipse测试

    想要研究下Tomcat的体系结构或者源码,最好将Tomcat的源码导入到ide中,编写实例进行代码跟踪(debug). 这里参考了网上一些资料,将自己操作过程记个流水账. 准备: 1.Tomcat源码 ...

  4. [JAVA设计模式]第三部分:结构模式

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. 第一发。。。codeforces 609 C Load Balancing 贪心

    /*题意:给你一个序列经过最小变换,变换一次为一个数+1,一个数-1,使得最大值与最小值相差1:思路:与最后得到的序列相差的sum/2:*/#include<iostream> #incl ...

  6. mac系统如何进行剪切

    来源: http://jingyan.baidu.com/article/1612d5007f76e7e20e1eeeca.html 分步阅读 mac os x下没有像windows中现成的“剪切”命 ...

  7. [转]-用Gradle 构建你的android程序

    出处:http://www.cnblogs.com/youxilua  前言 android gradle 的插件终于把混淆代码的task集成进去了,加上最近,android studio 用的是gr ...

  8. IQ一个人的智力和对科学知识的理解掌握程度。 EQ对环境和个人情绪的掌控和对团队关系的运作能力。 AQ挫折商 一个人面对困境时减除自己的压力、渡过难关的能力。

    IQ: Intelligence Quotient 智商 一个人的智力和对科学知识的理解掌握程度. EQ: Emotional Quotient 情商 一个人对环境和个人情绪的掌控和对团队关系的运作能 ...

  9. Nginx入门笔记之————配置文件结构

    在nginx.conf的注释符号位# nginx文件的结构,这个对刚入门的同学,可以多看两眼. 默认的config: #user nobody; worker_processes ; #error_l ...

  10. 关于JAVA中URL传递中文参数,取值是乱码的解决办法

    前几天看到有网友在问URLDecoder和URLEncoder方面的使用问题,突然想起,原来我刚遇到这两个类时,也觉得很神密,由此可以想想初学者的心情,于是便有了今天的这篇文章. 其实,这两个类的使用 ...