Round Trip
You are given an integer n representing the number of cities numbered from 1 to n, and a 2D array edges where edges[i] = [u, v] indicates that there is a two-way road between city u and city v.
Find a valid round trip that starts at any city, visits at least two other distinct cities, and returns to the start without using the same road twice in a row.
Return an array of integers representing the cities on the valid route in the order they are visited.
If multiple valid trips exist, return any of them. If none exist, return an empty array [ ].
Example 1:
Input: n = 5, edges = [[1, 3], [1, 2], [5, 3], [1, 5], [2, 4], [4, 5]]
Output: [1, 3, 5, 1]
Explanation:
The path 1 -> 3 -> 5 -> 1 is a valid cycle. Another valid output is [1, 2, 4, 5, 1].
Example 2:
Input: n = 4, edges = [[1, 2], [2, 3], [3, 4], [4, 1]]
Output: [1, 2, 3, 4, 1]
Explanation:
The graph forms a single loop. Returning any valid cycle of this loop is correct.
Now Your Turn!
Pick the correct output for the given inputInput: n = 3, edges = [[1, 2], [2, 3], [3, 1]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 105
- 0 <= edges.length <= 2 * 105
- edges[i].length == 2
- 1 <= edges[i][0], edges[i][1] <= n
- edges[i][0] != edges[i][1]
- At most one road exists between any two cities.