After traveling to Mali, Mr. Engineer and Mrs. Doctor claim that they have visited all…
- User
After traveling to Mali, Mr. Engineer and Mrs. Doctor claim that they have visited all the cities of this country. Country finance with n n is a city in such a way that there is a two-way road between some of its cities. We also know that both cities can be reached from each other by using these roads. Mr. Engineer, who was a computer Olympiad when he was a student, used the dfs algorithm to see different cities in Mali. Pay attention, Mrs. Doctor was also one of the children of the Olympiad of the same year!
Write a program that by taking all the roads of the country of Mali and the roads that Mr. Engineer and Mrs. Doctor have crossed exactly twice in their journey (edges of the dfs tree), find the cities that may be the starting city of their journey. The roads that Mr. Engineer and Mrs. Doctor have crossed are given in arbitrary order at the entrance, not chronological order. Also, in dfs, the roads connected to each city can be seen in any order, and it is not related to the order of entry.
input The first line of input contains two natural numbers n n, the number of cities, and m m is the number of squares.
Then in each of m m Next line, two natural numbers u u and v v is said to indicate the existence of a two-way road between the two cities u u and v v is n
1 n−1 The first line shows the roads that Mr. Engineer and Mrs. Doctor have crossed.
No city has its own road, but there may be more than one road between two cities.
It is guaranteed that the graph of the edges that Mr. Engineer and Mrs. Doctor have crossed are ( n
1 n−1 first road) is a tree.
1 ≤ n ≤ 200
000 1≤n≤200 000
n
1 ≤ m ≤ 200
000 n−1≤m≤200,000
( u ≠ v ) (u (v) 1 ≤ u , v ≤ n 1≤u,v≤n
output In the only output line, print the cities that can be the starting cities of Mr. Engineer and Ms. Doctor in ascending order.
If there was no city, the number
1 Print −1.
Sub-issues Constraint score subproblem 1 10 n , m ≤ 2
000 n, m ≤ 2000 2 15 n
1 The first n−1 roads form a route. 3 15 n
m n = m 4 60 without additional restrictions Example Sample entry 1 3 3 1 2 2 3 3 1 Plain text Sample output 1 1 3 Plain text Sample entry 2 4 4 1 2 3 1 1 4 2 3 Plain text Sample output 2 2 3 Plain text Sample entry 3 4 6 1 2 1 3 1 4 2 3 3 4 4 2 Plain text Sample output 3 -1 this is a very hard question try to solve it
- Assistant
To solve this problem, we need to determine all possible starting cities (nodes) for the DFS traversal given the constraints.
Key Observations:
- A necessary condition for a node
sto be a valid starting point is that it must lie on all paths in the spanning tree between the endpoints of each non-tree edge .
- A necessary condition for a node