HDU 1325 Is It a Tree?

Posted EricJeffrey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1325 Is It a Tree?相关的知识,希望对你有一定的参考价值。

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

解题思路: 判断 连通 && 边数 == 节点数-1 && 只有一个根 即可。需要注意的是 0 0这种情况应该是 Yes

本题与Hdu1272神似

代码:

 1 const int maxn = 1e5 + 5;
 2 
 3 int fa[maxn], vis[maxn];
 4 int ind[maxn]; 
 5 
 6 void init(){
 7     for(int i = 1; i < maxn; i++) fa[i] = i;
 8     memset(vis, 0, sizeof(vis));
 9     memset(ind, 0, sizeof(ind));
10 }
11 int find(int x){
12     return x == fa[x]? x: fa[x] = find(fa[x]);
13 }
14 void unite(int x, int y){
15     x = find(x);
16     y = find(y);
17     if(x != y){
18         fa[x] = y;
19     }
20 }
21 int main(){
22     int a, b;
23     int tc = 1;
24     while(scanf("%d %d", &a, &b) && a >= 0){
25         printf("Case %d ", tc++);
26         if(a == 0 && b == 0){
27             puts("is a tree.");
28             continue;
29         }
30         int maxa = max(a, b), cnt = 1;
31         init();
32         ind[b]++;
33         unite(a, b);
34         vis[a] = 1; vis[b] = 1;
35         while(scanf("%d %d", &a, &b) && a != 0 && b != 0){
36             unite(a, b);
37             cnt++;
38             maxa = max(maxa, max(a, b));
39             vis[a] = 1; vis[b] = 1;
40             ind[b]++;
41         }
42         int n = 0, la = 0, root = 0;
43         bool flag = true;
44         for(int i = 1; i <= maxa; i++){
45             if(vis[i]) {
46                 n++;
47                 if(ind[i] == 0 && root == 0) {
48                     root = i;
49                 }
50                 if(ind[i] == 0 && root != i){
51                     flag = false;
52                     break;
53                 }
54                 if(la != 0 && find(i) != find(la)){
55                     flag = false;
56                     break;
57                 }
58                 la = i;
59             }
60         }
61 //        debug
62 //        printf("-->flag:%d\ncnt:%d n:%d\nroot:%d\n", flag, cnt, n, root);
63         if(cnt == n - 1 && flag){
64             puts("is a tree.");
65         }
66         else{
67             puts("is not a tree.");
68         }
69     }
70 }

题目:

Is It A Tree?

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


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
 

 

Recommend
Ignatius.L

















以上是关于HDU 1325 Is It a Tree?的主要内容,如果未能解决你的问题,请参考以下文章

HDU-1325-Is It A Tree?(并查集)

HDU 1325 Is It A Tree?

hdu1325 Is It A Tree? solution

HDU 1325 Is It A Tree?

HDU 1325,POJ 1308 Is It A Tree

HDU 1325 POJ 1308 Is It A Tree? (并查集)