1106D.md (2651B)
1 Source: https://codeforces.com/problemset/problem/1106/D 2 3 D. Lunar New Year and a Wander 4 5 time limit per test 6 7 3 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. 22 23 The park can be represented as a connected graph with $n$ nodes and $m$ bidirectional edges. Initially Bob is at the node $1$ and he records $1$ on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes $a_1, a_2, \ldots, a_n$ is recorded. 24 25 Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it. 26 27 A sequence $x$ is lexicographically smaller than a sequence $y$ if and only if one of the following holds: 28 29 * $x$ is a prefix of $y$, but $x \ne y$ (this is impossible in this problem as all considered sequences have the same length); 30 * in the first position where $x$ and $y$ differ, the sequence $x$ has a smaller element than the corresponding element in $y$. 31 32 Input 33 34 The first line contains two positive integers $n$ and $m$ ($1 \leq n, m \leq 10^5$), denoting the number of nodes and edges, respectively. 35 36 The following $m$ lines describe the bidirectional edges in the graph. The $i$-th of these lines contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$), representing the nodes the $i$-th edge connects. 37 38 Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected. 39 40 Output 41 42 Output a line containing the lexicographically smallest sequence $a_1, a_2, \ldots, a_n$ Bob can record. 43 44 Examples 45 46 Input 47 48 ``` 49 3 2 50 1 2 51 1 3 52 ``` 53 54 Output 55 56 ``` 57 1 2 3 58 ``` 59 60 Input 61 62 ``` 63 5 5 64 1 4 65 3 4 66 5 4 67 3 2 68 1 5 69 ``` 70 71 Output 72 73 ``` 74 1 4 3 2 5 75 ``` 76 77 Input 78 79 ``` 80 10 10 81 1 4 82 6 8 83 2 5 84 3 7 85 9 4 86 5 6 87 3 4 88 8 10 89 8 9 90 1 10 91 ``` 92 93 Output 94 95 ``` 96 1 4 3 7 9 8 6 5 2 10 97 ``` 98 99 Note 100 101 In the first sample, Bob's optimal wandering path could be $1 \rightarrow 2 \rightarrow 1 \rightarrow 3$. Therefore, Bob will obtain the sequence $\{1, 2, 3\}$, which is the lexicographically smallest one. 102 103 In the second sample, Bob's optimal wandering path could be $1 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 5$. Therefore, Bob will obtain the sequence $\{1, 4, 3, 2, 5\}$, which is the lexicographically smallest one.