1004. Counting Leaves (30)
来源:互联网 发布:php curl 文件上传 编辑:程序博客网 时间:2024/05/24 05:41
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<stdio.h>#include<math.h>#include<vector>using namespace std;int n,m;vector<int>child[100];int cou[100]={0};int deepest=-1;void dfs(int index,int depth){if(child[index].size()==0){cou[depth]++;if(depth>deepest){deepest=depth;}return ;}int i;for(i=0;i<child[index].size();i++){dfs(child[index][i],depth+1);}}int main(){int i,j,k,father,temp;scanf("%d %d",&n,&m);for(i=0;i<m;i++){scanf("%d %d",&father,&k);for(j=0;j<k;j++){scanf("%d",&temp);child[father].push_back(temp);}}dfs(1,1);printf("%d",cou[1]);for(i=2;i<=deepest;i++){printf(" %d",cou[i]);}}
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- Tensorflow一些常用基本概念与函数(2)
- Unity3D 利用FSM设计相机跟随实现
- 深入理解javascript原型和闭包(14)——从【自由变量】到【作用域链】
- Tensorflow一些常用基本概念与函数(3)
- SAP HANA计算两个日期的差值
- 1004. Counting Leaves (30)
- (8)关卡编辑器之视口选项
- 恢复勒索病毒“永恒之蓝”中招的文件,ooops,your files have been encrypted!解决方案
- Nuttx romfs与启动脚本rcS
- 51nod1227 平均最小公倍数
- NYOJ 927 The partial sum problem(深搜)
- week 5 mongodb学习
- WeMos D1 R2使用笔记
- 操作系统原理与实践8-地址映射与共享