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

if(f[v]<0)
f[u] += f[v] * edges[c_e].dis;
else
f[u] += f[v];

然后放到CF上,直接WA,十分自闭,于是我试图define int long long,还是没过,仔细一想(偷瞄了一眼CF的数据),发现f数组有可能会爆long long,怎么办?高精? 显然这不太现实,于是想着如果它要爆了就把它置回边缘,实测不会WA

贴个代码

#include <cstdio>
#define ll long long const ll INF = (1ll << 62); using namespace std; inline ll read(){
ll x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
} struct Edge{
int to; ll dis; int next;
} edges[2000005]; int head[2000005], edge_num = 0; inline void addEdge(int u, int v, ll dis){
edges[++edge_num] = (Edge){v, dis, head[u]};
head[u] = edge_num;
} ll a[2000005], b[2000005];
ll f[2000005]; void DFS(int u){
//置为所需
f[u] = b[u] - a[u];
for(int c_e = head[u]; c_e; c_e = edges[c_e].next){
int v = edges[c_e].to;
DFS(v);
if(f[v] < 0){
//需要 u 去补
if (INF / edges[c_e].dis <= -f[v])
f[u] = -INF;
else{
f[u] += f[v] * edges[c_e].dis;
if (f[u] < -INF)
f[u] = -INF;
}
}
else{
//反向转换
f[u] += f[v];
}
}
} int main(){
int n = read();
for (int i = 1; i <= n; ++i)
b[i] = read();
for (int i = 1; i <= n; ++i)
a[i] = read();
for (int i = 2; i <= n; ++i){
int u = read(); ll dis = read();
addEdge(u, i, dis);
}
DFS(1);
if (f[1] >= 0)
printf("YES");
else
printf("NO");
return 0;
}

[CF846E]Chemistry in Berland题解的更多相关文章

  1. Chemistry in Berland CodeForces - 846E

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

  2. 【Educational Codeforces Round28】

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

  3. CF1494B Berland Crossword 题解

    Content 有一种叫做 Berland crossword 的拼图游戏.这个拼图由 \(n\) 行 \(n\) 列组成,你可以将里面的一些格子涂成黑色.现在给出 \(T\) 个这样的拼图,每个拼图 ...

  4. 【题解】Berland.Taxi Codeforces 883L 模拟 线段树 堆

    Prelude 题目传送门:ヾ(•ω•`)o Solution 按照题意模拟即可. 维护一个优先队列,里面装的是正在运营中的出租车,关键字是乘客的下车时间. 维护一个线段树,第\(i\)个位置表示第\ ...

  5. 题解 CF1359A 【Berland Poker】

    题意 给出 \(n,m,k\) ,表示 \(k\) 名玩家打牌,共 \(n\) 张牌,\(m\) 张王,保证 \(k|n\) ,记得分为 拿到最多王的玩家手中王数 \(-\)拿到第二多王的玩家手中的王 ...

  6. [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry

    链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...

  7. 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 ...

  8. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

  9. Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力

    C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...

随机推荐

  1. Linux_SquidProxyServer代理服务器

    目录 目录 Squid proxy server Web proxy server operating principle Squid features Setup squid server Setu ...

  2. RNN系列

    漫谈RNN之梯度消失及梯度爆炸:http://bbs.imefuture.com/article/4405 漫谈RNN之长短期记忆模型LSTM:http://bbs.imefuture.com/art ...

  3. js高级写法

    名称 一般写法 优化 取整(不四舍五入) parseInt(a,10); //Before Math.floor(a); //Before a>>0; //Before ~~a; //Af ...

  4. value_counts()函数

    value_counts函数用于统计dataframe或series中不同数或字符串出现的次数 ascending=True时,按升序排列. normalize=True时,可计算出不同字符出现的频率 ...

  5. 类HashMap

    /* * Map集合的特点 * 将键映射值的对象,一个映射不能包含重复的值:每个键最多只能映射到一个值 * * Map集合和Collection集合的区别? * Map集合存储元素是成对出现的,Map ...

  6. 小白学Python(13)——pyecharts 绘制 柱状图/条形图 Bar

    Bar-基本示例 from example.commons import Faker from pyecharts import options as opts from pyecharts.char ...

  7. 7、purge_haplogs 基因组去冗余

    1.下载安装 https://bitbucket.org/mroachawri/purge_haplotigs/wiki/Install 1.Dependencies (in no particula ...

  8. fullpage.js版本3.0.5报错问题(licenseKey)

    在文件里搜索licenseKey,删除!不会对程序造成任何影响

  9. noscript

    <noscript> <article id="noscript" class="error info_panel"> <head ...

  10. 02cython调用c++文件

    https://blog.csdn.net/ztf312/article/details/77340300 此时用python setup.py build_ext --inplace编译时报错如下: ...