问题

### Instructions: You are an expert Python programmer. You will be given a question (problem specification) and will generate a correct Python program that matches the specification and passes all tests. You will NOT return anything except for the program.
### Question:
There are N people standing in a line: person 1, person 2, \ldots, person N.
You are given the arrangement of the people as a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
A _ i\ (1\leq i\leq N) represents the following information:

- if A _ i=-1, person i is at the front of the line;
- if A _ i\neq -1, person i is right behind person A _ i.

Print the people's numbers in the line from front to back.

Input

The input is given from Standard Input in the following format:
N
A _ 1 A _ 2 \ldots A _ N

Output

If person s _ 1, person s _ 2, \ldots, person s _ N are standing in the line in this order, print s _ 1, s _ 2, \ldots, and s _ N in this order, separated by spaces.

Constraints


- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 or 1\leq A _ i\leq N\ (1\leq i\leq N)
- There is exactly one way to arrange the N people consistent with the information given.
- All input values are integers.

Sample Input 1

6
4 1 -1 5 3 2

Sample Output 1

3 5 4 1 2 6

If person 3, person 5, person 4, person 1, person 2, and person 6 stand in line in this order from front to back, the arrangement matches the given information.
Indeed, it can be seen that:

- person 1 is standing right behind person 4,
- person 2 is standing right behind person 1,
- person 3 is at the front of the line,
- person 4 is standing right behind person 5,
- person 5 is standing right behind person 3, and
- person 6 is standing right behind person 2.

Thus, print 3, 5, 4, 1, 2, and 6 in this order, separated by spaces.

Sample Input 2

10
-1 1 2 3 4 5 6 7 8 9

Sample Output 2

1 2 3 4 5 6 7 8 9 10

Sample Input 3

30
3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7

Sample Output 3

10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
# YOUR CODE HERE
```

### Answer: (use the provided format with backticks)

答案

模型 模型答案 正确答案
金声玉亮2.0 ```python
import sys

def main():
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))

# Create a dictionary to store the next person for each person
next_person = {}
start = None

for i, person in enumerate(a):
if person == -1:
start = i + 1 # Person at the front of the line
else:
next_person[person] = i + 1 # Person i+1 is behind person

# Start from the person at the front and follow the chain
current = start
result = []
while current:
result.append(current)
current = next_person.get(current) # Move to the next person in line

# Print the result
print(' '.join(map(str, result)))

if __name__ == "__main__":
main()
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
deepseek-r1

```python
n = int(input())
a = list(map(int, input().split()))
front = -1
for i in range(n):
if a[i] == -1:
front = i + 1
break
next_person = [0] * (n + 1)
for i in range(n):
current_person = i + 1
ai = a[i]
if ai != -1:
next_person[ai] = current_person
res = []
current = front
for _ in range(n):
res.append(str(current))
current = next_person[current]
print(' '.join(res))
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
gpto1 ```python
# YOUR CODE HERE
import sys
sys.setrecursionlimit(1<<25)
N, *rest = map(int, sys.stdin.read().split())
A = rest[:N]
immediate_successor = [-1]*(N+1)
for i in range(1, N+1):
Ai = A[i-1]
if Ai != -1:
immediate_successor[Ai] = i
for i in range(1, N+1):
if A[i-1] == -1:
front_person = i
break
sequence = []
p = front_person
while p != -1:
sequence.append(p)
p = immediate_successor[p]
print(' '.join(map(str, sequence)))
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
gpt-4o-2024-08-06 ```python
# YOUR CODE HERE
def main():
import sys
input = sys.stdin.read
data = input().split()

N = int(data[0])
A = list(map(int, data[1:]))

# Create a list to store the order of people
order = [0] * N

# Find the person at the front of the line
front_person = A.index(-1) + 1

# Start the order with the front person
order[0] = front_person

# Fill the order list by following the sequence
for i in range(1, N):
# The next person in line is the one who is right behind the current person
order[i] = A[order[i-1] - 1]

# Print the order
print(' '.join(map(str, order)))

main()
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
DeepSeek-Coder-V2-Lite-Instruct ```python
# YOUR CODE HERE
def main():
import sys
input = sys.stdin.read
data = input().split()

