1004. Counting Leaves (30)

时间限制

400 ms

内存限制

32000 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID‘s of its children. For the sake of simplicity, let us fix the root ID to be 01.

Output

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.

  1 #include <iostream>
  2
  3 using namespace std;
  4
  5
  6
  7 struct fun
  8
  9 {
 10
 11    int father;
 12
 13    bool ifdo;
 14
 15    int h;
 16
 17 };
 18
 19
 20
 21 fun F[101];
 22
 23
 24
 25 int main()
 26
 27 {
 28
 29
 30
 31    int n;
 32
 33    while(cin>>n)
 34
 35    {
 36
 37    int i;
 38
 39         for(i=1;i<=n;i++)
 40
 41 {
 42
 43     F[i].ifdo=true;
 44
 45 F[i].h=1;
 46
 47 F[i].father=-1;
 48
 49 }
 50
 51
 52
 53 int m;
 54
 55 cin>>m;
 56
 57         int high=0;
 58
 59         if(m==0)  cout<<1<<endl;
 60
 61         else
 62
 63 {
 64
 65
 66
 67  int name1,name2,num;
 68
 69        while(m--)
 70
 71 {
 72
 73       cin>>name1>>num;
 74
 75
 76
 77   F[name1].ifdo=false;
 78
 79       for(i=0;i<num;i++)
 80
 81   {
 82
 83      cin>>name2;
 84
 85                      F[name2].father=name1;
 86
 87   }
 88
 89
 90
 91 }
 92
 93
 94
 95
 96
 97              /*这题目陷阱在这,每行第一个ID并不是
 98
 99    从小到大按循序排列的,所以不能在上个循环中直接求h
100
101    需要记录父节点   在以下循环中求h;
102
103 */
104
105
106
107
108
109 for(i=2;i<=n;i++)
110
111  {
112
113     F[i].h=F[F[i].father].h+1;
114
115 if(F[i].h>high) high=F[i].h;
116
117  }
118
119
120
121
122
123  int j;
124
125              bool fir=true;
126
127          for(i=1;i<=high;i++)
128
129 {
130
131         int sum=0;
132
133        for(j=1;j<=n;j++)
134
135    {
136
137           if(F[j].ifdo&&F[j].h==i) sum++;
138
139    }
140
141                if(fir) cout<<sum;
142
143         else  cout<<" "<<sum;
144
145         fir=false;
146
147 }
148
149           cout<<endl;
150
151 }
152
153
154
155
156
157    }
158
159
160
161
162
163    return 0;
164
165 }
166
167
168  

时间: 2024-12-18 14:29:59

1004. Counting Leaves (30)的相关文章

PAT 1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tre

1004. Counting Leaves (30)——PAT (Advanced Level) Practise

题目信息: 1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contai

PTA 1004 Counting Leaves (30)(30 分)(建树dfs或者bfs,未AC 段错误)

1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 10

1004 Counting Leaves (30)(30 分)

1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 10

PAT 1004. Counting Leaves (30) C#实现

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i

PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; /* 统计每层的叶子节点个数 建树,然后dfs即可 */ const int maxn=105; int n,m; int layer[maxn]; //统计每层的叶子节点

PAT:1004. Counting Leaves (30) AC

#include<stdio.h> #include<vector> const int MAX=510; using namespace std; int n,m,le=0; //节点数,非叶子节点数,最深层叶层数 vector<int> child[MAX]; //存储孩子情况 int number[MAX]; //每一层叶子数 void DFS(int s,int l) { if(child[s].size()==0) { ++number[l]; if(le&l

1004 Counting Leaves (30分)

今天在热心网友的督促下完成了第一道PAT编程题. 太久没有保持训练了,整个人都很懵. 解题方法: 1.读懂题意 2.分析重点 3.确定算法 4.代码实现 该题需要计算每层的叶子节点个数,所以选用BFS 还有一个关键问题是 如何记录一层的开始和结束 另外,对于新手来说,图的存储也是一个知识点 容易忽略特殊取值情况下的答案: 当非叶节点个数为0,只有根节点一个点,所以直接输出1 而其他情况下,第一层叶子节点数为0 /**/ #include <cstdio> #include <cstrin