题意:有一颗树,每个点上有 \(b_i\) 东西,从叶子往上的汇率是 \(1:1\),从父亲往下的汇率是 \(k:1\),求能否使每个点的东西都不少于 \(a_i\)。

我们发现,从上往下肯定是不划算的,我们一定优先从下往上。而且一条边只经过一次,因为给 \(a\) 个拿回 \(b\) 个会导致总量减少,是不优的。可以通过退流得到最优答案。

而因为我们不需要最小化交易次数,所以我们允许交易过程中出现负数,因为先给出再补足和先补足再给出是等价的。假设当前方案是 \(b\rightarrow a\),\(c\rightarrow b\),而在中间过程中 \(b\) 出现了负数。但是 \(b\rightarrow a\) 是 \(a\) 和 \(b\) 所属的树的唯一一次交集,也就是两边是独立的,我们就完全可以先让 \(c\rightarrow b\) 先完成不会有影响。

那么,就有显然的贪心策略。从叶子开始,如果当前点不足,就向上面申请得到东西,所有多出来的不论青红皂白全部给到上面。不需要避免节点的负数,正常计算,最终看点 \(1\) 是否满足要求。

typedef long long ll;
ll n,p[100005],k[100005];
ll A[100005],B[100005];
__int128 a[100005],b[100005];
vt<int>vv[100005];
inline void dfs(int x,int p){
for(auto j:vv[x])if(j!=p){
dfs(j,x);
}
if(b[x]<a[x]&&p!=0){
__int128 res=a[x]-b[x];
b[p]-=res*k[x];b[x]+=res;
if(b[p]<(-1e18)){
cout<<"NO"<<endl;
exit(0);
}
}else if(p!=0){
b[p]+=(b[x]-a[x]);
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
rp(i,n)cin>>B[i];
rp(i,n)cin>>A[i];
rp(i,n)a[i]=A[i],b[i]=B[i];
rep(i,2,n){
cin>>p[i]>>k[i];
vv[p[i]].pb(i);
}
dfs(1,0);
rp(i,n)if(b[i]<a[i]){
cout<<"NO"<<endl;
return 0;
}
cout<<"YES"<<endl;
return 0;
}
//Crayan_r

CF846E - Chemistry in Berland的更多相关文章

  1. [CF846E]Chemistry in Berland题解

    这题乍一看是一道水树形DP(其实事实上它确实是树形DP),然后设f[i]表示第i个点所多余/需要的材料,然后我们愉快的列出了式子: if(f[v]<0) f[u] += f[v] * edges ...

  2. Chemistry in Berland CodeForces - 846E

    题目 题意: 有n种化学物质,第i种物质现有bi千克,需要ai千克.有n-1种,编号为2-n的转换方式,每种都为(x,k),第i行是编号为i+1的转换方式,编号为i的转换方式(xi,ki)表示ki千克 ...

  3. 【Educational Codeforces Round28】

    咸鱼选手发现自己很久不做cf了,晚节不保. A.Curriculum Vitae 枚举一下间断点的位置. #include<bits/stdc++.h> using namespace s ...

  4. cf723d Lakes in Berland

    The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...

  5. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. codeforces 723D: Lakes in Berland

    Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...

  7. CF 370B Berland Bingo

    题目链接: 传送门 Berland Bingo time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  8. CF 445B DZY Loves Chemistry(并查集)

    题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second     memory limit per test:256 megabytes D ...

  9. 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry

    C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...

  10. Codeforces Round #Pi (Div. 2) B. Berland National Library set

    B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

随机推荐

  1. python循环结构之for循环

    在python中,for循环是应用非常广的循环语句,遍历字典.遍历列表等等... # for语句结构 for 遍历 in 序列: 执行语句 遍历字典 lipsticks = {"Chanel ...

  2. Elasticsearch查询及聚合类DSL语句宝典

    作者:京东科技 纪海雨 前言 随着使用es场景的增多,工作当中避免不了去使用es进行数据的存储,在数据存储到es当中以后就需要使用DSL语句进行数据的查询.聚合等操作,DSL对SE的意义就像SQL对M ...

  3. Spark框架下均值漂移算法对舆情聚类的分析

    知网链接 原文链接 张京坤,  王怡怡 软件导刊   2022年21卷第6期 页码:141-146 DOI:10.11907/rjdk.211889    中图分类号:TP274 纸质出版日期:202 ...

  4. “It is required that your private key files are NOT accessible by others. This private key will be ignored.”

    Windows Terminal 通过密钥登录远程vps时提示: "It is required that your private key files are NOT accessible ...

  5. 每个Java程序员都必须知道的四种负载均衡算法

    前言 一般来说,我们在设计系统的时候,为了系统的高扩展性,会尽可能的创建无状态的系统,这样我们就可以采用集群的方式部署,最终很方便的根据需要动态增减服务器数量.但是,要使系统具有更好的可扩展性,除了无 ...

  6. [LeetCode]最大连续1的个数

    题目 代码 class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { int length= ...

  7. vue+div.canvas图像标注功能实现

    main.js import Vue from 'vue' import 'vueui-widgets/dist/index.css' import VueUI from 'vueui-widgets ...

  8. 如何解决github下载很慢的问题?(已经解决)

    目的是为了解决GitHub致命的下载速度慢的问题 方法 通过码云来导入github,通过码云下载 1.在github上面找到自己想要的项目 这一步略过 2.复制github项目上面的网页链接 3.打开 ...

  9. Spring在Filter中记录Web请求Request和返回Response的内容及时长

    1 简介 在Spring MVC中,我们有时需要记录一下请求和返回的内容,方便出现问题时排查.比较Header.Request Body等.这些在Controller也可以记录,但在Filter中会更 ...

  10. Node.js学习笔记----day03

    认真学习,认真记录,每天都要有进步呀!!! 加油叭!!! 一.Node中的模块系统 使用Node编写应用程序主要就是在使用 EcmaScript 和浏览器不一样的是,在Node中没有BOM.DOM 核 ...