当前位置:网站首页>【codeforces round#800 D题 Fake Plastic Trees】树上贪心
【codeforces round#800 D题 Fake Plastic Trees】树上贪心
2022-07-17 22:57:00 【宇智波一打七~】
题目链接
题意:
给你一棵树,树上有n个节点,树的根节点是1,一开始树上的每个节点的权值都是0,现在有一种操作,选择一个节点,使得根节点到这个节点的路径上的所有节点的权值都增加,增加的幅度从根节点到这个节点呈不递减趋势,每个节点的权值有一个范围,在题目输入中给出,现在问你最少需要多少个这种操作才能使得每个节点的权值都能在自己的范围里。
分析:
叶节点被更新到的机会只有一次,就是选择叶节点的时候,那么根据贪心的思想,首先把所有的叶节点都给更新到范围最大值,这里为什么要更新到最大值呢?因为它如果大了,它的父节点们就会有更大的空间,所以咱们可以从叶节点开始,每个节点的白嫖来的权值就是它的所有子节点的增加的权值之和,如果这个白嫖来的权值能在范围内的话就不用额外再来一次,这是根据贪心的思想,如果白嫖来的权值不能满足这个节点的权值范围的最小值的话那就对这个点进行一次这个操作,那么就可以用一遍dfs来实现这个思想,下面请看代码。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 200010,M = N+N;
int h[N],e[M],ne[M],idx;
int ans,n;
int l[N],r[N];
void add(int a,int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
int dfs(int u,int fa){
int sum = 0;
for(int i=h[u];i!=-1;i=ne[i]){
int j = e[i];
if(j == fa) continue;
sum += dfs(j,u);
}
if(sum < l[u]){
ans++;
return r[u];
}
return min(sum,r[u]);
}
void solve(){
ans = 0;
scanf("%lld",&n);
for(int i=1;i<=n;i++){
h[i] = -1;
}
for(int i=2;i<=n;i++){
int x;
scanf("%lld",&x);
add(i,x);
add(x,i);
}
for(int i=1;i<=n;i++) scanf("%lld%lld",&l[i],&r[i]);
dfs(1,0);
cout<<ans<<endl;
}
signed main(){
int _;
scanf("%lld",&_);
while(_--) solve();
return 0;
}
边栏推荐
- PKI: TLS handshake
- Is it safe for xinjimin to buy funds on Alipay
- Zabbix实现对Redis的监控
- Unveil the mystery of service grid istio service mesh
- Is it safe to buy funds in a securities account? I want to make a fixed investment in the fund
- 原始套接字
- Leetcode 1275. 找出井字棋的获胜者
- 2020 ICPC Asia East Continent Final G. Prof. Pang‘s sequence 线段树/扫描线
- B树
- C - matrix chain multiplexing (Application of stack)
猜你喜欢

Single channel 6Gsps 12 bit AD acquisition and single channel 6Gsps 16 bit Da (ad9176) output sub card based on vita57.1 standard

Top domestic experts gathered in Guangzhou to discuss the safety application of health care data

ORA-08103

Face technology: the picture of unclear people is repaired into a high-quality and high-definition image framework (with source code download)
![2021.07.13 [station B] collapsed like this](/img/af/d7d6ae059b4bc2d76c6ea0edb64c82.png)
2021.07.13 [station B] collapsed like this
![[XSS range 10-14] insert when you see parameters: find hidden parameters and various attributes](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[XSS range 10-14] insert when you see parameters: find hidden parameters and various attributes

3438. Number system conversion

SBOM (software bill of materials)

Wechat applet 7 cloud storage

Zabbix实现对Redis的监控
随机推荐
UVA - 12096 The SetStack Computer
Wechat applet 7 cloud storage
Redis高频面试题完整版
傅里叶变换的再理解
Achieve the effect of software login account by authorizing wechat ~ ~ unfinished
Cilium & Hubble
2020 ICPC Asia East continuous final g. Prof. Pang's sequence line segment tree / scan line
跨域与CORS
测试图片
马走斜日(回溯法)
全排列(深度优先,排列树)
Wechat applet 8 cloud function
Wechat applet 9 release code
5-21 interceptor
暑期第三周总结
Internship is a barrier to enter the society
3438. Number system conversion
微信小程序8-云函数
A - Trees on the level(树的层序遍历)
GYM103660E.Disjoint Path On Tree 树上计数