参考: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的更多相关文章

  1. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  2. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  3. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  4. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  5. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  6. 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 ...

  7. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

  8. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  9. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

随机推荐

  1. Spring注解开发-全面解析常用注解使用方法之生命周期

    本文github位置:https://github.com/WillVi/Spring-Annotation/ 往期文章:Spring注解开发-全面解析常用注解使用方法之组件注册 bean生命周期 ​ ...

  2. layer 遮罩层等待

    效果 代码: js函数之前:var msg = layer.msg('努力中加载中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,of ...

  3. my target

    目前看来 任何模块 改代码不是问题target是写得一手好代码.设计模式. 语法日积月累.c++ 先看media

  4. DIY简单功能的torrentkitty种子爬虫

    过完年回公司比较无聊,一不小心看到微博里美尤莉娅的图片,惊为天人,有图为证!!! 百度之原来这货以前叫小泉彩,貌似动了几个小手术换了个马甲重新出道了.你拍AV你家里知道么?.于是乎下了几个种子看了下, ...

  5. Python新式类和经典类的区别

    @Python新式类和经典类的区别 class ClassicClass(): pass class NewStyleClass(object): pass x1 = ClassicClass() x ...

  6. 超慢速移动动画使用CSS3实现流畅效果

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVpbGVpZm9yZXZlcg==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  7. Java并发案例04---生产者消费者问题03--使用ReentrantLock

    /** * 面试题:写一个固定容量同步容器,拥有put和get方法,以及getCount方法, * 能够支持2个生产者线程以及10个消费者线程的阻塞调用 * * 使用wait和notify/notif ...

  8. 2594. [WC2006]水管局长数据加强版【LCT+最小生成树】

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  9. RedHat(小红帽)下 yum用不了的解决办法

    由于RedHat是商业版的,通常由于没有注册,导致yum程序无法使用(linux下面,yum是个安装软件的“神器”).此时可用CentOS的地址进行替换.下面将一步步说明如何处理: 一.删除RedHa ...

  10. JNI由浅入深_5_基本类型应用

    1.基本类型应用 对于JNI处理基本类型还是比较简单的,下面是Java代码: <span style="font-size:14px;"> public native ...