1004. Counting Leaves (30)
来源:互联网 发布:township清除数据ios 编辑:程序博客网 时间:2024/06/16 10:13
题目:
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 childfor 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
题解:
BFS遍历,统计每层叶子节点的数量,保存在了ans数组中。
代码:
#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>using namespace std;struct node{int id, k;int child[100];}a[110];int main(){int n, m, k, tmp;scanf("%d%d", &n, &m);//node *a = new node[n + 1];for(int i = 0; i <= n; i++)a[i].id = i;for(int i = 0; i < m; i++){scanf("%d", &tmp);scanf("%d", &a[tmp].k);for(int j = 0; j < a[tmp].k; j++)scanf("%d", &a[tmp].child[j]);}queue<int> q;int ans[100], cnt = 1;memset(ans, 0, 100 * sizeof(int));q.push(1);while(!q.empty()){int n = q.size();for(int i = 0; i < n; i++){int tmp = q.front();q.pop();if(a[tmp].k != 0){for(int j = 0; j < a[tmp].k; j++)q.push(a[tmp].child[j]);}elseans[cnt]++;}cnt++;}printf("%d", ans[1]);for(int i = 2; i < cnt; i++)printf(" %d", ans[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)
- 期刊发表要求大概有哪些
- AAPT2 Error
- securecrt 直连无法连上,需要中间主机跳转的方法
- Linux内核IP Queue机制的分析(三)——ip_queue内核模块的分析(下)
- ES6系列(一)->定义变量
- 1004. Counting Leaves (30)
- ActiveMQ的简单介绍和安装
- typeof()用法及JS基本类型
- mysq数据库管理工具navicat基本使用方法
- 11b、11a/g、11n和11ac速率
- talib 使用 RSI
- HDU-1059 Dividing(多重背包)
- 先获取经纬度 然后再输出
- 正则表达式-问号的四种用法