标签:const scanf using iostream nod ons == ace print
这题竟然没有卡时间,有需要可以参考一下。
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 100001;
struct Node
{
vector<int> hz;
int bf;
int fm;
}a[N];
int ans[100001]; int top = -1;
int xam = -1;
void bff(int i, int n)
{
a[i].bf = n;
if (n > xam)xam = n;
for (int j = 0; j < a[i].hz.size(); j++)
{
bff(a[i].hz[j], n + 1);
}
}
int main()
{
int n, temp;
cin >> n;
for (int i = 1; i <= n; i++)
{
a[i].fm = -2;
a[i].bf = -2;
}
int s;
for (int i = 1; i <= n; i++)
{
scanf("%d", &temp);
a[i].fm = temp;
if (temp == -1) s = i;
else
a[temp].hz.push_back( i);
}
bff(s, 1);
cout << xam << endl;
for (int i = 1; i <= n; i++)
{
if (a[i].bf == xam)
{
ans[++top] = i;
}
}
for (int i = 0; i < top; i++)
{
printf("%d ", ans[i]);
}
printf("%d", ans[top]);
return 0;
}
标签:const scanf using iostream nod ons == ace print
原文地址:https://www.cnblogs.com/kreamyu/p/12585889.html