QOJ.ac

QOJ

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

#4290. 伟大的寻蛋之旅

統計

每年复活节,Bob 的祖母都会在他们的家族宅邸中组织“伟大的寻蛋游戏”(The Great Egg Hunt)。这已成为一个在 Bob 出生前就有的家族传统。Bob 的祖母首先会将糖果装满一个巨大的复活节彩蛋,然后随机(均匀分布)选择一个房间并将彩蛋藏在那里。第一个找到彩蛋的人可以保留里面所有的糖果。

Bob 的家族宅邸由 $N$ 个房间组成。房间之间有 $N - 1$ 条通道。你可以假设这座宅邸是连通的,这意味着总是可以通过通道从任何一个房间走到其他任何房间。

作为一名资深的寻蛋猎人,Bob 开发了一种寻找彩蛋的方法,他称之为“蛋优先搜索”(Egg First Search):

  1. 如果 Bob 当前所在的房间尚未探索,他会搜索该房间。由于 Bob 是一位资深的寻蛋猎人,如果彩蛋就在这个房间里,他就能找到它。
  2. 否则,Bob 会移动到一个相邻的房间,该房间位于通往任何一个最近的未探索房间的方向上。如果有多个房间可供选择,他会随机(均匀分布)选择一个。

假设 Bob 搜索一个房间需要 1 个单位时间,从一个房间移动到相邻房间也需要 1 个单位时间。

Bob 现在唯一不确定的是他应该从哪个房间开始搜索。因此,他向你寻求帮助。给定宅邸的地图,请找出哪些起始房间能使使用 Bob 的“蛋优先搜索”方法找到彩蛋的期望时间最小。

装有糖果的复活节彩蛋。来源:Wikimedia Commons, CC BY-SA 3.0。

输入格式

第一行包含一个整数 $N$ ($1 \le N \le 2 \cdot 10^5$),表示宅邸中的房间数量。 接下来的 $N - 1$ 行,每行包含两个空格分隔的整数 $u$ 和 $v$ ($1 \le u, v \le N, u \neq v$),表示一对通过通道相连的房间。保证宅邸是连通的,即总是可以通过通道从任何一个房间走到其他任何房间。

输出格式

第一行输出一个整数 $M$,表示最优起始房间的数量。第二行输出 $M$ 个空格分隔的整数 $S_1, \dots, S_M$ ($1 \le S_1 < S_2 < \dots < S_M \le N$),即最优起始房间的列表。

样例

输入 1

3
1 2
2 3

输出 1

2
1 3

输入 2

5
1 2
1 3
1 4
1 5

输出 2

4
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.