PAT A1004. Counting Leaves (30)
来源:互联网 发布:大华网络监控安装步骤 编辑:程序博客网 时间:2024/06/06 11:42
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.
Sample Input2 101 1 02Sample Output
0 1
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <stack>#include <queue>#include <vector>using namespace std;const int Max = 110;int n,m;vector<int> S[Max];;int D[Max]={0};int f= 0;void DFS(int root, int depth){if(S[root].size() == 0){D[depth]++;if(depth>f){f=depth;}}for(int i=0;i<S[root].size();i++){DFS(S[root][i],depth + 1);}}int main(){scanf("%d%d",&n,&m);int temp,num,ans;for(int i=0;i<m;i++){scanf("%d%d",&temp,&num);for(int j=0;j<num;j++){scanf("%d",&ans);S[temp].push_back(ans);}}DFS(1,1);for(int i=1;i<=f;i++){printf("%d",D[i]);if(i<f) printf(" ");}system("pause");return 0;}
- PAT A1004 Counting Leaves(30)
- PAT A1004. Counting Leaves (30)
- a1004. Counting Leaves (30)
- A1004. Counting Leaves (30)
- PAT甲级练习题A1004. Counting Leaves
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- [PAT]1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- Maven分离配置、依赖
- hdu 2647 Reward
- Maven 学习
- CSU-1011
- Qt进度条提示对话框
- PAT A1004. Counting Leaves (30)
- iOS 去掉字符串中特殊字符
- java
- 迷你TXT小说阅读器 V2.8 内测3 发布!
- 用Tensorflow拟合线性函数
- 【PAT】1119. Pre- and Post-order Traversals
- 新的一年,SEO之路如何走更好
- 1081. Rational Sum
- OKHttp实现大文件的断点续传