当前位置:网站首页>L2-026 small generation (25 points)
L2-026 small generation (25 points)
2022-06-29 10:13:00 【Momo 623】
List of articles
The title of this brush is 2021 Nian TIANTI
L2-026 Junior (25 branch )
This topic gives the genealogy of a large family , Please give a list of the youngest generation .
Input format :
Enter in the first line to give the total family population N( No more than 100 000 The positive integer ) —— Simplicity , We take family members from 1 To N Number . Then the second line gives N Number , Among them the first i The number corresponds to the i Father of member / mother . The father of the oldest ancestor in the family tree / The parent number is -1. The numbers in a row are separated by spaces .
Output format :
First of all, output the smallest generation ( The ancestors are divided into 1, The following is increasing step by step ). Then in the second line, output the number of the member with the lowest rank in ascending order . Numbers are separated by a space , There must be no extra space at the beginning and end of the line .
sample input :
9
2 6 5 5 -1 5 6 4 7
sample output :
4
1 9
Answer key
It's easier to see :
x It's the father i It's the son
need x Add son , The reason is possible x There are many sons
And then use bfs perhaps dfs Just ask
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int n = 100005;
int N;
vector<int> v[n];
// BFS seek
void bfs(int root)
{
// The first is serial number
// The minor bit is algebra
queue<pair<int, int>> q;
q.push({root, 0});
// The final answer
vector<int> ans;
// Algebra.
int maxt = 0;
while (!q.empty())
{
auto x = q.front();
q.pop();
// If a higher generation is found
// Update
if (maxt < x.second)
{
maxt = x.second;
ans.clear();
}
ans.push_back(x.first);
for (auto &e : v[x.first])
q.push({e, x.second + 1});
}
cout << maxt + 1 << endl;
int f = 0;
for (auto &e : ans)
{
if (f++ != 0)
cout << " ";
cout << e;
}
}
int main()
{
cin >> N;
int root = 0;
int x;
for (int i = 1; i <= N; i++)
{
cin >> x;
if (x == -1)
{
root = i;
continue;
}
v[x].push_back(i);
}
bfs(root);
return 0;
}
边栏推荐
猜你喜欢

十六制计数器和流水灯

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)

Rikka with Cake(线段树+线段树)

Container of the basic component of the flutter

HDU 6778 car (group enumeration -- > shape pressure DP)

Using rancher to build kubernetes cluster

力扣94二叉树的中序遍历

Flutter 基础组件之 Text

The stones game

自定义控件之侧滑关闭 Activity 控件
随机推荐
Summary of PHP memory horse technology research and killing methods
在Activity外使用startActivity()方法报错原因与解决办法
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
Nacos registry cluster
XML布局中Button总是在最上层显示
Codeforces - 1151b thinking
Recyclerview refreshes blinks and crashes when deleting items
Wandering --- 最后的编程挑战
L1-009 N个数求和 (20 分)
JVM之方法返回地址
JVM之虚拟机栈之动态链接
manacher
2019.10.23训练总结
L2-3 这是二叉搜索树吗?-题解超精彩哦
CodeForces - 1151B 思维
2019.10.30学习总结
图片验证码控件
Codeforces Round #657 Div. 2
Time varying and non time varying
Cisco ASA、FTD和HyperFlex HX的漏洞分析复现