这题乍一看是一道水树形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. Eclipse设置控制台日志输出位置

    1.选择服务器配置 2.设置输出文件路径

  2. winform项目中开发的一套UI控件库

    https://github.com/houyhea/winform-control-lib winform-control-lib 曾经在一个winform项目中开发的一套UI控件库 类图:  效果 ...

  3. Krypton Suite of .NET WinForms Controls

    The Krypton Suite of .NET WinForms controls are now freely available for use in personal or commeric ...

  4. Learn Python the hard way, ex45 对象、类、以及从属关系

    #!/usr/bin/python #coding:utf-8 # animal is-a object(yes,sort of sonfusing)look at the extra credit ...

  5. Vue的生命周期(在其他地方看到一份非常好又详细的详解)

    链接地址:https://segmentfault.com/a/1190000011381906 首先,每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期.首先看一张图 ...

  6. Nginx配置之rewrite、proxy_pass、upstream、location

    如图,这是Nginx的配置文件nginx.conf中的一段配置代码. 在http段中定义了一个名为webservers的upstream模块,主要用于负载均衡. 在server模块中,定义了一个loc ...

  7. jQuery源码分析系列——来自Aaron

    jQuery源码分析系列——来自Aaron 转载地址:http://www.cnblogs.com/aaronjs/p/3279314.html 版本截止到2013.8.24 jQuery官方发布最新 ...

  8. [Python3 练习] 004 水仙花数

    题目:水仙花数 (1) 描述 水仙花数各位的数字的立方之和等于自身 如 153 为水仙花数,因为 153 = 1^3 + 5^3 + 3^3 (2) 要求 找到所有的三位数的水仙花数 (3) 程序 # ...

  9. JAVA总结--代码规范

    一.命名规范 1.标识符:统一.达意.简洁 统一:一个词有多种表达方式,不求最好,但求统一:例:供应商,既可以用supplier,也可以用provider,选择一种统一使用: 达意:明确表达其意义,正 ...

  10. Spring MVC-学习笔记(3)参数绑定注解、HttpMessageConverter<T>信息转换、jackson、fastjson、XML

    1.参数绑定注解 1>@RequestParam: 用于将指定的请求参数赋值给方法中的指定参数.支持的属性: 2>@PathVariable:可以方便的获得URL中的动态参数,只支持一个属 ...