1004. Counting Leaves (30)
来源:互联网 发布:java软件测试 编辑:程序博客网 时间:2024/06/11 20:38
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<vector>using namespace std;#define SIZE 100vector< vector<int> > node;int leaves[SIZE] = {0};void dfs(int id, int level){if(node[id].size() == 0){leaves[level]++;return;}vector<int>::iterator it = node[id].begin();for(it; it < node[id].end(); it++)dfs(*it, level+1);}int main(){int n, m;while(scanf("%d %d", &n, &m) != EOF){node.resize(n+1);int id, k, id_child;for(int i = 0; i < m; i++){scanf("%d %d", &id, &k);for(int j = 0; j < k; j++){scanf("%d", &id_child);node[id].push_back(id_child);}}int level = 0;dfs(1, level);int leaf_nodes = n-m;printf("%d", leaves[0]);int cnt = leaves[0];for(int i = 1; cnt < leaf_nodes; i++){printf(" %d", leaves[i]);cnt += leaves[i];}printf("\n");}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)
- final,finalize,finally的区别
- 排序算法——简单选择排序(Python&Java)
- 【51Nod1463】找朋友
- 数据结构思维 前言
- php 中抽象的方法和抽象类的定义 abstract
- 1004. Counting Leaves (30)
- Java线程池之ThreadPoolExecutor
- 加密
- Struts2 图片上传、缩放、剪切处理 第二节
- 1005. Spell It Right (20)
- Linux文件---文件IO概述
- Android实现异步从网络加载图片列表和上拉加载更多、下拉刷新列表(使用xListView框架实现)
- python代码高效经验
- 文档主题模型