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 ...
随机推荐
- 乘风破浪:LeetCode真题_015_3Sum
乘风破浪:LeetCode真题_015_3Sum 一.前言 关于集合的操作,也是编程最容易考试的问题,比如求集和中的3个元素使得它们的和为0,并且要求不重复出现,这样的问题该怎么样解决呢? 二.3Su ...
- jquery实现的时间轴
代码 样式文件style.css 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- How Flask Routing Works
@How Flask Routing Works The entire idea of Flask (and the underlying Werkzeug library) is to map UR ...
- 【LGP2045】方格取数加强版
题目 还纠结了一下是费用流还是最小割 最终还是决定让最小割去死吧 我们的问题就是让一个点的点权只被计算一次 考虑拆点 将所有点拆成入点和出点,入点向出点连流量为\(1\)的边 每一个出点往下连能到达的 ...
- 【[SDOI2014]数表】
求 \[\sum_{i=1}^N\sum_{j=1}^Mσ(gcd(i,j))[σ(gcd(i,j))<=a]\] \(σ\)表示约数和函数 感觉非常难求的样子 先把套路搞出来 \[f(n)=\ ...
- P1081 开车旅行
题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...
- chrome下载离线安装包的方法
https://www.google.com/chrome/browser/desktop/index.html?system=true&standalone=1,一般默认下载稳定版,如果需要 ...
- Cookies、sessionStorage和localStorage解释及区别?
浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务器端进行数据交互 一.cookie和session Cookie和 session都是用来跟踪浏览 ...
- robotframwork接口测试(三)—HttpLibrary.HTTP安装
1. 安装: 1)可以用命令:pip install robotframework-httplibrary(或pip install --upgrade robotframework-httplib ...
- 监听器中spring注入相关的问题
问题描述: 需求是要求在项目启动自动触发一个service中的线程的操作,使用监听器来实现,但是自定义监听器中spring注解service失败,通过WebApplicationContextUtil ...