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. vs调试的时候debug和release的区别

    今天在VS项目中调式遇到一个问题,断点快速查询变量的发现变两竟然不存在 花了一个小时到处百度也查不出一个所以然,后来请教了大神才知道VS调试有debug和release两个模式, vs中的程序有deb ...

  2. (二)RabbitMQ使用笔记

    1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...

  3. 快速理解mysql主从,主主备份原理及实践

    感谢大家在上一篇 学一点Git--20分钟git快速上手 里的踊跃发言.这里再次分享干货, 简单介绍mysql双机,多机异地热备简单原理实战. 双机热备的概念简单说一下,就是要保持两个数据库的状态自动 ...

  4. xunsearch

    xunsearch 1.wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2 2.解压 tar -jxvf xuns ...

  5. Windows NLB搭配IIS的ARR搭建高可用环境(转载)

    原文地址:http://www.cnblogs.com/shanyou/archive/2010/04/28/1723276.html 在现行的许多网络应用中,有时一台服务器往往不能满足客户端的要求, ...

  6. [译文]Casperjs1.1.0参考文档-快速开始

    快速开始 只要casperjs被正确安装,你就可以开始写你的第一个脚本,你可以使用javascript或者coffiescript编译脚本. 提示: 如果你对javascript不是很熟悉,最好先看专 ...

  7. Python登陆人人网

    #!coding:utf-8 import urllib2 import urllib import cookielib def renrenBrower(url,user,password): #登 ...

  8. eclipse中查看java源码时,出现source not found问题

  9. 极限编程(xp),iso国际标准化组织

    极限编程(xp):eXtreme Programming 极限编程-XP 四个价值:沟通 communication.简化 similicity.反馈 feedback.勇气 courage http ...

  10. Kibana6.x.x源码结构分析笔记