1004. Counting Leaves (30)

 
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 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 Input

2 1
01 1 02

Sample Output

0 1

有关树的遍历的问题,用到了深搜的方法
树的存放很有意思,map<int,vector<int> >; key为某个节点的ID,vector是他的孩子ID数组
#include <iostream>
#include <map>
#include <vector> using namespace std; map<int,vector<int> > adjlist;
int levelleaves[]={}; void dfs(int node,int level){
if(adjlist[node].empty()){
levelleaves[level]++;
return;
}
vector<int>:: iterator itea = adjlist[node].begin();
for(;itea!=adjlist[node].end();itea++){
dfs(*itea,level+);
}
} int main()
{
int n,m;
cin>>n>>m;
int leaves = n-m;
for(int i=;i<m;i++){
int id1,k, id2;
cin>>id1>>k;
for(int j=;j<k;j++){
cin>>id2;
adjlist[id1].push_back(id2);
}
}
dfs(,);
int a=levelleaves[];
cout<<levelleaves[];
for(int i=;a<leaves;i++){
cout<<" "<<levelleaves[i];
a=a+levelleaves[i];
}
return ;
}


1004. Counting Leaves (30)的更多相关文章

  1. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  2. PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

    1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...

  3. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  4. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  5. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  6. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  7. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

  8. PAT (Advanced Level) 1004. Counting Leaves (30)

    简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  9. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

随机推荐

  1. placeholder实现的两种方式

    /** * PlaceHolder组件 * $(input).placeholder({ * word: // @string 提示文本 * color: // @string 文本颜色 * evtT ...

  2. 纯css控制-表格表头固定,内容多时滚动内容

    <html> <head> <style type="text/css"> * { margin: 0; padding: 0; } /*所有内 ...

  3. 一颗躁动的心---下决心从SLAM开始,不钻研嵌入式底层了

    在写这个随笔时,北京的外面正在下2016的第一场雪.夜深人尽之时总会考虑一下自己的未来在何方. 长这么大了,我发现我这人始终不能坚定不移的朝着一个方向努力,总是朝三暮四,对学习更是朝令夕改,这造成了我 ...

  4. JS实现 页面提交防刷新等待提示

    //关闭等待窗口 function closediv() { //Close Div document.body.removeChild(document.getElementById("b ...

  5. (转)强大的JQuery表单验证插件 FormValidator使用介绍

    jQuery formValidator表单验证插件是客户端表单验证插件.在做B/S开发的时候,我们经常涉及到很多表单验证,例如新用户注册,填写个人资料,录入一些常规数据等等.在这之前,页面开发者(J ...

  6. ASP.NET Core Identity Hands On(1)——Identity 初次体验

    ASP.NET Core Identity是用于构建ASP.NET Core Web应用程序的成员资格系统,包括成员资格.登录和用户数据存储 这是来自于 ASP.NET Core Identity 仓 ...

  7. VS Code 1.18版本更新内容整理(2017年10月 October 2017)

    久前开始使用的VS Code,使用一段时间以后确实感觉比之前在用的Sublime Text好很多,可能是汉化及插件方面使用做的更好吧. 今天推送到更新到1.18,按我的个性,喜欢一个东西的话,我就回去 ...

  8. 元素的属性:client系列,scroll系列,offset系

    元素的属性 div.attributes 是所有标签属性构成的数组集合 dir.classList 是所有class名构成的数组集合 在classList的原型链上看一看到从 add()和remove ...

  9. C# WebBrowser的8个方法、13个属性和事件

    1.方法 说明 GoBack 相当于IE的“后退”按钮,使你在当前历史列表中后退一项 GoForward 相当于IE的“前进”按钮,使你在当前历史列表中前进一项  GoHome 相当于IE的“主页”按 ...

  10. vue-cli使用说明

    一.安装npm install -g vue-cli 推荐使用国内镜像 先设置cnpm npm install -g cnpm --registry=https://registry.npm.taob ...