树的判定_待解决

树的判定

时间限制: 1000 ms  |  内存限制:65535 KB
难度: 4
 
<dl class="problem&#45;display"> <dt> 描述 </dt> <dd>

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.

 

</dd> </dl>
 
<dl class="others"> <dt> 输入 </dt> <dd> 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.

The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1. </dd> <dt> 输出 </dt> <dd> 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). </dd> <dt> 样例输入 </dt> <dd>
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
</dd> <dt> 样例输出 </dt> <dd>
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
</dd> </dl>

 

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 typedef struct node{
 8     int father;
 9     int son;
10 };
11 node n[10005];
12 
13 int main()
14 {
15     int a,b;
16     int ff=0;
17     int ss=0;
18     int maxx=0;
19     int y=0;
20     int cou=0;
21     while(scanf("%d %d",&a,&b)!=EOF&&a!=-1&&b!=-1){
22         y=0;
23         cou++;
24         memset(n,0,10005*sizeof(node));
25         maxx=max(maxx,max(a,b));
26         if(a==b&&a!=0){
27             y=1;goto loap;
28         }
29         if(a==b&&a==0){
30             y=0;
31             ss=1;
32             goto loap;
33         }
34         ss=0;
35         n[b].father++;
36         n[a].son++;
37         while(scanf("%d %d",&a,&b)&&(a+b)){
38             if(a==b){
39                 y=1;goto loap;
40             }
41             n[b].father++;
42             n[a].son=1;
43         }
44 
45         for(int i=0;i<=maxx;i++){
46             if(n[i].father>1){
47                 printf("Case %d is not a tree.\n",cou);\
48                 y=1;
49                 break;
50             }
51             if(n[i].son==1){
52                 if(n[i].father==0){
53                     ss++;
54                 }
55             }
56         }
57         loap:
58         if(ss!=1){
59             printf("Case %d is not a tree.\n",cou);
60             y=1;
61         }
62         if(y!=1){
63             printf("Case %d is a tree.\n",cou);
64         }
65 
66     }
67     return 0;
68 }

 

全部评论

相关推荐

10-24 11:10
山西大学 Java
若梦难了:哥们,面试挂是很正常的。我大中厂终面挂,加起来快10次了,继续努力吧。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务