链接:https://codeforces.ml/contest/1363/problem/E
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 aiai, and binary digit bibi is written in it. He wants to have binary digit cici 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⋅au. 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 cucu written in it, or determine that it is impossible.
Input
First line contains a single integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) denoting the number of nodes in the tree.
ii-th line of the next nn lines contains 3 space-separated integers aiai, bibi, cici (1≤ai≤109,0≤bi,ci≤1)(1≤ai≤109,0≤bi,ci≤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≤u,v≤n, u≠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−1 if it is impossible.
Examples
input
Copy
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
Copy
4
input
Copy
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
Copy
24000
input
Copy
2 109 0 1 205 0 1 1 2
output
Copy
-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 4⋅14⋅1 = 44 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 10000⋅210000⋅2, select nodes 1,51,5 and exchange their digits, and similarly, choose node 22 and k=2k=2 for a cost of 2000⋅22000⋅2, 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.
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lb double
#define INF 0x3f3f3f3f
#define maxn 200010
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define gep(i,x,y) for(int i=x;i>=y;i--)
ll n,t,x,k,u,v,max1,s,ans,mod=1e9+7;
ll a[200001],b[200001],c[200001],dp[200001][2],f[200001];
vector<ll>p[200001];
void dfs(int i,int fa)
{
for(int j=0;j<p[i].size();j++)
{
if(p[i][j]!=fa)
{
a[p[i][j]]=min(a[p[i][j]],a[i]);
dfs(p[i][j],i);
}
}
}
void dfss(int i,int fa)
{
if(b[i]!=c[i])
dp[i][b[i]]++;
for(int j=0;j<p[i].size();j++)
{
if(p[i][j]!=fa)
{
dfss(p[i][j],i);
dp[i][1]+=dp[p[i][j]][1];
dp[i][0]+=dp[p[i][j]][0];
}
}
k=min(dp[i][1],dp[i][0]);
ans+=k*a[i]*2;
dp[i][1]-=k;
dp[i][0]-=k;
}
int main()
{
cin>>n;
ll s1=0,s2=0;
for(int i=1;i<=n;i++)
{
cin>>a[i]>>b[i]>>c[i];
s1+=b[i];
s2+=c[i];
f[i]=0;
}
for(int i=1;i<n;i++)
{
cin>>u>>v;
p[u].push_back(v);
p[v].push_back(u);
}
if(s1!=s2)
cout<<-1<<endl;
else
{
ans=0;
dfs(1,0);
dfss(1,0);
cout<<ans<<endl;
}
}