GitHub

markdownPDF


问题描述

  1. 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 ID1 ID2 ... 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<iostream>
#include<cstdio>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k,n,m,s,t,a[101]={0},b[101]={0},c[102]={0};
cin>>n>>m;
for (i=0;i<m;i++)
{
cin>>s>>t;
b[s]=1;
for (j=0;j<t;j++)
{
cin>>k;
a[k]=s;
}
}
s=0;
for (i=1;i<=n;i++)
if (b[i]==0)
{
t=a[i];
k=1;
while (t>0)
{
k++;
t=a[t];
}
c[k]++;
if (k>s) s=k;
}
cout<<c[1];
for (i=2;i<=s;i++) cout<<' '<<c[i];
return 0;
}

提交记录

PAT (Advanced Level) Practise 1004 解题报告的更多相关文章

  1. PAT (Advanced Level) Practise 1002 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...

  2. PAT (Advanced Level) Practise 1003 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题 ...

  3. PAT (Advanced Level) Practise 1001 解题报告

    GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...

  4. PAT (Advanced Level) Practise - 1094. The Largest Generation (25)

    http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...

  5. 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...

  6. 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...

  7. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

  8. 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise

    题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...

  9. 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

    题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...

随机推荐

  1. Linux 用户切换、修改用户名、修改密码

    一.用户切换 "$":普通用户提示符 "#":root用户提示符 1.普通用户到root: 方式一:命令:su然后输入root密码 此种方式只是切换了root身 ...

  2. 【python】python版本升级2.6.6到2.7.3(CentOS release 6.2)

    一. 升级python到2.7.3 wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz tar -zxvf Python-2.7. ...

  3. Python实战二

    要求:按照要求完成对文件的增.删.改.查操作. def add(**kwargs): '''新增内容,在指定位置新增''' while True: flag = False with open(&qu ...

  4. MAKEWORD 宏(macro)

    先看看Microsoft给出的关于MAKEWORD的参考: 从Microsoft给出的参考可以得知,宏MAKEWORD的作用是用于创建一个由bHigh和bLow组成的WORD类型的值. 其中bLow是 ...

  5. window下tomcat的内存溢出问题

    打开注册表:https://jingyan.baidu.com/article/49ad8bce09d6085835d8fa63.html Tomcat 内存溢出对应解决方式 Windows平台,使用 ...

  6. jQuery核心方法

    1.$(document.body).css( "background", "black" ); 2.$(myForm.elements).hide():隐藏表 ...

  7. linux操作系统中的netstat命令查看端口状态的使用和window操作系统查看端口号

    1:linux操作系统 netstat 命令用于显示各种网络相关信息,即网络状态.而我主要使用netstat查看端口号是否启动: 参数详情: 1 -a (all)显示所有选项,默认不显示LISTEN相 ...

  8. [转] mongoose学习笔记(超详细)

    名词解释 Schema: 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model: 由Schema编译而成的假想(fancy)构造器,具有抽象属性和行为.Model的每一个实例(ins ...

  9. [转] Lodash常用API笔记

    原生用法 直接使用的API _.reject 根据条件去除某个元素. var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name ...

  10. javascript var变量删除

    var有三种声明的情形: var声明的全局变量 var在函数范围内声明的局部变量 eval中声明的全局变量. 首先, 1.2种情形var声明的变量是无法删除的. 尽管var声明的全局变量是属于wind ...