QOJ.ac

QOJ

时间限制: 2 s 内存限制: 1024 MB 总分: 100

#4989. 浆果之战

统计

采摘浆果是一项艰苦的工作,但也是一种宁静而放松的体验。经过一整天的采摘,闭上眼睛准备入睡时,脑海中往往只剩下浆果的影子。随着意识逐渐模糊,浆果开始拥有自己的生命,并创造出各种荒诞的场景。

Picture by Jonas Bergsten, public domain

给定一棵有 $n$ 个顶点的树,顶点编号从 $1$ 到 $n$。最初,每个顶点上都有一颗浆果。每个顶点上还有一只蚂蚁在守护着浆果。当你采摘顶点 $v$ 上的浆果时,所有位于其他顶点上的蚂蚁都会向 $v$ 的方向移动一步。已经在 $v$ 上的蚂蚁会留在原地。注意,由于图是一棵树,蚂蚁移动的路径是唯一的。

你的目标是采摘树上的所有浆果。只要蚂蚁保持分散,它们就不会对你构成威胁。但如果所有 $n$ 只蚂蚁在任何时刻最终都聚集在同一个顶点上,它们就会攻击你。请找出一个顶点的排列,使得按照该顺序采摘浆果时,所有蚂蚁不会最终聚集在同一个顶点上。

输入格式

第一行包含一个整数 $n$ ($2 \le n \le 3 \cdot 10^5$)。

接下来的 $n - 1$ 行,每行包含两个整数 $u$ 和 $v$ ($1 \le u \neq v \le n$),表示顶点 $u$ 和 $v$ 之间有一条边。

输出格式

如果无法找到答案,输出“NO”。

否则,第一行输出“YES”。第二行输出 $n$ 个整数 $p_1, p_2, \dots, p_n$,表示采摘浆果的顺序 ($1 \le p_i \le n$)。这意味着你采摘的第 $i$ 颗浆果位于顶点 $p_i$。

样例

输入格式 1

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

输出格式 1

YES
1 5 6 3 4 9 8 7 10 2

输入格式 2

3
1 2
2 3

输出格式 2

NO

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.