PAT (Advanced Level) Practise 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 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 解题报告的更多相关文章
- PAT (Advanced Level) Practise 1002 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...
- PAT (Advanced Level) Practise 1003 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题 ...
- PAT (Advanced Level) Practise 1001 解题报告
GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise
题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...
随机推荐
- Max Sum (dp)
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...
- 【sqli-labs】Less1~Less4
学习sql注入啦,一下都是我做sqli-labs时的笔记.可能有错误,如果有人发现了欢迎指正~~ 常用知识点: 1.mysql注释有三种:① #: 注释从#到行尾 ② --空格: 注释到行尾,注意-- ...
- poj1094 恶心题,,每次加边进行判断
/* 给定一组偏序关系,问最少第几步能确定次序 如果出现环,问第几步出现环 因为要求第几步确定次序或者第几步出现环,所以每次读入一个偏序关系就进行一次拓扑排序 */ #include <iost ...
- 第五周学习总结-HTML5
2018年8月12日 暑假第五周,我把HTML剩余的一些标签和用法看完了并学了一些HTML5的标签及用法. HTML5比HTML多了一些元素,也删去了一些元素. HTML5新增元素 图形元素 < ...
- Allegro PCB Design GXL (legacy) 从dxf文件中导入板框
Allegro PCB Design GXL (legacy) version 16.6-2015 新建brd文件,并设置好相应的参数之后,点击菜单:File > Import > DXF ...
- NPOI操作Excel(一)--NPOI基础
用C#读取Excel的方法有很多中,由于近期工作需要,需要解析的Excel含有合并单元格以及背景色等特殊要求,故在网上查了一些关于读Excel的方法的优缺点,觉得NPOI能满足我的需要,所以搜索了一些 ...
- 数据增强(每10度进行旋转,进行一次增强,然后对每张图片进行扩充10张patch,最后得到原始图片数*37*10数量的图片)
# -*- coding: utf-8 -*-"""Fourmi Editor This is a temporary script file.""& ...
- 蓝桥杯第十届真题B组(2019年)
2019年第十届蓝桥杯大赛软件类省赛C/C++大学B组# 试题 A:组队# 本题总分:5分[问题描述]作为篮球队教练,你需要从以下名单中选出 1号位至 5号位各一名球员,组成球队的首发阵容.每位球员担 ...
- HDU 1452 Happy 2004(因数和+费马小定理+积性函数)
Happy 2004 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- C#通讯录——Windows Form Contact List
C#通讯录 Windows Form Contact List 主窗口 using System; using System.Collections.Generic; using System.Com ...