#abc362f. F - Perfect Matching on a Tree
F - Perfect Matching on a Tree
Score : points
问题陈述
给定一个有 个顶点的树 。顶点编号为 到 ,第 条边 双向连接顶点 和 。
使用 定义一个有 个顶点的完全图 ,如下:
- 在 中,顶点 和 之间的边的权重 是在 中顶点 和 之间的最短距离。
在 中找到一个 最大权重最大匹配。也就是说,找到一组 对顶点 $M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\}$,使得每个顶点 在 中最多出现一次,并且 $\displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i)$ 最大化。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a tree with vertices. The vertices are numbered to , and the -th edge connects vertices and bidirectionally.
Using , define a complete graph with vertices as follows:
- The weight of the edge between vertices and in is the shortest distance between vertices and in .
Find one maximum weight maximum matching in . That is, find a set of 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 appears in at most once, and $\displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i)$ is maximized.
Constraints
- The input graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
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.
Sample Input 1
4
1 2
2 3
3 4
Sample Output 1
2 4
1 3
In , the distance between vertices and is , and the distance between vertices and is , so the weight of the matching is . There is no matching with a weight greater than , 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 , the distance between vertices and is , so the weight of the matching is . There is no matching with a weight greater than , so this is a maximum weight maximum matching. Another acceptable output is:
3 1