N = int(data[0])
A = list(map(int, data[1:]))

# Create a list to store the order of people
order = [0] * N

# Initialize the positions of people
for i in range(N):
if A[i] == -1:
order[i] = i + 1
else:
order[A[i] - 1] = i + 1

# Reconstruct the order from front to back
result = []
current = 0
for i in range(N):
if order[i] != 0:
current = i + 1
break

while current != -1:
result.append(current)
current = A[current - 1]

print(' '.join(map(str, result)))

if __name__ == "__main__":
main()
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
Meta-Llama-3.1-405B-Instruct-Turbo ```python
import sys

def read_ints():
return list(map(int, sys.stdin.readline().split()))

def main():
n = int(sys.stdin.readline())
a = read_ints()

next_person = [-1] * n
start = -1

for i in range(n):
if a[i] == -1:
start = i
else:
next_person[a[i] - 1] = i

result = []
current = start

while current != -1:
result.append(current + 1)
current = next_person[current]

print(' '.join(map(str, result)))

if __name__ == "__main__":
main()
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
Meta-Llama-3.1-70B-Instruct-Turbo ```python
import sys

def read_input():
return sys.stdin.readline().strip()

def main():
n = int(read_input())
a = list(map(int, read_input().split()))

ans = []
for i in range(n):
if a[i] == -1:
ans.append(i + 1)
j = i
while True:
j = a[j] - 1
if j == -1:
break
ans.append(j + 1)

print(*ans)

if __name__ == "__main__":
main()
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
Qwen2-72B-Instruct ```python
from collections import deque
n = int(input())
a = list(map(int, input().split()))
ans = deque()
for i in range(n):
if a[i] == -1:
ans.appendleft(i+1)
else:
a[a[i]-1] = -1
print(*ans)
```
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
baichuan4 ```python
from collections import deque

def read_input():
n = int(input().strip())
arrangement = list(map(int, input().strip().split()))
return n, arrangement

def find_starting_point(arrangement):
for i, person in enumerate(arrangement):
if person == -1:
return i
return None

def reconstruct_line(n, arrangement):
start_index = find_starting_point(arrangement)
if start_index is None:
raise ValueError("No starting point found")

line = deque()
for i in range(start_index, start_index + n):
line.append(i % n + 1)

for i in range(n):
if arrangement[i] != -1:
prev_person = arrangement[i]
next_person = line.popleft()
line.append(next_person)
while line[0] != prev_person:
next_person = line.popleft()
line.append(next_person)

return list(line)

def main():
n, arrangement = read_input()
line = reconstruct_line(n, arrangement)
print(' '.join(map(str,



if __name__ == "__main__":
main()
```

This Python program reads the number of people `N` and their arrangement from standard input, reconstructs the line based on the given arrangement, and prints the resulting order of people from front to back. It uses a deque to efficiently rearrange the people according to the specified rules.
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
doubao ```python
N = int(input())
A = list(map(int, input().split()))

line = []
for i in range(1, N + 1):
if A[i - 1] == -1:
line.append(i)
break

while len(line) < N:
for i in range(1, N + 1):
if i in line:
continue
if A[i - 1] == line[-1]:
line.append(i)

for num in line:
print(num, end=" ")
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
gpto1 ```python
# YOUR CODE HERE
import sys
sys.setrecursionlimit(1<<25)
N, *rest = map(int, sys.stdin.read().split())
A = rest[:N]
immediate_successor = [-1]*(N+1)
for i in range(1, N+1):
Ai = A[i-1]
if Ai != -1:
immediate_successor[Ai] = i
for i in range(1, N+1):
if A[i-1] == -1:
front_person = i
break
sequence = []
p = front_person
while p != -1:
sequence.append(p)
p = immediate_successor[p]
print(' '.join(map(str, sequence)))
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
kimi ```python
# YOUR CODE HERE
from collections import deque
N = int(input())
A = list(map(int, input().split()))
ans = []
d = deque()
for i in range(N):
if A[i] == -1:
d.append(i + 1)
else:
A[A[i] - 1].append(i + 1)
while d:
temp = d.popleft()
while temp:
ans.append(temp)
temp = A[temp - 1].pop(0) if A[temp - 1] else 0
print(*ans)
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
minimax ```python
# YOUR CODE HERE
from collections import deque

