CF2227H.Fallen Leaves

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Yousef is given a tree∗^{\text{∗}} of nn vertices numbered from 11 to nn.

Let SS be the set of all leaves†^{\text{†}} of the given tree (this set is determined from the original tree and does not change).

Yousef repeats the following process until the number of unchosen vertices is at most one:

  • Choose two distinct vertices u,v∈Su, v \in S that have not been chosen before.
  • Add d(u,v)d(u, v) to the total cost, where d(u,v)d(u, v) is the number of edges on the simple path between uu and vv.
  • Mark uu and vv as chosen.

Your task is to help Yousef determine the minimum possible total cost achievable after the process ends.

∗^{\text{∗}}A tree is an undirected connected graph in which there are no cycles.

†^{\text{†}}A leaf of a tree is a vertex that is connected to at most one other vertex.

优素福得到了一棵包含 nn 个顶点的树∗^{\text{∗}},顶点编号为 11 到 nn。

令 SS 表示给定树中所有叶子节点†^{\text{†}} 的集合(该集合由原始树确定,且在整个过程中保持不变)。

优素福重复执行以下过程,直到未被选中的顶点数至多为 11:

  • 从 SS 中选择两个尚未被选过的不同顶点 u,v∈Su, v \in S;
  • 将 d(u,v)d(u, v) 加入总代价,其中 d(u,v)d(u, v) 表示 uu 与 vv 之间唯一简单路径上的边数;
  • 将 uu 和 vv 标记为已选。

你的任务是帮助优素福确定该过程结束时所能达到的最小可能总代价。

∗^{\text{∗}} 树是一种无向连通图,其中不含任何环。

†^{\text{†}} 树的一个叶子节点是指度数至多为 11 的顶点(即仅与至多一个其他顶点相连)。

输入格式

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Then tt test cases follow.

Each test case consists of several lines. The first line of the test case contains an integer nn (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.

Then n−1n − 1 lines follow, each of which contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) that describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.

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

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。随后是 tt 个测试用例。

每个测试用例由若干行组成。测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)—— 树中顶点的数量。

接下来是 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \ne v),表示由一条边连接的一对顶点。保证所给图是一棵树,且不含环或重边。

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

输出格式

For each test case, output a single integer — the minimum possible total cost achievable after the process ends.

对于每个测试用例,输出一个整数——过程结束后所能达到的最小总成本。

输入输出样例

  • 输入#1

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

    输出#1

    2
    4
    5
    2

说明/提示

In the first test case, the set of leaves SS contains 1,3,4{1, 3, 4}. Yousef can do the following:

  • Choose u=1u = 1, v=3v = 3, with d(1,3)=2d(1, 3) = 2. Yousef adds 22 to the total cost and marks vertices 11 and 33 as chosen.

Since there is one vertex left unchosen, the process stops, and the total cost is 22. It can be shown that 22 is the minimum answer.

The given tree in the first test case.

In the second test case, the set of leaves SS contains 1,3,5,6{1, 3, 5, 6}. Yousef can do the following:

  • Choose u=1u = 1, v=3v = 3, with d(1,3)=2d(1, 3) = 2. Yousef adds 22 to the total cost and marks vertices 11 and 33 as chosen.
  • Choose u=5u = 5, v=6v = 6, with d(5,6)=2d(5, 6) = 2. Yousef adds 22 to the total cost and marks vertices 55 and 66 as chosen.

Since there is no vertex left unchosen, the process stops, and the total cost is 44.

The given tree in the second test case.

在第一个测试用例中,叶子节点集合 SS 包含 {1,3,4}\{1, 3, 4\}。Yousef 可以执行以下操作:

  • 选择 u=1u = 1、v=3v = 3,此时 d(1,3)=2d(1, 3) = 2。Yousef 将 22 加入总代价,并将顶点 11 和 33 标记为已选。

由于还剩一个顶点(44)未被选中,该过程终止,总代价为 22。可以证明 22 是最小可能答案。

第一个测试用例中给出的树。

在第二个测试用例中,叶子节点集合 SS 包含 {1,3,5,6}\{1, 3, 5, 6\}。Yousef 可以执行以下操作:

  • 选择 u=1u = 1、v=3v = 3,此时 d(1,3)=2d(1, 3) = 2。Yousef 将 22 加入总代价,并将顶点 11 和 33 标记为已选。
  • 选择 u=5u = 5、v=6v = 6,此时 d(5,6)=2d(5, 6) = 2。Yousef 将 22 加入总代价,并将顶点 55 和 66 标记为已选。

由于所有顶点均已选中,该过程终止,总代价为 44。

第二个测试用例中给出的树。

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

首页