树的判定_待解决

树的判定

时间限制: 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 }

 

全部评论

相关推荐

来,说点可能被同行“骂”的大实话。🙊当初接数字马力Offer时,朋友都说:“蚂蚁的“内包”公司?你想清楚啊!”但入职快一年后的今天,我反而对他有了不一样的看法!🔹&nbsp;是偏见?还是信息差!之前没入职之前外面都在说什么岗位低人一等这类。实际上:这种情况不可至否,不能保证每个团队都是其乐融融。但我在的部门以及我了解的周边同事都还是十分好相处的~和蚂蚁师兄师姐之间也经常开一些小玩笑。总之:身份是蚂蚁公司给的,地位是自己挣的(一个傲娇女孩的自述)。🔹&nbsp;待遇?玩的就是真实!试用期工资全额发!六点下班跑得快(早9晚6或者早10晚7,动态打卡),公积金顶格交。别听那些画饼的,到手的钱和下班的时间才是真的(都是牛马何必难为牛马)。🔹&nbsp;能不能学到技术?来了就“后悔”!我们拥有权限直通蚂蚁知识库,技术栈多到学不完。说“学不到东西”的人,来了可能后悔——后悔来晚了(哈哈哈哈,可以不学但是不能没有)!💥&nbsp;内推地址:https://app.mokahr.com/su/ueoyhg❗我的内推码:NTA6Nvs走我的内推,可以直达业务部门,面试流程更快速,进度可查!今天新放HC,之前挂过也能再战!秋招已经正式开始啦~机会就摆在这,敢不敢来试一试呢?(和我一样,做个勇敢的女孩)
下午吃泡馍:数字马力的薪资一般哇,5年经验的java/测试就给人一万出头,而且刚入职第三天就让人出差,而且是出半年
帮你内推|数字马力 校招
点赞 评论 收藏
分享
迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务