官方题解
2025-11-03 10:08:13
发布于:浙江
11阅读
0回复
0点赞
题目大意
给定 个人的回答 ,表示第 个人站在第 个人的前面。
解题思路
定义 数组记录每个人的后一个是谁,单独记录排头,通过循环依次输出所有人的编号。
参考代码
#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int nxt[N];
int main(){
int n;cin>>n;
int rt=-1;
for(int i=1;i<=n;i++){
int x;cin>>x;
if(x==-1) rt=i;
else nxt[x]=i;
}
for(int i=rt;i;i=nxt[i]){
cout<<i<<" ";
}
return 0;
}
这里空空如也






有帮助,赞一个