CF1689C.Infected Tree

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Byteland is a beautiful land known because of its beautiful trees.

Misha has found a binary tree with nn vertices, numbered from 11 to nn. A binary tree is an acyclic connected bidirectional graph containing nn vertices and n−1n - 1 edges. Each vertex has a degree at most 33, whereas the root is the vertex with the number 11 and it has a degree at most 22.

Unfortunately, the root got infected.

The following process happens nn times:

  • Misha either chooses a non-infected (and not deleted) vertex and deletes it with all edges which have an end in this vertex or just does nothing.
  • Then, the infection spreads to each vertex that is connected by an edge to an already infected vertex (all already infected vertices remain infected).

As Misha does not have much time to think, please tell him what is the maximum number of vertices he can save from the infection (note that deleted vertices are not counted as saved).

Byteland 是一片以美丽树木而闻名的土地。

米沙发现了一棵包含 nn 个顶点的二叉树,顶点编号从 11 到 nn。二叉树是一种无环、连通、无向图,包含 nn 个顶点和 n−1n - 1 条边;每个顶点的度数至多为 33,而根节点是编号为 11 的顶点,其度数至多为 22。

不幸的是,根节点被感染了。

接下来的过程将执行 nn 轮:

  • 米沙要么选择一个未被感染且未被删除的顶点,并将其(以及所有以该顶点为端点的边)删除;要么什么都不做。
  • 然后,感染会沿着边传播:所有与已感染顶点相邻的顶点均被感染(所有已感染顶点保持感染状态)。

由于米沙没有太多时间思考,请你告诉他:他最多能拯救多少个顶点免于感染?(注意:被删除的顶点不计入被拯救的顶点。)

输入格式

There are several test cases in the input data. The first line contains a single integer tt (1≤t≤50001\leq t\leq 5000) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains one integer nn (2≤n≤3⋅1052\leq n\leq 3\cdot 10^5) — the number of vertices of the tree.

The ii-th of the following n−1n-1 lines in the test case contains two positive integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), meaning that there exists an edge between them in the graph.

It is guaranteed that the graph is a binary tree rooted at 11. It is also guaranteed that the sum of nn over all test cases won't exceed 3⋅1053\cdot 10^5.

输入数据包含多个测试用例。第一行包含一个整数 tt(1≤t≤50001\leq t\leq 5000),表示测试用例的数量。随后是各测试用例的描述。

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

接下来的 n−1n-1 行中,第 ii 行包含两个正整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n),表示图中存在一条连接它们的边。

保证该图是一棵以顶点 11 为根的二叉树。同时保证所有测试用例的 nn 值之和不超过 3⋅1053\cdot 10^5。

输出格式

For each test case, output the maximum number of vertices Misha can save.

对于每个测试用例,输出米沙最多能拯救的顶点数量。

输入输出样例

  • 输入#1

    4
    2
    1 2
    4
    1 2
    2 3
    2 4
    7
    1 2
    1 5
    2 3
    2 4
    5 6
    5 7
    15
    1 2
    2 3
    3 4
    4 5
    4 6
    3 7
    2 8
    1 9
    9 10
    9 11
    10 12
    10 13
    11 14
    11 15

    输出#1

    0
    2
    2
    10

说明/提示

In the first test case, the only possible action is to delete vertex 22, after which we save 00 vertices in total.

In the second test case, if we delete vertex 22, we can save vertices 33 and 44.

在第一个测试用例中,唯一可能的操作是删除顶点 22,之后总共保留下 00 个顶点。

在第二个测试用例中,如果我们删除顶点 22,则可以保留下顶点 33 和 44。

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

首页