#C9943. Restore the Subway Loop
Restore the Subway Loop
Restore the Subway Loop
In a bustling city, a new subway system has been established where the stations were originally connected in a circular loop. Due to maintenance work, one station had to be closed, causing the loop to break into a simple path. The city's planners now need to identify the two end stations of the remaining path and reconnect them to the closed station to restore the original circular loop.
Given an integer \(n\) representing the total number of stations and a list of connections between stations that form a path (each connection linking two stations), your task is to compute the two stations that currently have only one connection (i.e., the endpoints of the path). These endpoints are the stations to which the closed station should be reconnected.
Note: It is guaranteed that exactly two stations will have a degree of 1, and reconnecting these stations with the closed one will restore the cycle.
Example:
Input: 4 3 1 2 2 3 3 4</p>Output: 1 4
inputFormat
The input is given via standard input and consists of multiple lines:
- The first line contains two integers \(n\) and \(m\) separated by a space, where \(n\) is the number of stations and \(m\) is the number of connections.
- The following \(m\) lines each contain two integers \(u\) and \(v\), representing a direct connection between station \(u\) and station \(v\).
You can assume that station indices are from 1 to \(n\) and that exactly two stations will have a degree of 1.
outputFormat
Output the two endpoints (the stations with exactly one connection) separated by a space. The endpoints should be printed in increasing order.
## sample4 3
1 2
2 3
3 4
1 4