QOJ.ac

QOJ

Límite de tiempo: 2.0 s Límite de memoria: 1024 MB Puntuación total: 100

#17417. Alice 和 Bob

Estadísticas

Alice 和 Bob 拥有一棵包含 $2N$ 个节点的树 $T$,节点编号为 $1$ 到 $2N$。

Bob 有一个整数 $K$,他希望找到 $N$ 个数对 $(X_1, Y_1), (X_2, Y_2), \dots, (X_N, Y_N)$,使得 $1$ 到 $2N$ 中的每个整数恰好在其中一个数对中出现一次,且距离之和 $\sum_{i=1}^N \text{dist}(X_i, Y_i) = K$。其中 $\text{dist}(X_i, Y_i)$ 表示树中节点 $X_i$ 和 $Y_i$ 之间的距离,即 $X_i$ 和 $Y_i$ 之间(唯一)路径上的边数。

Alice 不希望 Bob 成功。她可以执行以下操作:

  • 从 $T$ 中删除一条边 $(U, V)$。
  • 添加一条新边 $(A, B)$,使得 $T$ 仍然是一棵树。

注意,允许选择同一条边进行删除和插入,这只会导致树保持原样。

帮助 Alice 找到一条应该删除的边 $(U, V)$ 和一条应该添加的边 $(A, B)$。如果存在多个解,你可以输出其中任意一个。

可以证明,在本题的限制下,总是可以通过删除一条边并添加一条边,使得不存在 $N$ 个数对 $(X_1, Y_1), (X_2, Y_2), \dots, (X_N, Y_N)$(其中 $1$ 到 $2N$ 的每个整数恰好出现一次)满足其距离之和等于 $K$。

输入格式

输入按以下格式给出:

T
N K
U_1 V_1
U_2 V_2
...
U_{2N-1} V_{2N-1}
...

数据范围

  • 所有输入值均为整数。
  • $1 \le T \le 10^4$
  • $2 \le N \le 2 \times 10^5$
  • $1 \le K \le 10^{12}$
  • $1 \le U_i, V_i \le 2N$
  • 保证给定的边构成一棵树。
  • 保证所有测试用例中 $N$ 的总和不超过 $2 \times 10^5$。

输出格式

对于每个测试用例,输出两行:

第一行包含两个整数 $U$ 和 $V$ —— 表示要删除的边 $(U, V)$。

第二行包含两个整数 $A$ 和 $B$ —— 表示要添加的边 $(A, B)$。

如果存在多个解,输出其中任意一个。

样例

输入 1

2
2 20
1 2
2 3
3 4
2 4
1 2
2 3
3 4

输出 1

1 2
1 2
1 2
1 3

说明

测试用例 1:初始树已经无法让 Bob 获得 $K = 20$ 的得分,因此 Alice 只需简单地删除并插入任意一条已有的边即可。

测试用例 2:初始树中 Bob 有一种方法可以获得 $K = 4$ 的得分,例如选择数对 $(1, 4)$ 和 $(2, 3)$。Alice 删除了边 $(1, 2)$ 并插入了 $(1, 3)$。在这棵新树中,Bob 无法获得 $4$ 的得分。

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.