参考: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. 数据链路层 点对点协议 PPP

    点对点协议 PPP 一. PPP 协议应满足的需求 简单.提供不可靠的数据报服务,比IP协议简单,不需要纠错,不需要序号,不需要流量控制. 工作方式:接收方每收到一个帧就进行CRC校验,如正确就接受该 ...

  2. 使用NPOI 2.1.1读取EXCEL2003/2007返回DataTable

    winform中打开excel的筛选器设置为:openFileDialog.Filter = "Excel 文件(*.xls)|*.xls;*.xlsx"; 一,不借助插件读取Ex ...

  3. 关于eclipse没有js、xml代码提示的解决:下载一个插件

    1)eclipse打开帮助 2)Eclipse Marketplace,然后搜索AngularJS Eclipse 安装后重启就行了 xml的搜索Rinzo. 没有vpn,我的网络到达不了.

  4. zeromq 笔记

    一. 当执行zmq_bind后会进入mute state,直到有进入或者出去的连接发生才会进入ready state 在mute state状态下会根据不同的套接字类型决定是丢弃消息还是阻塞 可参考z ...

  5. IOS 对JSON解析的要求

    JOSN格式的原始字符串中, 键名必须为 引号 “” 包含的字符串,值必须是数组("[]" 用中括号包起来的部分),字典("{}" 用中括号包起来的部分),数字 ...

  6. eclipse 自动生成get/set方法

    Shift+Alt+S 会弹出一个对话框 选择Generate Getters and Setters

  7. POJ3304 Segments

    嘟嘟嘟 题面就不说了,网上都有. 刚开始理解成了只要有不孤立的线段就算合法,结果就不会了--然而题中要求是所有线段至少有一个交点. 其实想一想就知道,问题转化为了是否存在一条直线和所有线段都有交点. ...

  8. 3、Android-全局大喇叭-广播机制

    所谓的官博机制可以理解成为1对多的概念 即一个喇叭所有的人都能听到(统一范围内) 为了便于及逆行系统级别的消息通知 Android引入了一套广播机制 而且更容易进行实现. 3.1.广播机制的简介 再A ...

  9. logback将日志写入不同文件夹里

    转载:logback不同业务的日志打印到不同文件 一.logback.xml文件配置如下: <?xml version="1.0" encoding="UTF-8& ...

  10. yii2数据库简单操作

    1.简单查询: one(): 根据查询结果返回查询的第一条记录. all(): 根据查询结果返回所有记录. count(): 返回记录的数量. sum(): 返回指定列的总数. average():  ...