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. Jmeter测试demo

    复制代码,保存为.jmx文件 需要安装插件: JMeterPlugins-ExtrasLibs E:\软件\apache-jmeter-3.0\lib\ext <?xml version=&qu ...

  2. HDU 1695 GCD (莫比乌斯反演模板)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. redis性能监控(一): Redis Info 命令 - 获取 Redis 服务器的各种信息和统计数值

    https://blog.csdn.net/wmx3ng/article/details/82772891 https://blog.csdn.net/lxpbs8851/article/detail ...

  4. Zookeeper(一)CentOS7.5搭建Zookeeper3.4.12集群与命令行操作

    一. 分布式安装部署 1.0 下载地址 官网首页: https://zookeeper.apache.org/ 下载地址: http://mirror.bit.edu.cn/apache/zookee ...

  5. 替罪羊树&&非旋treap

    题解: 替罪羊树的模板和splay差距还是比较大的.. 按照我的splay的写法 真是都是问题.. 替罪羊树就是暴力的搞 当某颗子树大小大于这棵树的alpha时 就退出 另外删除的时候打懒标记删除 当 ...

  6. James Munkres Topology: Sec 37 Exer 1

    Exercise 1. Let \(X\) be a space. Let \(\mathcal{D}\) be a collection of subsets of \(X\) that is ma ...

  7. ionic2程序调试

    新手一枚,之前一直做.net开发,最近接触Ionic2,也没有人带,只能自己一点点抠文档,查资料.一直苦于无法直接调试打包发不好的app,只能在代码里面加上alert一点一点的抛出要看信息,感觉就像瞎 ...

  8. Flink--Window apply

    和window的操作类似,只不过操作更加灵活,具体的操作需要在匿名内部类的方法中实现:当有比较复杂的需求时候,可以使用: object WindowApply { def main(args: Arr ...

  9. 网页安全政策"(Content Security Policy,缩写 CSP)

    作者:阿里聚安全链接:https://www.zhihu.com/question/21979782/answer/122682029来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  10. oracle数据入库出现空格问题

    java做数据入库,不知为什么报如下图错误: debug发现数据是两位,如(FB),但是入库后发现FB后面多了两个空格,检查表发现类型声明是char(4),上网百度,说是char类型会自动补足.参考h ...