pat1004. Counting Leaves (30)
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 Input
2 1
01 1 02
Sample Output
0 1
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
struct node{
int floor,sonnum;
vector<int> v;
node(){
sonnum=;
}
};
node t[];
int f[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,m;
int i,num,j,k,maxfloor;
memset(f,,sizeof(f));
scanf("%d %d",&n,&m); //cout<<n<<" "<<m<<endl; for(i=;i<m;i++){
scanf("%d",&num); //cout<<num<<endl; t[num].floor=;
scanf("%d",&t[num].sonnum); //cout<<t[num].sonnum<<endl; for(j=;j<t[num].sonnum;j++){
scanf("%d",&k);
t[num].v.push_back(k);
}
}
queue<int> q;
int top=;
maxfloor=;
q.push(top);
while(!q.empty()){
top=q.front();
q.pop();
if(!t[top].sonnum){
f[t[top].floor]++;
continue;
} //cout<<t[top].floor<<endl; if(t[top].floor+>maxfloor){
maxfloor=t[top].floor+;
//cout<<maxfloor<<endl;
}
for(i=;i<t[top].sonnum;i++){
t[t[top].v[i]].floor=t[top].floor+;
q.push(t[top].v[i]);
}
} //cout<<maxfloor<<endl;
printf("%d",f[]);
for(i=;i<=maxfloor;i++){
printf(" %d",f[i]);
}
printf("\n");
return ;
}
pat1004. Counting Leaves (30)的更多相关文章
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT1004:Counting Leaves
1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...
- 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 ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT-1004 Counting Leaves
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
随机推荐
- windows phone开发必备工具翔
1.图标设计 http://www.flaticon.com/ http://www.iconfont.cn/ 2.界面设计: 2.1.behance.com 2.2.dribbble.com ...
- c#设计模式之:组合模式(Composite)
一:引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理,因为目录客园包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象在功能上 ...
- SQL server T-SQL索引详解
SQL索引在数据库优化中占有一个非常大的比例,一个好的索引的设计,可以让sql语句查询效率提高很多被. 1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引,索引的主要目的是提高T-SQL系统 ...
- vs 2017局域网内调试
之前调试代码都是在本地启动服务,以 localhost:端口号 的形式调试,今天发现也是可以用ip地址的形式来调用接口,这种方式可以支持内网内Client端调用接口,实现调试的功能,具体方法如下 ...
- javascript framework vue.js
vue.js 参考: http://cn.vuejs.org/guide/installation.html 不管使用何框架,首先都是要创建它的实例: var vue = new Vue({//参 ...
- c++实现多叉树树形显示(适合家谱的显示)
多叉树(左兄弟右孩子二叉树)的树形显示 核心代码 void positionadd(Multiway_tree*root, int n) { if (!root)return; Multiway_tr ...
- EF进阶篇(三)——上下文
前言 上下文,到底什么是上下文,且听我仔细吹来. 内容 在对EF实体进行关系操作的时候,第一步需要我们创建上下文实例对象,然后根据实体的变化进而通过上下文对该实体进行状态的修改,我的理解就是上下文就是 ...
- 【转】检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问
源地址:https://www.cnblogs.com/love522/p/4462253.html 最近在做一个网站时,有一个下载word文档功能,在本地直接调试是可以下载的,但部署到IIS上就出现 ...
- ASP Session 对象
http://www.w3school.com.cn/asp/asp_sessions.asp
- P2045 方格取数加强版 最大费用最大流
$ \color{#0066ff}{ 题目描述 }$ 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每 ...