hdoj1325 Is It A Tree? 判断是否为树 并查集

来源:互联网 发布:机器语言编程软件 编辑:程序博客网 时间:2024/05/02 04:46

Is It A Tree?

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


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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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.
 
/*1,无环2,除了根,所有的入度为1,根入度为03,这个结构只有一个根,不然是森林了注意:空树也是树*/#include<stdio.h>const int max_num=100000+10;typedef struct{int num,root,conn;//数据,根结点,入度}Node;Node node[max_num];void init()//初始化{for(int i=0;i<max_num;i++){node[i].conn=0;//入度初始化为0node[i].root=i;//根结点为自身node[i].num=0;//标记数字是否用过。0-没用过,1-用过}}int find(int a)//寻找它的根节点{if(node[a].root!=a)return node[a].root=find(node[a].root);elsereturn node[a].root;}void merge(int a,int b)//合并{a=find(a),b=find(b);if(a==b)//同一个根,说明在同一棵树里return;node[b].root=a;//把b的根结点赋给a的根结点,此时a已经是根结点}int main(){int n,m,i=1;bool flag=true;init();while(scanf("%d%d",&n,&m)!=EOF&&n>=0&&m>=0){if(!flag&&n!=0&&m!=0)continue;//判断是否是树if(n==0&&m==0)//说明输入结束,开始处理吧 {int root_num=0;for(int j=1;j<max_num;j++){//判断是否为森林。如果是,用root_num来记录根的数目if(node[j].num&&find(j)==j)root_num++;if(node[j].conn>1)//如果出现某个节点的入度超过1,则不是树{flag=false;break;}}if(root_num>1)//连通分支大于1,是森林,不是树flag=false;if(flag)printf("Case %d is a tree.\n",i++);            else printf("Case %d is not a tree.\n",i++);            init();            continue;}if(m!=n&&find(n)==find(m)||m==n)flag=false;else{//将m,n记为结点node[m].num=1;node[n].num=1;node[m].conn++;//入度加一merge(n,m);}}return 0;}

判断是否为树
0 0
原创粉丝点击