当前位置:网站首页>L2-026 小字辈 (25 分)
L2-026 小字辈 (25 分)
2022-04-21 08:39:00 【Sss_xxh、】
L2-026 小字辈 (25 分)
原题连接
题意
找最底层的叶子节点

思路
根据题意,输入一个数组a[i],第i个数的父亲是a[i],祖宗节点的父亲是-1,那么就可以根据这种方案建图。i为父节点,a[i]为子节点。
坑点
暂无
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N];
int res[N];
int n;
vector<int> g[N];
void dfs(int u, int cnt) //分别记录当前到那个点和第几层
{
res[u] = cnt;//标记第几层
for (int i = 0; i < g[u].size(); i ++ )
{
int j = g[u][i];
dfs(j, cnt + 1);
}
}
int main(int argc, char const *argv[])
{
int h;
cin >> n;
for (int i = 1; i <= n; i ++ )
{
cin >> a[i];
if (a[i] == -1) //确定祖宗节点
{
h = i;
continue;
}
g[a[i]].push_back(i); //a[i]的子节点有i
}
dfs(h, 1);//从祖宗节点开始往下遍历
vector<int> ans;
int maxn = -0x3f3f3f3f;//找最大值
for (int i = 1; i <= n; i ++ )
{
if (res[i] == maxn)//一样的就直接加入
{
ans.push_back(i);
}
else if (res[i] > maxn)//如果更新了最大值,需要清空,再加入
{
maxn = res[i];
ans.clear();
ans.push_back(i);
}
}
cout << maxn << endl;
for (int i = 0; i < ans.size(); i ++ )
{
cout << ans[i];
if (i != ans.size() - 1) cout << " ";
}
return 0;
}
版权声明
本文为[Sss_xxh、]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_34682765/article/details/124296830
边栏推荐
- Power grid enterprise standard B interface access record (II): resource reporting
- ffmpeg整合sdl2实现纹理渲染随机出现的方块
- Notice on printing and distributing the administrative measures for the first edition of software product certification in Hunan Province
- Power grid enterprise standard B interface access record (I): Registration
- What is paternity and diamond inheritance
- Explain in simple terms the optimization of ext4 block and inode distributor (Part 2)
- SIP语音环境中十大经典问题及解决办法
- knn之交叉验证,网格搜索
- Opencv图像处理之形态学操作
- State Grid Enterprise standard B interface record (attachment): address code of video monitoring system
猜你喜欢

Power grid enterprise standard B interface access record (II): resource reporting

神经网络学习之Opencv使用记录

Theme model of image

MES and ERP need to be integrated to play a key role

卷积神经网络中二维卷积核与三维卷积核有什么区别?

【MySQL】基于Linux-CentOS7.9的详细安装教程

Experiment 1: basic operation of database

JVM——》常用参数

渗透实战-挖掘某学校站点漏洞(APP漏洞)

JS原型与原型链
随机推荐
Notice on organizing the application of the first set of technical equipment and key core parts project in Shandong Province in 2022
Webapi (VI) - BOM
php的urldecode无法还原出原来的url
深入浅出 Ext4 块和 Inode 分配器的优化(下)
Notice on printing and distributing the administrative measures for the first edition of software product certification in Hunan Province
关于印发《湖南省首版次软件产品认定管理办法》的通知
JVM——》吞吐量
JVM——》CMS
Leetcode0824. Goat Latin (simple, string processing)
User authentication center of micro service
一定要看,MES选型九步法,值得收藏反复观看(下)
php使用正则时提示unknown modifier )
Analysis of VoIP technology of network telephone
State Grid Enterprise standard B interface record (attachment): address code of video monitoring system
MySQL error of Navicat connection under Linux access denied for user 'root' @ 'xxx XXX. XXX. XXX‘ (USING PASSWORD: YES
【MySQL】基于Linux-CentOS7.9的详细安装教程
【ARM汇编判断】如何用汇编判断数组中正负数个数?
Enter four integers in descending order
7.4 并行卷积神经网络 GoogleNet
【Appium】使用模拟器实现有道云App的业务功能-新增、搜索、修改、删除