#abc362f. F - Perfect Matching on a Tree

F - Perfect Matching on a Tree

Score : 550550 points

问题陈述

给定一个有 NN 个顶点的树 TT。顶点编号为 11NN,第 ii 条边 (1iN1)(1 \leq i \leq N-1) 双向连接顶点 uiu_iviv_i

使用 TT 定义一个有 NN 个顶点的完全图 GG,如下:

  • GG 中,顶点 xxyy 之间的边的权重 w(x,y)w(x,y) 是在 TT 中顶点 xxyy 之间的最短距离。

GG 中找到一个 最大权重最大匹配。也就是说,找到一组 N/2\lfloor N/2 \rfloor 对顶点 $M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\}$,使得每个顶点 1,2,,N1,2,\dots, NMM 中最多出现一次,并且 $\displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i)$ 最大化。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a tree TT with NN vertices. The vertices are numbered 11 to NN, and the ii-th edge (1iN1)(1 \leq i \leq N-1) connects vertices uiu_i and viv_i bidirectionally.

Using TT, define a complete graph GG with NN vertices as follows:

  • The weight w(x,y)w(x,y) of the edge between vertices xx and yy in GG is the shortest distance between vertices xx and yy in TT.

Find one maximum weight maximum matching in GG. That is, find a set of N/2\lfloor N/2 \rfloor pairs of vertices $M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\}$ such that each vertex 1,2,,N1,2,\dots, N appears in MM at most once, and $\displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i)$ is maximized.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1ui<viN1 \leq u_i < v_i \leq N
  • The input graph is a tree.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uN1u_{N-1} vN1v_{N-1}

Output

Print a solution as $\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\}$ in the following format. If multiple solutions exist, any of them is acceptable.

x1x_1 y1y_1

x2x_2 y2y_2

\vdots

xN/2x_{\lfloor N/2 \rfloor} yN/2y_{\lfloor N/2 \rfloor}

Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2 4
1 3

In TT, the distance between vertices 22 and 44 is 22, and the distance between vertices 11 and 33 is 22, so the weight of the matching {(2,4),(1,3)}\{(2,4),(1,3)\} is 44. There is no matching with a weight greater than 44, so this is a maximum weight maximum matching. Other acceptable outputs include:

2 3
1 4

Sample Input 2

3
1 2
2 3

Sample Output 2

1 3

In TT, the distance between vertices 11 and 33 is 22, so the weight of the matching {(1,3)}\{(1,3)\} is 22. There is no matching with a weight greater than 22, so this is a maximum weight maximum matching. Another acceptable output is:

3 1