CF2258F.Plus Minus Tree

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree TT with nn vertices, rooted∗^{\text{∗}} at vertex 11. Each vertex vv is assigned an initial weight $ a_v \in { -1, 0, +1 } $.

For each vertex with weight 00, you have to assign a new weight, either −1-1 or +1+1. Let xvx_v be the final weight of vertex vv.

For vertex vv, let TvT_v be the set of nodes in the subtree†^{\text{†}} of vertex vv. The cost of vertex vv is defined as $$ S_v = \left| \sum_{u \in T_v}{x_u} \right|.$$

The total cost of the tree is defined as the sum of costs over all vertices. Find the minimum possible total cost of the tree after assigning the weights of all vertices with zero initial weight.

∗^{\text{∗}}A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root.

†^{\text{†}}A subtree of vertex vv is the subgraph consisting of vv, all its descendants, and all the edges between them. A descendant of vertex vv is any vertex uu for which vv is an ancestor. No vertex is its own descendant. An ancestor of vertex vv is any vertex on the simple path from vv to the root, including the root, but not including vv. The root has no ancestors.

给你一棵有 nn 个顶点的树 TT,以顶点 11 为根∗^{\text{∗}}。每个顶点 vv 被赋予一个初始权重 $ a_v \in { -1, 0, +1 } $。

对每个初始权重为 00 的顶点,你必须为其重新指定一个新权重,取值为 −1-1 或 +1+1。记顶点 vv 的最终权重为 xvx_v。

对顶点 vv,令 TvT_v 表示顶点 vv 的子树†^{\text{†}} 中所有节点的集合。顶点 vv 的代价定义为

S_v=∣∑_u∈T_vx_u∣.S\_v = \left| \sum\_{u \in T\_v}{x\_u} \right|.

整棵树的总代价定义为所有顶点代价之和。在为所有初始权重为零的顶点分配权重后,求整棵树可能的最小总代价。

∗^{\text{∗}} 树是一个无环连通图。有根树是一棵指定了一个特殊顶点作为根的树。

†^{\text{†}} 顶点 vv 的子树是指由 vv、vv 的所有后代以及它们之间的所有边构成的子图。顶点 vv 的后代是指任意满足 vv 是其祖先的顶点 uu;任何顶点均不是自身的后代。顶点 vv 的祖先是指从 vv 到根节点的简单路径上的所有顶点(包括根节点,但不包括 vv 自身)。根节点没有祖先。

输入格式

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

The first line of each test case contains nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5), denoting the number of vertices in the tree.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−1≤ai≤1-1 \le a_i \le 1).

Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n), denoting the nodes connected with an edge of the tree. 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 \cdot 10^5.

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

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

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−1≤ai≤1-1 \le a_i \le 1)。

接下来的 n−1n-1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n),表示树中一条边所连接的两个节点。保证所给的边构成一棵树。

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

输出格式

For each test case, print the minimum total cost of the tree.

对于每个测试用例,输出该树的最小总成本。

输入输出样例

  • 输入#1

    2
    5
    0 1 -1 0 1
    1 2
    1 3
    2 4
    2 5
    8
    1 -1 1 -1 1 -1 1 -1
    1 2
    2 3
    2 4
    3 5
    4 6
    6 7
    2 8

    输出#1

    5
    7

说明/提示

The best assignments in the first test case are [+1,+1,−1,−1,+1][+1, +1, -1, -1, +1] and [−1,+1,−1,−1,+1][-1, +1, -1, -1, +1]. They have a cost of 55.

Another assignment, [+1,+1,−1,+1,+1][+1, +1, -1, +1, +1] has a cost of 99, and [−1,+1,−1,+1,+1][-1, +1, -1, +1, +1] has a cost of 77.

The tree of the first testcase

The tree of the second testcase

第一个测试用例中最优的赋值方案为 [+1,+1,−1,−1,+1][+1, +1, -1, -1, +1] 和 [−1,+1,−1,−1,+1][-1, +1, -1, -1, +1],它们的代价均为 55。

另一组赋值方案 [+1,+1,−1,+1,+1][+1, +1, -1, +1, +1] 的代价为 99,而 [−1,+1,−1,+1,+1][-1, +1, -1, +1, +1] 的代价为 77。

第一个测试用例对应的树

第二个测试用例对应的树

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

首页