def reconstruct_line(N, A):
# Create a queue to store the indices of people who are not yet placed in the line
queue = deque()
# Create a list to store the final order of people in the line
line_order = [0] * N

# Add all people who are at the front of the line to the queue
for i in range(N):
if A[i] == -1:
queue.append(i)

# Reconstruct the line order
for i in range(N):
# Take the person at the front of the queue
person = queue.popleft()
# Place them in the line order
line_order[i] = person + 1 # Adding 1 because the problem statement uses 1-based indexing

# If the person is not at the front, add the person they are behind to the queue
if person != A[person]:
queue.append(A[person])

return line_order

# Read the inputs from stdin
N = int(input())
A = list(map(int, input().split()))

# Solve the problem and write the answer to stdout
print(' '.join(map
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
wenxinyiyan ```python
# Read the number of people
N = int(input())

# Read the arrangement of people
A = list(map(int, input().split()))

# Initialize an empty list to store the order of people in the line
order = []

# Initialize a visited array to keep track of visited people
visited = [False] * (N + 1)

# Define a recursive function to find the order of people in the line
def find_order(person):
# Mark the current person as visited
visited[person] = True

# If the current person is at the front of the line, append them to the order list
if A[person - 1] == -1:
order.append(person)
else:
# Find the person who is standing right behind the current person
next_person = A[person - 1]

# If the next person has not been visited, recursively find their order
if not visited[next_person]:
find_order(next_person)

# Append the current person to the order list
order.append(person)

# Find the order of people starting from person 1
find_order(1)

# Append the remaining unvisited people to the order list (they must be at the end of the line)
for i in range(1, N + 1):
if not visited[i]:
order.append(i)

# Print the order of people in the line
print(' '.join(map(str, order)))
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
zhipu ```python
import sys
input = sys.stdin.read

def main():
data = input().split()
n = int(data[0])
a = list(map(int, data[1:]))

# This will hold the final order of people
order = []

# This will keep track of the next person in line for each person
next_person = [0] * n

# Find the person at the front of the line
front_person = -1
for i in range(n):
if a[i] == -1:
front_person = i
else:
next_person[a[i] - 1] = i

# Traverse the line starting from the front person
current_person = front_person
while current_person != -1:
order.append(current_person + 1)
current_person = next_person[current_person]

# Print the order of people
print(" ".join(map(str, order)))

if __name__ == "__main__":
main()
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]
deepseek-v3 ```python
# YOUR CODE HERE
def main():
import sys
input = sys.stdin.read
data = input().split()

N = int(data[0])
A = list(map(int, data[1:N+1]))

# Create a dictionary to map each person to the person behind them
behind = {}
for i in range(N):
person = i + 1
a_i = A[i]
if a_i != -1:
behind[a_i] = person

# Find the person at the front
front = -1
for i in range(N):
if A[i] == -1:
front = i + 1
break

# Reconstruct the line
line = []
current = front
while current in behind:
line.append(current)
current = behind[current]
line.append(current)

print(' '.join(map(str, line)))

if __name__ == "__main__":
main()
```
[{"input": "6\n4 1 -1 5 3 2\n", "output": "3 5 4 1 2 6\n", "testtype": "stdin"}, {"input": "10\n-1 1 2 3 4 5 6 7 8 9\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "testtype": "stdin"}, {"input": "30\n3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7\n", "output": "10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14\n", "testtype": "stdin"}]