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 ...
随机推荐
- SQL server T-SQL存储过程
简介 存储过程是由一些SQL语句和控制语句组成的被封装起来的过程,它驻留在数据库中,可以被客户应用程序调用,也可以从另一个过程或触发器调用.它的参数可以被传递和返回.与应用程序中的函数过程类似,存储过 ...
- 上课总结-数据库Chapter2: 关系数据库
Chapter2: 关系数据库 一.搞懂主键 外键关系 主键(主码):能唯一标识一个元组的某一属性组. 外键:不是这组数据的主键 但是另一组数据的唯一主键(当这组数据的主键有2个时 可以作为外键) 例 ...
- 【ARC075F】Mirrored 搜索/数位dp
Description 给定正整数DD,求有多少个正整数NN,满足rev(N)=N+Drev(N)=N+D,其中rev(N)rev(N)表示将NN的十进制表示翻转来读得到的数 Input 一个 ...
- 并查集简述 (HDU-1213-How Many Tables)
并查集主要解决集合的有关运算,主要操作是查找操作和并操作. 1.集合的储存方式. 为便于查找,集合通常以树结构储存,每个元素分 数据域和指针域,可以用链式储存,也可以用结构数组储存,用根节点来表示一个 ...
- 在Pd中取消Code Name 同步
以前记得现在忘记了,好不容易找回来,记住备忘吧.
- 1.2、Logistics Regression算法实践
1.1.Logistics Regression算法实践 有了上篇博客的理论准备后,接下来,我们用以及完成的函数,构建Logistics Regression分类器.我们利用线性可分的数据作为训练样 ...
- ubuntu下QtCreator启动无响应问题解决
打开Qt后就卡死. 解决方法:删除系统配置目录下的QtProject文件夹: find / -name QtProject 输出: /root/.config/QtProject 删除QtProjec ...
- centos6+如何对外开放80,3306端口号或者其他端口号
1.查看防火墙对外开放了哪些端口 [root@hadoop110 ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt sour ...
- 压缩感知(CS)
总结一下最近看的压缩感知(Compressed Sensiong)的内容. 它是在采样过程中完成了数据压缩的过程. 一. 将模拟信号转换为计算机能够处理的数字信号,必然要经过采样的过程.问题在于,应该 ...
- bzoj3252 攻略 dfs序+线段树
题目传送门 题目大意:给出一棵树,1为根节点,每个节点都有权值,每个叶子节点都是一个游戏的结局,选择k个游戏结局,使得权值总和最大,同一个节点不会被重复计算. 思路:这道题最关键的是要想到一个性质,就 ...