1004. Counting Leaves (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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

提交代码

 #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)的更多相关文章

  1. 1004. Counting Leaves (30)

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

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

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

  3. PAT1004:Counting Leaves

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

  4. 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 ...

  5. PAT 1004 Counting Leaves (30分)

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

  6. 1004 Counting Leaves (30分) DFS

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

  7. PAT-1004 Counting Leaves

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

  8. PAT 1004. Counting Leaves (30)

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

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

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

随机推荐

  1. WinForm中的重绘 - 文本的重绘

    两种方式 TextRenderer.DrawText 注意:默认在每次绘制的文本左右有padding,即使参数中设置了TextFormatFlags.NoPadding也是一样,因此在分段绘制文本时( ...

  2. NSKeyedArchiver数据归档

    前言 在 OC 语言中,归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象. 通常,这个过程包括将(多个)对象写入文件中,以便以后读取该对象.可以使用归档的方法进行对象的深复制. ...

  3. java 列表与集合总结

    列表与集合 (一切输出都用for each!丢弃迭代器) 列表List 1 顺序表 Arraylist  适用于静态查找2   链式双向表 Linkedlist 适用于增删该查3 (容器) Vecto ...

  4. 51 nod 1350 斐波那契表示

    每一个正整数都可以表示为若干个斐波那契数的和,一个整数可能存在多种不同的表示方法,例如:14 = 13 + 1 = 8 + 5 + 1,其中13 + 1是最短的表示(只用了2个斐波那契数).定义F(n ...

  5. golang文件处理函数openfile与linux系统的文件函数的耦合

    golang运行最理想的环境是linux系统中,编译速度和执行速度都比较快,本文是关于golang中的文件操作函数 在golang标准库中os包提供了不依赖平台的借口,但是使用的风格是unix风格的. ...

  6. 【bzoj1066】: [SCOI2007]蜥蜴 图论-最大流

    [bzoj1066]: [SCOI2007]蜥蜴 把石柱拆点,流量为高度 然后S与蜥蜴连流量1的边 互相能跳到的石柱连inf的边 石柱能到边界外的和T连inf的边 然后跑dinic就好了 /* htt ...

  7. Unity---高度解耦和

    介绍 先举一个简单的例子: 在UGUI中新建一个Button和Text,要求实现点击Button改变Text中的文字. 我的第一反应就是在Button上添加一个脚本,获取点击事件来改变Text的内容. ...

  8. JUC包下CyclicBarrier学习笔记

    CyclicBarrier,一个同步辅助类,在API中是这么介绍的: 它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这 ...

  9. springboot整合mybatis,druid,mybatis-generator插件完整版

    一 springboot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...

  10. 【转】nginx在Windows系统启动不了

    这几天用到Nginx,第一次是win7系统下部署,一次性成功,第二次在win10系统下,部署失败. 出现的情况: 打开Nginx.exe,界面一闪而过,而且进程里面搜不到Nginx. 1.端口占用问题 ...