0%

Codeforces 1363E - Tree Shuffling

原文

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 kauk\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.

Input

First line contains a single integer nn (1n2105)(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 (1ai109,0bi,ci1)(1\le a_i\le 10^9,0\le b_i,c_i\le1) — the cost of the ii-th node, its initial digit and its goal digit.

Each of the next n−1 lines contain two integers uu, vv (1u,vn,uv)(1\le u,v\le n, u\ne v), meaning that there is an edge between nodes uu and vv in the tree.

Output

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

Examples

input

1
2
3
4
5
6
7
8
9
10
5
1 0 1
20 1 0
300 0 1
4000 0 0
50000 1 0
1 2
2 3
2 4
1 5

output

1
4

input

1
2
3
4
5
6
7
8
9
10
5
10000 0 1
2000 1 0
300 0 1
40 0 0
1 1 0
1 2
2 3
2 4
1 5

output

1
24000

input

1
2
3
4
2
109 0 1
205 0 1
1 2

output

1
-1

Note

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 41=44\cdot1 = 4 and select nodes 1,2,3,51,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 10000210000\cdot2, select nodes 1,51,5 and exchange their digits, and similarly, choose node 22 and k=2k=2 for a cost of 200022000\cdot2, select nodes 2,32,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.

题意

有一颗树,树根为节点1,每个节点有三个属性a,b,ca,b,cbb代表当前节点的状态,cc代表当前节点需要变成的状态,aa代表修改节点状态所需要的花费。

现在能够进行一种操作:

选择一颗以uu为根节点的子树,任意的修改其子树中的kk个节点的状态(互相交换),这个操作的花费为kauk \cdot a_u

现在需要求解使树中所有节点都能变成目标状态所需要的最小开销,如果不存在则进行判定。

思路

首先可以知道,如果一个节点ii的父亲节点为pp,且有a[i]>a[p]a[i]>a[p],那么所有以ii节点为根的子树上涉及到的操作都可以移动到其父节点上进行,可以对整棵树进行a[i]=min(a[i],a[p])a[i] = \min(a[i],a[p])的操作而不改变本身的结果。

完成以上操作之后,根节点到叶子结点的值实际上就是一个单调递减的序列,可以从叶子结点往根节点进行遍历,一旦当前子树上存在可以操作的成对节点就进行操作。

时间复杂度为遍历树的时间复杂度O(N)O(N)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;

#define maxn 200010
#define ll long long

int n;
ll a[maxn], b[maxn], c[maxn];
vector<int> g[maxn];
ll ans;

pair<ll, ll> solve(int u, int pa=0){
a[u] = min(a[u], a[pa]);
ll cnt0 = 0;
ll cnt1 = 0;
if(b[u] == 0 && c[u] == 1){
++cnt0;
}
else if(b[u] == 1 && c[u] == 0){
++cnt1;
}

for(auto v: g[u]){
if(v != pa){
auto tmp = solve(v, u);
cnt0 += tmp.first;
cnt1 += tmp.second;
}
}

ll change = min(cnt0, cnt1);
ans += change * a[u] * 2;
return make_pair(cnt0-change, cnt1-change);
}

int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>n;
for(int i=1;i<=n;++i){
cin>>a[i]>>b[i]>>c[i];
}

for(int i=1;i<n;++i){
int u,v;
cin>>u>>v;
g[v].push_back(u);
g[u].push_back(v);
}

a[0] = a[1];

auto res = solve(1);

if(res.first != 0 || res.second != 0){
cout<<-1<<endl;
}
else{
cout<<ans<<endl;
}
}