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 ...
随机推荐
- HTML学习---基础知识学习
1.1. HTML 1.为什么要有HTML? "Hello" "<h1>Hello</h1>" - 浏览器渲染时使用一套HTML规则, ...
- Git Hub 使用手册参考
参考信息 1.http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0002.http://ww ...
- kafka概要设计
Kafka核心功能 即:高性能的消息发送与高性能的消息消费 下载安装包后即可启动Kafka服务器,但是此前需要首先启动Zookeeper服务器,Zookeeper是为Kafka提供协调服务的工具,Ka ...
- python操作mysql二
游标 游标是一种能从包括多条数据记录的结果集中每次提取一条记录的机制,游标充当指针的作用,尽管游标能遍历结果中的所有行,但它一次只指向一行,游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行 ...
- iOS js 使用与JSContext
JSContext:js执行环境,包含了js执行时所需要的所有函数和对象: js执行时,会在执行环境搜索需要的函数然后执行,或者保存传入的变量或函数: JSContext *jsContext = [ ...
- weblogic之CVE-2016-3510反序列化分析
将反序列化的对象封装进了weblogic.corba.utils.MarshalledObject,然后再对MarshalledObject进行序列化,生成payload字节码.由于Marshalle ...
- PM2 部署 nodejs 项目
在网上找到pm2.目前似乎最常见的线上部署nodejs项目的有forever,pm2这两种.使用场合: forever管理多个站点,每个站点访问量不大,不需要监控. pm2 网站访问量比较大,需要完整 ...
- 《metasploit渗透测试魔鬼训练营》学习笔记第五章--网络服务渗透攻击
三.网络服务渗透攻击 3.1 内存攻防技术 3.1.1 缓冲区溢出漏洞机理 缓冲区溢出是程序由于缺乏对缓冲区的边界条件检查而引起的一种异常行为. ...
- JQ中$(window).load和$(document).ready区别与执行顺序
JQ中的$(document).ready()大家应该用的非常多,等同于$(function(){}),基本每个JS脚本中都有这个函数的出现有时甚至会出现多个,那么另一个加载函数$(window).l ...
- sharepoint 搜索报错
配置sharepoint 拓扑架构,将两台服务器一起来爬网. 配置如下: $hostA = Get-SPEnterpriseSearchServiceInstance -Identity " ...