CF846E - Chemistry in Berland
题意:有一颗树,每个点上有 \(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的更多相关文章
- [CF846E]Chemistry in Berland题解
这题乍一看是一道水树形DP(其实事实上它确实是树形DP),然后设f[i]表示第i个点所多余/需要的材料,然后我们愉快的列出了式子: if(f[v]<0) f[u] += f[v] * edges ...
- Chemistry in Berland CodeForces - 846E
题目 题意: 有n种化学物质,第i种物质现有bi千克,需要ai千克.有n-1种,编号为2-n的转换方式,每种都为(x,k),第i行是编号为i+1的转换方式,编号为i的转换方式(xi,ki)表示ki千克 ...
- 【Educational Codeforces Round28】
咸鱼选手发现自己很久不做cf了,晚节不保. A.Curriculum Vitae 枚举一下间断点的位置. #include<bits/stdc++.h> using namespace s ...
- 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 ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 × ...
- CF 370B Berland Bingo
题目链接: 传送门 Berland Bingo time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- CF 445B DZY Loves Chemistry(并查集)
题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second memory limit per test:256 megabytes D ...
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- 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 ...
随机推荐
- 过两年 JVM 可能就要被 GraalVM 替代了
大家好,我是风筝,公众号「古时的风筝」,专注于 Java技术 及周边生态. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 今天说一说 Graal ...
- SQLMap入门——获取数据库中的表名
查询完数据库后,查询指定数据库中所有的表名 python sqlmap.py -u http://localhost/sqli-labs-master/Less-1/?id=1 -D xssplatf ...
- nuxt.js框架 如何打包 build
nuxt脚手架开发好项目后怎么打包 以下是脚手架的package.json部分代码 "scripts": { "dev": "cross-env NO ...
- csp-j 游记
### 初赛 day -7 ~ day -1 赛前集训,都很简单,什么二叉树,图论呀,轻松搞定.做了 $2008$ 至 $2015$ 年的普及组真题,都在 $50$ 分以上,感觉初赛稳了(坐标 $HN ...
- Django简介以及基本使用
目录 Django简介以及基本使用 一.django简介 1.web框架的本质是什么 ? 2.python主流web框架有那些 ? 3.web框架的推导过程 二.基本使用 1.运行django注意事项 ...
- Python启动HTTP服务进行文件传输
有时候局域网共享个东西不方便,尤其在服务器上的时候,总不能先下载下来,再上传上去吧,于是经常在这台机器用python起个http服务,然后去另一台机器直接访问,一来二去,妥试不爽,特进行一下分离 py ...
- 基本能看懂的C编译器,只有365行!
Fabrice Bellard is a French computer programmer known for writing FFmpeg, QEMU, and the Tiny C Compi ...
- .NET性能优化-使用RecyclableMemoryStream替代MemoryStream
提到MemoryStream大家可能都不陌生,在编写代码中或多或少有使用过:比如Json序列化反序列化.导出PDF/Excel/Word.进行图片或者文字处理等场景.但是如果使用它高频.大数据量处理这 ...
- [数据与分析可视化] D3入门教程3-d3中的数据操作
d3.js入门教程3-d3.js中的数据操作 文章目录 d3.js入门教程3-d3.js中的数据操作 数学操作 对象和数组 过滤Filtering 排序Sorting 映射group 循环loop 重 ...
- 8. 字符串转整数 (atoi)
题目 代码 class Solution { public: int myAtoi(string str) { int res=0,sign=1; int i=str.find_first_not_o ...