CF1935F.Andrey's Tree

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Master Andrey loves trees†^{\dagger} very much, so he has a tree consisting of nn vertices.

But it's not that simple. Master Timofey decided to steal one vertex from the tree. If Timofey stole vertex vv from the tree, then vertex vv and all edges with one end at vertex vv are removed from the tree, while the numbers of other vertices remain unchanged. To prevent Andrey from getting upset, Timofey decided to make the resulting graph a tree again. To do this, he can add edges between any vertices aa and bb, but when adding such an edge, he must pay ∣a−b∣|a - b| coins to the Master's Assistance Center.

Note that the resulting tree does not contain vertex vv.

Timofey has not yet decided which vertex vv he will remove from the tree, so he wants to know for each vertex 1≤v≤n1 \leq v \leq n, the minimum number of coins needed to be spent to make the graph a tree again after removing vertex vv, as well as which edges need to be added.

†^{\dagger}A tree is an undirected connected graph without cycles.

大师安德烈非常喜爱树†^{\dagger},因此他拥有一棵由 nn 个顶点构成的树。

但事情并非如此简单。大师季莫费决定从这棵树中偷走一个顶点。若季莫费偷走了顶点 vv,则顶点 vv 及所有以 vv 为一个端点的边均被从树中移除,其余顶点的编号保持不变。为避免安德烈生气,季莫费决定使剩余图再次成为一棵树。为此,他可以在任意两个顶点 aa 和 bb 之间添加边;但每次添加这样一条边时,他必须向大师援助中心支付 ∣a−b∣|a - b| 枚金币。

注意:最终得到的树不包含顶点 vv。

季莫费尚未决定将从树中移除哪一个顶点 vv,因此他希望对每个顶点 1≤v≤n1 \leq v \leq n,均求出在移除顶点 vv 后,使图重新变为一棵树所需花费的最少金币数,以及需要添加的具体边。

†^{\dagger} 树是指无向、连通且无环的图。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (5≤n≤2⋅1055 \le n \le 2\cdot10^5) — the number of vertices in Andrey's tree.

The next n−1n - 1 lines contain a description of the tree's edges. The ii-th of these lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n) — the numbers of vertices connected by the ii-th edge.

It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(5≤n≤2⋅1055 \le n \le 2\cdot10^5),表示安德烈的树中顶点的数量。

接下来的 n−1n - 1 行描述了树的边。其中第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n),表示第 ii 条边所连接的两个顶点的编号。

保证所给的边构成一棵树。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, output the answer in the following format:

For each vertex vv (in the order from 11 to nn), in the first line output two integers ww and mm — the minimum number of coins that need to be spent to make the graph a tree again after removing vertex vv, and the number of added edges.

Then output mm lines, each containing two integers aa and bb (1≤a,b≤n,a≠v,b≠v1 \le a, b \le n, a \ne v, b \ne v, a≠ba \ne b) — the ends of the added edge.

If there are multiple ways to add edges, you can output any solution with the minimum cost.

对于每个测试用例,按如下格式输出答案:

对每个顶点 vv(按编号从 11 到 nn 的顺序),在第一行输出两个整数 ww 和 mm —— 分别表示删除顶点 vv 后,使图重新成为一棵树所需花费的最少硬币数,以及需添加的边数。

随后输出 mm 行,每行包含两个整数 aa 和 bb(1≤a,b≤n1 \le a, b \le n,a≠va \ne v,b≠vb \ne v,且 a≠ba \ne b)—— 表示所添加边的两个端点。

若存在多种添加边的方式,可输出任意一种最小代价的方案。

输入输出样例

  • 输入#1

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

    输出#1

    1 1
    3 4
    
    0 0
    
    1 1
    1 2
    
    2 1
    3 5
    
    0 0
    
    0 0
    
    0 0
    
    1 1
    1 2
    
    1 1
    1 2
    
    1 1
    1 2
    
    3 3
    2 3
    4 5
    3 4
    
    0 0
    
    0 0
    
    0 0
    
    0 0

说明/提示

In the first test case:

Consider the removal of vertex 44:

The optimal solution would be to add an edge from vertex 55 to vertex 33. Then we will spend ∣5−3∣=2|5 - 3| = 2 coins.

In the third test case:

Consider the removal of vertex 11:

The optimal solution would be:

  • Add an edge from vertex 22 to vertex 33, spending ∣2−3∣=1|2 - 3| = 1 coin.
  • Add an edge from vertex 33 to vertex 44, spending ∣3−4∣=1|3 - 4| = 1 coin.
  • Add an edge from vertex 44 to vertex 55, spending ∣4−5∣=1|4 - 5| = 1 coin.

Then we will spend a total of 1+1+1=31 + 1 + 1 = 3 coins.

Consider the removal of vertex 22:

No edges need to be added, as the graph will remain a tree after removing the vertex.

在第一个测试用例中:

考虑移除顶点 44:

最优方案是添加一条从顶点 55 到顶点 33 的边,花费 ∣5−3∣=2|5 - 3| = 2 枚金币。

在第三个测试用例中:

考虑移除顶点 11:

最优方案为:

  • 添加一条从顶点 22 到顶点 33 的边,花费 ∣2−3∣=1|2 - 3| = 1 枚金币;
  • 添加一条从顶点 33 到顶点 44 的边,花费 ∣3−4∣=1|3 - 4| = 1 枚金币;
  • 添加一条从顶点 44 到顶点 55 的边,花费 ∣4−5∣=1|4 - 5| = 1 枚金币。

此时总共花费 1+1+1=31 + 1 + 1 = 3 枚金币。

考虑移除顶点 22:

无需添加任何边,因为移除该顶点后图仍保持为一棵树。

输入解题思路,AI测评打分。不知道怎么写?

首页