Tree Shuffling CodeForces - 1363E(贪心)

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
Note
The tree corresponding to samples 1 and 2 are:
在这里插入图片描述

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

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

In sample 3, it is impossible to get the desired digits, because there is no node with digit 1 initially.
思路:对于以x为根的子树,能进行交换的条件是什么?这是我们需要思考的。以x为根的子树,可以在这个子树中交换的条件是val[x](在x子树中交换一个代价)在x的祖先中是最小的,那么就可以交换,否则的话,就不能交换(因为在以祖先为根的子树中交换更优)。这是我们做这个题目的准则。
对于如何查看是否是最小的,我的做法是记录一个最小值_min,代表祖先中最小的代价,然后二分找出当前点的位置,如果小于_min的话,就说明可以交换了。
-1的情况是什么呢?那就是需要交换的0和1的个数不一样多,就是-1.
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define PLL pair<int,int>
using namespace std;

const int maxx=2e5+100;
struct edge{
	int to,next;
}e[maxx<<1];
struct node{
	int val;
	int st,ed;
}p[maxx];
struct tt{
	int x0,x1;
	tt(int a=0,int b=0){x0=a,x1=b;}
};
int head[maxx<<1],vis[maxx],a[maxx];
int n,tot,len;

inline void init()
{
	memset(head,-1,sizeof(head));
	memset(vis,0,sizeof(vis));
	tot=0;
}
inline void add(int u,int v)
{
	e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
inline tt dfs(int u,int f,ll &ans,int _min)
{
	int x=lower_bound(a+1,a+1+len,p[u].val)-a;
	tt c;
	if(p[u].st==p[u].ed){}
	else if(p[u].st) c.x1++;
	else c.x0++;
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		tt zz=dfs(to,u,ans,min(_min,x));
		c.x0+=zz.x0;
		c.x1+=zz.x1;
	}
	if(x<=_min)
	{
		int cmin=min(c.x0,c.x1);
		ans+=(ll)p[u].val*(ll)cmin*2;
		c.x0-=cmin;
		c.x1-=cmin;
	}
	return c;
}
int main()
{
	scanf("%d",&n);
	int cnt0=0,cnt1=0;
	for(int i=1;i<=n;i++) 
	{
		scanf("%d%d%d",&p[i].val,&p[i].st,&p[i].ed);
		if(p[i].st==p[i].ed) continue;
		else if(p[i].st==0) cnt0++;
		else cnt1++;
	}
	init();
	int x,y;
	for(int i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x);
	if(cnt1!=cnt0) cout<<"-1"<<endl;
	else
	{
		for(int i=1;i<=n;i++) a[i]=p[i].val;
		sort(a+1,a+1+n);
		len=unique(a+1,a+1+n)-a-1;
		ll ans=0;
		dfs(1,0,ans,lower_bound(a+1,a+1+len,p[1].val)-a);
		cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值