CF1830D.Mex Tree

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree with nn nodes. For each node, you either color it in 00 or 11.

The value of a path (u,v)(u,v) is equal to the MEX†^\dagger of the colors of the nodes from the shortest path between uu and vv.

The value of a coloring is equal to the sum of values of all paths (u,v)(u,v) such that 1≤u≤v≤n1 \leq u \leq v \leq n.

What is the maximum possible value of any coloring of the tree?

†^{\dagger} The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1][2,2,1] is 00, because 00 does not belong to the array.
  • The MEX of [3,1,0,1][3,1,0,1] is 22, because 00 and 11 belong to the array, but 22 does not.
  • The MEX of [0,3,1,2][0,3,1,2] is 44 because 00, 11, 22, and 33 belong to the array, but 44 does not.

给你一棵包含 nn 个节点的树。对每个节点,你将其染成 00 或 11。

路径 (u,v)(u,v) 的值等于 uu 到 vv 的最短路径上所有节点颜色所构成数组的 MEX†^\dagger。

一种染色方案的值定义为所有满足 1≤u≤v≤n1 \leq u \leq v \leq n 的路径 (u,v)(u,v) 的值之和。

该树的所有染色方案中,最大可能的值是多少?

†^{\dagger} 数组的 MEX(最小未出现值)是指不属于该数组的最小非负整数。例如:

  • [2,2,1][2,2,1] 的 MEX 是 00,因为 00 不在该数组中。
  • [3,1,0,1][3,1,0,1] 的 MEX 是 22,因为 00 和 11 在该数组中,但 22 不在。
  • [0,3,1,2][0,3,1,2] 的 MEX 是 44,因为 00、11、22 和 33 都在该数组中,但 44 不在。

输入格式

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

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of nodes in the tree.

The following n−1n-1 lines of each test case contains 22 integers aia_i and bib_i (1≤ai,bi≤n,ai≠bi1 \leq a_i, b_i \leq n, a_i \neq b_i) — indicating an edge between vertices aia_i and bib_i. It is guaranteed that the given edges form a tree.

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

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示树中节点的数量。

每个测试用例接下来的 n−1n-1 行,每行包含两个整数 aia_i 和 bib_i(1≤ai,bi≤n, ai≠bi1 \leq a_i, b_i \leq n,\, a_i \neq b_i),表示节点 aia_i 与 bib_i 之间存在一条边。保证所给的边构成一棵树。

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

输出格式

For each test case, print the maximum possible value of any coloring of the tree.

对于每个测试用例,输出该树的任意染色方案所能达到的最大值。

输入输出样例

  • 输入#1

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

    输出#1

    8
    15
    96
    1

说明/提示

In the first sample, we will color vertex 22 in 11 and vertices 1,31,3 in 00. After this, we consider all paths:

  • (1,1)(1,1) with value 11
  • (1,2)(1,2) with value 22
  • (1,3)(1,3) with value 22
  • (2,2)(2,2) with value 00
  • (2,3)(2,3) with value 22
  • (3,3)(3,3) with value 11

We notice the sum of values is 88 which is the maximum possible.

在第一个样例中,我们将顶点 22 染成颜色 11,顶点 11 和 33 染成颜色 00。随后,我们考虑所有路径:

  • (1,1)(1,1),其值为 11
  • (1,2)(1,2),其值为 22
  • (1,3)(1,3),其值为 22
  • (2,2)(2,2),其值为 00
  • (2,3)(2,3),其值为 22
  • (3,3)(3,3),其值为 11

我们注意到这些值的总和为 88,这是可能的最大值。

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

首页