#C4421. Reconstruct the Railway Journey
Reconstruct the Railway Journey
Reconstruct the Railway Journey
You are given a list of railway tickets, each represented as a pair of strings (source, destination). These tickets form a unique journey. Your task is to reconstruct the journey in the correct order.
It is guaranteed that there exists a unique starting station which does not appear as a destination. Formally, if we define a mapping \( f: S \to D \) from source stations to destination stations using the given tickets, the starting station is the unique station \( s \) such that \( s \notin f(S) \).
Print the complete journey as a space-separated sequence of station names.
inputFormat
The input is read from standard input (stdin) and is in the following format:
n source1 destination1 source2 destination2 ... source_n destination_n
Here, n
denotes the number of tickets, and each of the following n
lines contains two non-empty strings representing the source and destination respectively.
outputFormat
Print the reconstructed journey as a single line to standard output (stdout), where the station names are separated by a single space.
## sample4
Mumbai Pune
Pune Bangalore
Bangalore Chennai
Chennai Hyderabad
Mumbai Pune Bangalore Chennai Hyderabad