简简单单dfs
#include <iostream>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 100010;
vector<vector<int>> Node(maxn);
int s;
vector<int> ans;
int max_index, max_height = 0;
void dfs(int index,int height)
{
if(Node[index].size() == 0)
{
if(height == max_height)
{
ans.push_back(index);
}
else if(height > max_height)
{
ans.clear();
ans.push_back(index);
max_height = height;
}
}
for(int i = 0; i < Node[index].size(); i++)
{
dfs(Node[index][i], height + 1);
}
}
int main()
{
int n,x;
cin >> n;
ans.resize(n);
for(int i = 1; i <= n; i++)
{
cin >> x;
if(x != -1) Node[x].push_back(i);
else s = i;
}
dfs(s,1);
sort(ans.begin(),ans.end());
cout << max_height << endl;
cout << ans[0];
for(int i = 1; i < ans.size(); i++)
{
cout << " " << ans[i];
}
return 0;
}