1004. Counting Leaves (30)
来源:互联网 发布:手机老是弹出淘宝口令 编辑:程序博客网 时间:2024/06/09 23:01
1004. Counting Leaves (30)
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<stdlib.h> #include<math.h>#include<string.h>#include<algorithm>#include <vector> #include<stack> #include <queue> #include<map>#include<iostream> #include <functional> #define MAX 110#define MAXD 10001#define TELNUM 10using namespace std;struct node{vector<int>child;}Node[MAX];int n,m,t = 0;int count1[MAX] = {0};int Max = 0;/*记录最大层数*/void DFS(int root,int depth){if(Node[root].child.size() == 0){count1[depth]++;if(depth > Max)/*更新最大层数*/{Max = depth;}return;}for(int i = 0; i < Node[root].child.size(); i++){DFS(Node[root].child[i],depth + 1);}}int main(void){scanf("%d%d",&n,&m);int root,child,k;for(int i = 0; i < m; i++)/*建树*/{scanf("%d%d",&root,&k);for(int j = 0; j < k; j++){scanf("%d",&child);Node[root].child.push_back(child);}}DFS(1,1);/*默认01是树的根结点,根结点为第一层*/for(int i = 1; i <= Max; i++){printf("%d",count1[i]);if(i < Max){printf(" ");}}return 0;}
- 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)
- 观察者模式Observable与Observer的运用(单指拖放)
- Activity切换动画效果的修改
- 采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序(多activity)
- File I/O总结
- 有弹性的ScrollView&ListView&HorizontalScrollView
- 1004. Counting Leaves (30)
- STM32架构相关
- 对象与json转换
- Javaweb 02—Servlet
- 紫书章五习题八 图书管理系统
- 布局式跑马灯,非TextView
- Postman使用手册1——导入导出和发送请求查看响应
- 自定义RadioButton样式1
- SYN Flood攻击实验流程