QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 1024 MB 満点: 100

#4996. 冰雪行程

統計

在一个严寒的冬日,小镇上有 $n$ 座房子。清晨时分,大家都还在熟睡,只有邮递员托马斯醒着。地面上积满了厚厚的雪,只有 $m$ 条连接部分房子的道路可以通行。

图片由 Stöhrfall 提供,公有领域

托马斯需要给镇上的 $n$ 座房子送信。房子编号从 $1$ 到 $n$。托马斯将从他自己的房子(房子 $1$)出发,然后以某种顺序访问所有其他房子。他可以使用自行车在有道路连接的两座房子之间穿行,如果两座房子之间没有道路,他可以使用滑雪板。但是,在道路上不能使用滑雪板,在道路外也不能使用自行车。由于在自行车和滑雪板之间切换非常麻烦,托马斯希望这种切换最多发生一次。

你的任务是找到 $n$ 座房子的一个排列 $a_1, a_2, \dots, a_n$,使得 $a_1 = 1$,并且最多存在一个索引 $i$ ($2 \le i \le n - 1$),满足以下条件之一:

  1. $a_{i-1}$ 和 $a_i$ 之间有道路连接,但 $a_i$ 和 $a_{i+1}$ 之间没有;或者
  2. $a_{i-1}$ 和 $a_i$ 之间没有道路连接,但 $a_i$ 和 $a_{i+1}$ 之间有。

换句话说,这个序列从 $1$ 开始,在道路和非道路之间的切换次数最多为一次。

输入格式

第一行包含两个整数 $n, m$ ($2 \le n \le 3 \cdot 10^5, 0 \le m \le 3 \cdot 10^5$),分别表示房子的数量和道路的数量。

接下来的 $m$ 行,每行包含两个整数 $u_i, v_i$ ($1 \le u_i \neq v_i \le n$),表示 $u_i$ 和 $v_i$ 之间有一条道路。道路是双向的,且所有无序对 $\{u_i, v_i\}$ 均不相同。

输入保证对于每种情况都存在合法的访问顺序。

输出格式

在一行中输出 $n$ 个整数 $a_1, a_2, \dots, a_n$,表示访问房子的顺序。

请记住,第一个数字 $a_1$ 必须是 $1$。

样例

输入格式 1

4 4
1 2
1 3
1 4
3 4

输出格式 1

1 4 3 2

输入格式 2

5 0

输出格式 2

1 2 3 4 5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.