CF1363E.Tree Shuffling

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Ashish has a tree consisting of nn nodes numbered 11 to nn rooted at node 11. The ii-th node in the tree has a cost aia_i, and binary digit bib_i is written in it. He wants to have binary digit cic_i written in the ii-th node in the end.

To achieve this, he can perform the following operation any number of times:

  • Select any kk nodes from the subtree of any node uu, and shuffle the digits in these nodes as he wishes, incurring a cost of k⋅auk \cdot a_u. Here, he can choose kk ranging from 11 to the size of the subtree of uu.

He wants to perform the operations in such a way that every node finally has the digit corresponding to its target.

Help him find the minimum total cost he needs to spend so that after all the operations, every node uu has digit cuc_u written in it, or determine that it is impossible.

阿什什有一棵由 nn 个节点组成的树,节点编号为 11 到 nn,根节点为节点 11。树中第 ii 个节点有一个代价 aia_i,且写有一个二进制数字 bib_i。他希望最终第 ii 个节点上写的二进制数字为 cic_i。

为实现这一目标,他可以执行以下操作任意多次:

  • 任选节点 uu 的子树中的任意 kk 个节点,并随意重排这些节点上的二进制数字,该操作花费代价为 k⋅auk \cdot a_u。其中,kk 可取 11 到 uu 的子树大小之间的任意整数。

他希望以某种方式执行这些操作,使得每个节点最终所写的数字均与其目标值一致。

请帮助他求出使所有节点 uu 最终写有数字 cuc_u 所需的最小总代价;若不可能实现,则判定其不可行。

输入格式

First line contains a single integer nn (1≤n≤2⋅105)(1 \le n \le 2 \cdot 10^5) denoting the number of nodes in the tree.

ii-th line of the next nn lines contains 3 space-separated integers aia_i, bib_i, cic_i (1≤ai≤109,0≤bi,ci≤1)(1 \leq a_i \leq 10^9, 0 \leq b_i, c_i \leq 1) — the cost of the ii-th node, its initial digit and its goal digit.

Each of the next n−1n - 1 lines contain two integers uu, vv (1≤u,v≤n, u≠v)(1 \leq u, v \leq n, \text{ } u \ne v), meaning that there is an edge between nodes uu and vv in the tree.

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

接下来的 nn 行中,第 ii 行包含三个用空格分隔的整数 aia_i、bib_i、cic_i (1≤ai≤109,0≤bi,ci≤1)(1 \leq a_i \leq 10^9, 0 \leq b_i, c_i \leq 1) —— 分别表示第 ii 个节点的代价、其初始数字和目标数字。

接下来的 n−1n - 1 行中,每行包含两个整数 uu、vv (1≤u,v≤n, u≠v)(1 \leq u, v \leq n, \text{ } u \ne v),表示树中节点 uu 与节点 vv 之间存在一条边。

输出格式

Print the minimum total cost to make every node reach its target digit, and −1-1 if it is impossible.

输出使每个节点都达到其目标数字的最小总代价;如果无法实现,则输出 −1-1。

输入输出样例

  • 输入#1

    5
    1 0 1
    20 1 0
    300 0 1
    4000 0 0
    50000 1 0
    1 2
    2 3
    2 4
    1 5

    输出#1

    4
  • 输入#2

    5
    10000 0 1
    2000 1 0
    300 0 1
    40 0 0
    1 1 0
    1 2
    2 3
    2 4
    1 5

    输出#2

    24000
  • 输入#3

    2
    109 0 1
    205 0 1
    1 2

    输出#3

    -1

说明/提示

The tree corresponding to samples 11 and 22 are:

In sample 11, we can choose node 11 and k=4k = 4 for a cost of 4⋅14 \cdot 1 = 44 and select nodes 1,2,3,5{1, 2, 3, 5}, shuffle their digits and get the desired digits in every node.

In sample 22, we can choose node 11 and k=2k = 2 for a cost of 10000⋅210000 \cdot 2, select nodes 1,5{1, 5} and exchange their digits, and similarly, choose node 22 and k=2k = 2 for a cost of 2000⋅22000 \cdot 2, select nodes 2,3{2, 3} and exchange their digits to get the desired digits in every node.

In sample 33, it is impossible to get the desired digits, because there is no node with digit 11 initially.

对应样例 11 和样例 22 的树如下:

在样例 11 中,我们可以选择节点 11 并取 k=4k = 4,花费为 4⋅1=44 \cdot 1 = 4,选中节点集 {1,2,3,5}\{1, 2, 3, 5\},对其数字进行重排,从而使得每个节点均得到目标数字。

在样例 22 中,我们可以选择节点 11 并取 k=2k = 2,花费为 10000⋅210000 \cdot 2,选中节点集 {1,5}\{1, 5\} 并交换它们的数字;类似地,选择节点 22 并取 k=2k = 2,花费为 2000⋅22000 \cdot 2,选中节点集 {2,3}\{2, 3\} 并交换它们的数字,从而使得每个节点均得到目标数字。

在样例 33 中,无法得到目标数字,因为初始时没有任何节点包含数字 11。

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

首页