HDU 1325 Is It A Tree? 判断是否为一棵树

本文讨论了如何通过给定的边描述判断一组节点集合是否满足树的定义,涉及图论基础知识,重点关注树的性质及算法实现。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325

POJ同题:http://poj.org/problem?id=1308

Is It A Tree?

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


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.
 
分析:此题纯属判断一个图是否满足一棵树,输入整得好坑爹- -!。
树的性质:空树或一个根节点、有向无环、除根结点外每个结点入度都为1。
注意,有环的图的情况:要么不存在入度为0的结点,要么存在入度大于1的点。
这样根据以上性质进行编程,如下:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn = 10010;
 7 bool vis[maxn];
 8 int indeg[maxn];
 9 int main()
10 {
11     int cas = 0;
12     int a, b;
13     while(scanf("%d%d", &a, &b),a>=0||b>=0)
14     {
15         if(a==0 && b==0)
16         {
17             printf("Case %d is a tree.\n", ++cas);
18             continue;
19         }
20         int maxx = 0;
21         if(a > maxx) maxx = a;
22         if(b > maxx) maxx = b;
23         memset(vis, false, sizeof(vis));    //该结点是否存在
24         memset(indeg, 0, sizeof(indeg));
25         vis[a] = vis[b] = true;     //设置结点存在
26         indeg[b]++;                 //入度+1,注意是有顺序的
27         while(scanf("%d%d", &a, &b), a!=0||b!=0)
28         {
29             if(a > maxx) maxx = a;
30             if(b > maxx) maxx = b;
31             indeg[b]++;             //入度+1
32             vis[a] = vis[b] = true; //设置结点存在
33         }
34         bool flag = true;
35         int k = 0;
36         for(int i = 1; i <= maxx; i++)
37             if(vis[i] && indeg[i] > 1)    //入度大于1,说明一个结点有两个父结点,违背树的性质
38             {
39                 flag = false;
40                 break;
41             }
42         for(int i = 1; i <= maxx; i++)
43             if(vis[i] && indeg[i] == 0)     //入度等于0的点有两个以上(包括)的话,则说明根结点不唯一,可能是森林
44                 k++;
45         if(!flag || k != 1)
46             printf("Case %d is not a tree.\n", ++cas);
47         else
48             printf("Case %d is a tree.\n", ++cas);
49     }
50     return 0;
51 }
View Code

 

转载于:https://www.cnblogs.com/dzkang2011/p/tree_1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值