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;
} 

提交记录

转载于:https://www.cnblogs.com/S031602240/p/6354111.html

PAT (Advanced Level) Practise 1004 解题报告相关推荐

  1. PAT (Advanced Level) Practise:1001. A+B Format

    [题目链接] Calculate a + b and output the sum in standard format -- that is, the digits must be separate ...

  2. PAT (Advanced Level) Practise 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  3. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  4. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  5. PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)

    PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)  http://www.patest.cn/contests/pat-b-practise/1034 ...

  6. PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...

  7. PAT (Advanced Level) Practice 题解代码 - II (1051-1100)

    PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...

  8. 卡拉兹(Callatz)猜想,PAT(Basic Level) Practise NO.1001

    PAT(Basic Level) Practise NO.1001 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半. 这样 ...

  9. PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...

最新文章

  1. 2020年,计算机视觉领域会有哪些新的研究方向值得提前探索?
  2. 树莓派人脸识别门禁opencv4.2_树莓派人脸识别实际应用:人脸识别门禁
  3. Velocity知识点总结
  4. 几大科技公司在VR方面的布局是怎么样的?
  5. 乔布斯的斯坦福大学演讲:你必须要找到你所爱的东西
  6. 04_Nginx命令行参数,控制信号,Nginx启动、停止、重启命令
  7. GitHub 的前世今生
  8. dbcc dbreindex server sql_DBCC DBREINDEX重建索引提高SQL Server性能
  9. 今天才知道,MySQL 的 binlog 编号可以这么大!
  10. 【数据库】SQL查询强化篇
  11. 上海音乐学院计算机考研难吗,上海音乐学院考研难吗?一般要什么水平才可以进入?...
  12. 完美解决 Ubuntu 18.04 安装网易云音乐 不能正常点击启动问题
  13. 每天学一点Linux(一)——apt-get
  14. 【机器学习】:如何对你的数据进行分类?
  15. lbochs模拟器最新版_bochs模拟器最新版下载
  16. 学习笔记图片从本地复制到csdn博客出现:外链图片转存失败:解决方法
  17. 分享华为鲲鹏(ARM)镜像站地址
  18. C++中string类下的begin,end,rbegin,rend的用法
  19. 世界三大质量奖项介绍---波多里奇奖(转载)
  20. 梦里花落知多少,网络抖动逃不了

热门文章

  1. [唐诗]入朝洛堤步月-上官仪
  2. web前端研发工程师编程能力成长之路
  3. linux 根目录下的子目录的意义
  4. Cisco交换机路由器的部分命令解析(3)
  5. Nginx访问VM虚拟机CentOS 7系统与本地Windows系统共享目录403
  6. JavaWeb编程(十)Json语句
  7. 【BZOJ-1864】三色二叉树 树形DP
  8. canvas绘制圆形
  9. Android SurfaceView 的应用
  10. 这样的“牛”人,绝佳客户最好能多碰上上几个是我们当程序员的好运