CF1905B.Begginer's Zelda

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree†^{\dagger}. In one zelda-operation you can do follows:

  • Choose two vertices of the tree uu and vv;
  • Compress all the vertices on the path from uu to vv into one vertex. In other words, all the vertices on path from uu to vv will be erased from the tree, a new vertex ww will be created. Then every vertex ss that had an edge to some vertex on the path from uu to vv will have an edge to the vertex ww.

Illustration of a zelda-operation performed for vertices 11 and 55.

Determine the minimum number of zelda-operations required for the tree to have only one vertex.

†^{\dagger}A tree is a connected acyclic undirected graph.

给你一棵树†^{\dagger}。一次“塞尔达操作”(zelda-operation)可执行如下步骤:

  • 选择树中的两个顶点 uu 和 vv;
  • 将 uu 到 vv 路径上的所有顶点压缩为一个顶点。换言之,uu 到 vv 路径上的所有顶点将从树中被删除,并创建一个新顶点 ww;随后,每个原本与该路径上某个顶点相邻的顶点 ss,都将改为与新顶点 ww 相邻。

对顶点 11 和 55 执行一次塞尔达操作的示意图。

求使整棵树最终仅剩一个顶点所需的最少塞尔达操作次数。

†^{\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 (2≤n≤1052 \le n \le 10^5) — the number of vertices.

ii-th of the next n−1n − 1 lines contains two integers uiu_i and viv_i (1≤ui,vi≤n,ui≠vi1 \le u_i, v_i \le n, u_i \ne v_i) — 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 10510^5.

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

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5),表示顶点的数量。

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

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

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, output a single integer — the minimum number of zelda-operations required for the tree to have only one vertex.

对于每个测试用例,输出一个整数——使树最终仅剩一个顶点所需的最少 zelda-操作次数。

输入输出样例

  • 输入#1

    4
    4
    1 2
    1 3
    3 4
    9
    3 1
    3 5
    3 2
    5 6
    6 7
    7 8
    7 9
    6 4
    7
    1 2
    1 3
    2 4
    4 5
    3 6
    2 7
    6
    1 2
    1 3
    1 4
    4 5
    2 6

    输出#1

    1
    3
    2
    2

说明/提示

In the first test case, it's enough to perform one zelda-operation for vertices 22 and 44.

In the second test case, we can perform the following zelda-operations:

  1. u=2,v=1u = 2, v = 1. Let the resulting added vertex be labeled as w=10w = 10;
  2. u=4,v=9u = 4, v = 9. Let the resulting added vertex be labeled as w=11w = 11;
  3. u=8,v=10u = 8, v = 10. After this operation, the tree consists of a single vertex.

在第一个测试用例中,对顶点 22 和 44 执行一次 zelda-操作即可。

在第二个测试用例中,我们可以执行以下 zelda-操作:

  1. u=2,v=1u = 2, v = 1,将新添加的顶点标记为 w=10w = 10;
  2. u=4,v=9u = 4, v = 9,将新添加的顶点标记为 w=11w = 11;
  3. u=8,v=10u = 8, v = 10,该操作完成后,树仅包含一个顶点。

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

首页