E. Tree Shuffling
Ashish has a tree consisting of n nodes numbered 1 to n rooted at node 1. The i-th node in the tree has a cost ai, and binary digit bi is written in it. He wants to have binary digit ci written in the i-th node in the end.
To achieve this, he can perform the following operation any number of times:
Select any k nodes from the subtree of any node u, and shuffle the digits in these nodes as he wishes, incurring a cost of k⋅au. Here, he can choose k ranging from 1 to the size of the subtree of u.
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 u has digit cu written in it, or determine that it is impossible.
Input
First line contains a single integer n (1≤n≤2⋅105) denoting the number of nodes in the tree.
i-th line of the next n lines contains 3 space-separated integers ai, bi, ci (1≤ai≤109,0≤bi,ci≤1) — the cost of the i-th node, its initial digit and its goal digit.
Each of the next n−1 lines contain two integers u, v (1≤u,v≤n, u≠v), meaning that there is an edge between nodes u and v 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
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
4
input
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
24000
input
2
109 0 1
205 0 1
1 2
output
-1
解题思路:
比赛的时候思路差不多了,但是最后代码还是没有写对。
首先要统计 每颗子树的 未能匹配的0和1的数量。
然后需要 维护一个cost最小值,从根节点往下传,遇到最小值时就要更新。
此时 把不匹配的点 调整成匹配的点的代价是最小的 因为如果用父结点去调整的话 代价一定比他大。
然后 把已经匹配的点从不匹配的点中删去(比赛的时候没想到这里怎么操作!)
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int,int>
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define int long long
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qpow(int m, int k, int mod){int res=1,t=m;while(k){if(k&1)res=res*t%mod;t=t*t%mod;k>>=1;}return res;}
ll gcd(ll a,ll b){return b==0?a : gcd(b,a%b);}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll inv(ll x,ll m){return qpow(x,m-2,m)%m;}
const int N = 2e5+10;
int n,m,q;
int a[N];
int c[N];
int b[N];
int v[N];
int dp[N];
int cnt0[N];
int cnt1[N];
int res = 0;
string s;
map<int,int> mp;
vector<int> edge[N];
void dfs1(int x,int par,int cost)
{
int minn = min(c[x],cost);
int nn = edge[x].size();
if(b[x] != a[x])
{
cnt1[x] = (a[x] == 1);
cnt0[x] = (a[x] == 0);
}
if(par != -1 && nn == 1) return;
for(int i = 0 ; i < nn ; i ++)
{
int to = edge[x][i];
if(to != par)
{
dfs1(to,x,minn);
cnt0[x] += cnt0[to];
cnt1[x] += cnt1[to];
}
}
//cout<<minn<<" === "<<c[x]<<endl;
if(minn == c[x])
{
int mi = min(cnt1[x],cnt0[x]);
//cout<<minn<<" ---- "<<mi<<endl;
res += mi*minn*2;
cnt1[x] -= mi;
cnt0[x] -= mi;
}
}
signed main()
{
signed t = 1;
//cin>>t;
while(t--)
{
cin>>n;
res = 0;
int cnt11 = 0;
int cnt22 = 0;
for(int i = 1 ; i <= n ; i ++)
{
cin>>c[i]>>a[i]>>b[i];
cnt11 += a[i];
cnt22 += b[i];
}
for(int i = 0 ; i < n-1 ; i ++)
{
int x,y;
cin>>x>>y;
edge[x].pb(y);
edge[y].pb(x);
}
if(cnt11 != cnt22)
{
cout<<-1<<endl;
continue;
}
c[0] = 9999999999999;
dfs1(1,-1,9999999999999);
cout<<res<<endl;
}
return 0;
}