PAT甲级 1004.Counting Leaves
参考:https://blog.csdn.net/qq278672818/article/details/54915636
首先贴上我一开始的部分正确代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+;
struct node
{
int level,child;//level为该节点层数,child为该节点孩子数
node()
{
level=;
child=;
}
}no[N];
int n,m;
int ans[N];//ans【i】为第i层叶子节点数
int cmp(struct node x,struct node y)
{
return x.level<y.level;
}
int main()
{
cin>>n;
if (n==)
return ;
cin>>m;
no[].level=;
int k,id,child,maxlevel=;//maxlevel为最大层数
for (int i=;i<m;i++)
{
cin>>id>>k;
no[id].child=k;
for (int i=;i<k;i++)
{
cin>>child;
no[child].level=no[id].level+;
maxlevel=max(maxlevel,no[child].level);
}
}
sort(no+,no++n,cmp);//按层数排序
memset(ans,,sizeof(ans));
for (int i=;i<=n;i++)
{
if (no[i].child==)
{
ans[no[i].level ]++;
}
}
cout<<ans[];
for (int i=;i<=maxlevel;i++)
cout<<" "<<ans[i];
cout<<endl; return ;
}
经参考了上边的参考链接后发现:若有测试点是无序的,则该解法错误,因为节点的层数设置将不正确。

再贴上AC代码:
#include<bits/stdc++.h>
using namespace std;
vector<int> ve[];
int n,m,maxlevel=;//maxleve记录树的最大层数
int ans[];//ans[i]保存第i层的叶子节点数
void dfs(int level,int id)//level为当前层数,id为当前节点编号
{
if (ve[id].size()==)//找到叶子节点
{
maxlevel=max(maxlevel,level);
ans[level]++;
return;
}
for (int i=;i<ve[id].size();i++)//递归遍历孩子节点
{
dfs(level+,ve[id][i]);
}
}
int main()
{
cin>>n;
if (n==)
return ;
cin>>m;
int id,k,child;
for (int i=;i<m;i++)
{
cin>>id>>k;
for (int j=;j<k;j++)
{
cin>>child;
ve[id].push_back(child);
}
}
memset(ans,,sizeof(ans));
dfs(,);
cout<<ans[];
for (int i=;i<=maxlevel;i++)
{
cout<<" "<<ans[i];
}
cout<<endl; return ;
}
PAT甲级 1004.Counting Leaves的更多相关文章
- PAT 甲级 1004 Counting Leaves
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- PAT甲级——A1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]
题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
随机推荐
- 连续支付的年金(continuously payable annuity)
一.含义 假设连续不断地付款,但每年的付款总量仍然为1元. 二. 连续支付年金是年支付次数m趋于无穷大时的年金,故 连续支付年金与基本年金的关系: 连续支付,每年的支付总量为1,支付期限为无穷: 积累 ...
- css文字闪烁效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- PostgreSQL 连接的问题
一.在postgresql的安装文件夹\8.3\data\pg_hba.conf里面(或者在开始菜单程序下面的postgresql的配置文档)找到“# IPv4 local connections:” ...
- python中基于descriptor的一些概念(上)
@python中基于descriptor的一些概念(上) python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2 ...
- gluoncv 训练自己的数据集,进行目标检测
跑了一晚上的模型,实在占GPU资源,这两天已经有很多小朋友说我了.我选择了其中一个参数. https://github.com/dmlc/gluon-cv/blob/master/scripts/de ...
- 【[SDOI2008]Sandy的卡片】
被\(mhr\)的暴力干翻了 这道题做法还是非常好想的 先做一遍差分,在每个串的某尾插入一个特殊字符,再将所有的串拼接在一起 现在的问题就转化为找到一个最长的公共子串使得其出现了\(n\)次,但是在一 ...
- [Python 网络编程] TCP Client (四)
TCP Client 客户端编程步骤: 创建socket对象 连接到服务端的ip和port,connect()方法 传输数据 使用send.recv方法发送.接收数据 关闭连接,释放资源 最简单的客户 ...
- SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成增删改查逻辑
习惯使用jpa操作对象的方式,现在用mybatis有点不习惯. 其实是懒得写SQL,增删改查那么简单的事情你帮我做了呗,mybatis:NO. 没办法,自己搞喽! 这里主要是实现了通过代码自动生成my ...
- Dubbo实践(十六)集群容错
Dubbo作为一个分布式的服务治理框架,提供了集群部署,路由,软负载均衡及容错机制.下图描述了Dubbo调用过程中的对于集群,负载等的调用关系: 集群 Cluster 将Directory中的多个In ...
- PAT——1042. 字符统计
请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过1000的字符串.字符串由ASCII码表中任意可见字符及空格组成,至少包含1个英文字母,以回车结束( ...