1004. Counting Leaves (30)
来源:互联网 发布:淘宝女鞋店铺排行榜 编辑:程序博客网 时间:2024/05/20 18:45
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>const int MAX = 110;typedef struct people{int index;int children[MAX];}PEOPLE;PEOPLE a[MAX];int leaf[MAX];int count[MAX];int flag[MAX];void process(int num, int layer){int i;flag[layer] = 1;for (i = 0; i < a[num].index; i++){if (leaf[a[num].children[i]] == 0){count[layer]++;}else{process(a[num].children[i], layer + 1);}}}int main(){int N, M;int i, k, father, j, child;//freopen("d:\\input.txt", "r", stdin);scanf("%d%d", &N, &M);for (i = 0; i < M; i++){scanf("%d%d", &father, &k);leaf[father] = 1;for (j = 0; j < k; j++){scanf("%d", &child);a[father].children[a[father].index++] = child;}}process(1, 1);if (leaf[1] == 1){printf("0");}else{printf("1");flag[1] = 0;}for (i = 1; flag[i] == 1; i++){printf(" %d", count[i]);}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)
- Android 直播点赞(飘❤)
- 工作流系统之四十 抄送功能的实现
- [ 备战NOIP2016 ] 杂项
- POJ1274 The Perfect Stall
- 工作流系统之四十一 流程的单起点和多起点
- 1004. Counting Leaves (30)
- oracle监听listener连接风暴
- 基于mpc体系架构的I2C体系结构(适配器与core层交互)
- 重大新闻!Mozilla 将封杀沃通和 StartSSL 一年内新签发的所有证书
- Web自定义表单工具和协同办公系统之集成(1)
- java对称加密(DES/DESESE/AES)的简单使用
- 命令行 TODO 工具中的王者
- i2c体系结构篇(dev与core层交互)
- SpringMVC表单标签简介