QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 512 MB Puntuación total: 100

#1884. 不可能的任务:侠盗猎车手

Estadísticas

汤姆·克鲁斯在伯明翰拍摄期间车被偷了。作为首席警官,你被要求抓住小偷。

你的手下正在乡村巡逻,罪犯只能藏在 $n$ 个城镇中的某一个,城镇之间的公路构成了一棵树——也就是说,有 $n-1$ 条双向道路,每条道路连接一对城镇,且从任何一个城镇出发都能到达其他任何城镇。每天你可以选择任意两个城镇 $A$ 和 $B$(不一定不同),并请求一支部队检查 $A$ 和 $B$ 之间简单路径上的每一个城镇(包括这两个城镇本身)。如果小偷在这些城镇中的某一个,那么他就被抓住了。否则,当天晚些时候,他可以移动到任何一个相邻的城镇,或者留在原地。

由于这是一个非常重要的案件,你的时间非常紧迫。具体来说,设 $m$ 为树中叶子节点的数量(即只有一条道路连接的城镇)。你需要制定一个 $\lceil m/2 \rceil + 1$ 天的计划来抓住小偷:每天你都要给出对应的 $A$ 和 $B$,你的目标是不依赖于小偷在这些天之间的行动而抓住他。

请找到一个满足要求的计划。你需要回答多个测试用例。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 100$),表示测试用例的数量。接下来是 $T$ 个测试用例。

每个测试用例的第一行包含一个整数 $n$ ($2 \le n \le 2 \cdot 10^5$),表示城镇的数量。接下来的 $n-1$ 行,每行包含两个由空格分隔的整数 $u$ 和 $v$ ($1 \le u, v \le n$),表示城镇 $u$ 和 $v$ 之间的一条道路。

保证每个测试用例都构成一棵树,且所有 $n$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,打印恰好 $\lceil m/2 \rceil + 1$ 行,每行包含两个整数 $A$ 和 $B$ ($1 \le A, B \le n$),表示对应路径的端点。如果你确定可以用更少的操作抓住小偷,只需在末尾添加任意路径即可。可以证明在这些约束条件下,答案总是存在的。

样例

样例输入 1

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

样例输出 1

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

说明

样例输出中的额外空行仅用于视觉上区分不同测试用例的答案。你不需要打印它们。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#550Editorial Open集训队作业 解题报告 by 朱鹏睿Qingyu2026-01-02 22:09:48 Download

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.