CF1106D.Lunar New Year and a Wander

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.

The park can be represented as a connected graph with nn nodes and mm bidirectional edges. Initially Bob is at the node 11 and he records 11 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 a1,a2,…,ana_1, a_2, \ldots, a_n is recorded.

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.

A sequence xx is lexicographically smaller than a sequence yy if and only if one of the following holds:

  • xx is a prefix of yy, but x≠yx \ne y (this is impossible in this problem as all considered sequences have the same length);
  • in the first position where xx and yy differ, the sequence xx has a smaller element than the corresponding element in yy.

农历新年即将来临,Bob 决定在附近的一个公园里散步。

该公园可被表示为一个包含 nn 个节点和 mm 条双向边的连通图。初始时,Bob 位于节点 11,并在笔记本上记录下 11。他可以通过这些双向边从一个节点移动到另一个节点。每当他访问一个尚未被记录在笔记本上的节点时,他便会将其记录下来。当他至少访问过所有节点一次后,便停止散步,最终在笔记本上得到一个节点的排列 a1,a2,…,ana_1, a_2, \ldots, a_n。

散步是一件无聊的事,但解题却令人着迷。Bob 想知道:他在散步过程中所能记录下的字典序最小的节点序列是什么?Bob 认为这个问题很简单,因此希望你来解答它。

序列 xx 的字典序小于序列 yy,当且仅当满足以下条件之一:

  • xx 是 yy 的前缀,但 x≠yx \ne y(本题中不可能出现,因为所有考虑的序列长度均相同);
  • 在 xx 和 yy 首次出现差异的位置上,xx 中对应位置的元素比 yy 中对应位置的元素更小。

输入格式

The first line contains two positive integers nn and mm (1≤n,m≤1051 \leq n, m \leq 10^5), denoting the number of nodes and edges, respectively.

The following mm lines describe the bidirectional edges in the graph. The ii-th of these lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), representing the nodes the ii-th edge connects.

Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.

第一行包含两个正整数 nn 和 mm(1≤n,m≤1051 \leq n, m \leq 10^5),分别表示节点数和边数。

接下来的 mm 行描述图中的无向边。其中第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n),表示第 ii 条边所连接的两个节点。

注意:图中可能存在连接相同两个节点的多条边(重边)以及自环。保证该图是连通的。

输出格式

Output a line containing the lexicographically smallest sequence a1,a2,…,ana_1, a_2, \ldots, a_n Bob can record.

输出一行,包含鲍勃可以记录的字典序最小的序列 a1,a2,…,ana_1, a_2, \ldots, a_n。

输入输出样例

  • 输入#1

    3 2
    1 2
    1 3

    输出#1

    1 2 3
  • 输入#2

    5 5
    1 4
    3 4
    5 4
    3 2
    1 5

    输出#2

    1 4 3 2 5
  • 输入#3

    10 10
    1 4
    6 8
    2 5
    3 7
    9 4
    5 6
    3 4
    8 10
    8 9
    1 10

    输出#3

    1 4 3 7 9 8 6 5 2 10

说明/提示

In the first sample, Bob's optimal wandering path could be 1→2→1→31 \rightarrow 2 \rightarrow 1 \rightarrow 3. Therefore, Bob will obtain the sequence 1,2,3{1, 2, 3}, which is the lexicographically smallest one.

In the second sample, Bob's optimal wandering path could be 1→4→3→2→3→4→1→51 \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{1, 4, 3, 2, 5}, which is the lexicographically smallest one.

在第一个样例中,Bob 的最优游走路径可以是 1→2→1→31 \rightarrow 2 \rightarrow 1 \rightarrow 3。因此,Bob 将得到序列 1,2,3{1, 2, 3},这是字典序最小的序列。

在第二个样例中,Bob 的最优游走路径可以是 1→4→3→2→3→4→1→51 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 5。因此,Bob 将得到序列 1,4,3,2,5{1, 4, 3, 2, 5},这是字典序最小的序列。

输入解题思路,AI测评打分。不知道怎么写?

首页