C - 電車の連結順序 / Train Car Coupling Order 解説 by admin 印刷ページ
gemini-3-flash-thinkingOverview
Given the connection information (previous and next car numbers) of \(N\) shuffled train cars, the problem asks to reconstruct the correct order from the head to the tail of the train.
Analysis
This problem can be rephrased as constructing a Linked List data structure and traversing it from the head.
The two key points are: 1. Finding the head of the train: Among the connection information, the car \(i\) where \(prev_i = 0\) is the starting point (head) of the train. 2. Fast lookup of the next car: Instead of searching for the next car \(next_i\) connected after car \(i\) each time, we need to set up an array or similar structure to look it up in \(O(1)\).
If we scan all cars every time to find the next car, it takes \(O(N)\) to find each one, resulting in \(O(N^2)\) total time. Since \(N=10^5\), this would not fit within the time limit. We can solve this problem by creating an array next_node[car number] = next car number.
Algorithm
- Storing the data:
Prepare an array
next_nodeof length \(N+1\). For each car \(i\), store the input \(next_i\) intonext_node[i]. - Identifying the head car:
While reading the input, record the car number \(i\) such that \(prev_i = 0\) into a variable
head. - Traversing the train:
Set
current = headand repeat the following operations untilcurrentbecomes \(0\) (past the tail):- Record the current car number
current. - Update
currenttonext_node[current]to move to the next car.
- Record the current car number
- Outputting the result: Output the recorded car numbers in order.
Complexity
- Time complexity: \(O(N)\) Reading the input takes \(O(N)\), and traversing from the head to the tail takes \(O(N)\), so the overall complexity is \(O(N)\).
- Space complexity: \(O(N)\)
The array
next_nodeholding the connection information and the list storing the results use \(O(N)\) memory.
Implementation Tips
Fast I/O: Since \(N\) can be as large as \(10^5\), in Python it is faster to read all input at once using
sys.stdin.read().split()rather than callinginput()repeatedly.Array indexing: Since car numbers range from \(1\) to \(N\), setting the array size to \(N+1\) allows using car numbers directly as indices, making the implementation smoother.
Source Code
import sys
def solve():
# Read all input at once for efficient processing
# sys.stdin.read().split() handles all whitespace and is generally fast in Python
input_data = sys.stdin.read().split()
if not input_data:
return
# The first value is the number of cars
n = int(input_data[0])
# next_node[i] will store the car number that is connected to the tail side of car i
# Since car numbers are from 1 to N, we use an array of size N + 1
next_node = [0] * (n + 1)
head = -1
# The input provides connection information for each car i (1 to N)
# The information for car i is located at data[2*i - 1] (prev_i) and data[2*i] (next_i)
for i in range(1, n + 1):
# prev_i: car on the head side, next_i: car on the tail side
prev_i = int(input_data[2 * i - 1])
next_i = int(input_data[2 * i])
# Map car i to its successor
next_node[i] = next_i
# If prev_i is 0, car i is the start of the train
if prev_i == 0:
head = i
# Traversal from the head car to the tail car
# We follow the next_node pointers until we reach 0 (the end of the train)
current = head
result = []
# Since it's guaranteed to be a single chain of N cars, the loop runs exactly N times
while current != 0:
result.append(str(current))
current = next_node[current]
# Output the car numbers in order, each on a new line
sys.stdout.write('\n'.join(result) + '\n')
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-thinking.
投稿日時:
最終更新: