Description
Glossary
Please see the course slides for the definition of Depth-First Search and Breadth-First Search. Note that for the case of BFS, the order is valid as long as we choose an unvisited vertex closest to the starting vertex every time.
A valid DFS/BFS sequence is a sequence s, such that there exists a DFS/BFS search order, where si is the i-th vertex added into the DFS/BFS tree.
A sequence s is lexicographically smaller than s′ if there exists a index t, such that and st < s′t.
Input
The first line of the input contains two integers N and M, denoting the number of cities and the number of roads in the ADA Kingdom.
The next M lines describes the roads connecting the cities. The i-th line contains two integers ui and vi, denoting a bidirectional road connecting city ui and vi.
- 1 ≤ N ≤ 105
- 1 ≤ M ≤ 2 × 105
- 1 ≤ ui,vi ≤ N,∀1 ≤ i ≤ M
- ui ̸= vi,∀1 ≤ i ≤ M
- You can reach any city from any city using some of the roads.
- Every pair of cities is directly connected by at most one edge.
Test Group 0 (0 %)
- Sample Input
Test Group 2 (75 %)
- No Additional Constraint Output
Test Group 1 (25 %)
- N ≤ 1000
- M ≤ 2000
Please output two lines. The first line contains the most attractive DFS trip plan starting from the capital. The second line contains the most attractive BFS trip plan starting from the capital.
Sample Input 1 Sample Input 2
5 7
5 4
5 4
1 2
3 5
- 4
- 3
- 3
- 1
- 5
1 4
- 4
- 2
Sample Output 1
Sample Output 2
1 2 5 4 3 1 4 2 3 5
1 2 4 3 5 1 4 5 2 3
Hint
- bidirectional = undirected ̸= unidirectional
- Make sure you understand the sample input/outputs before coding.
- STL is your good firend. For example:
- You can use std::vector to store the graph (adjacency list).
- You can use std::sort to sort an array/vector/etc.




