博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(DFS)Educational Codeforces Round 28 E. Chemistry in Berland
阅读量:4343 次
发布时间:2019-06-07

本文共 4262 字,大约阅读时间需要 14 分钟。

E. Chemistry in Berland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment.

Fortunately, chemical laws allow material transformations (yes, chemistry in Berland differs from ours). But the rules of transformation are a bit strange.

Berland chemists are aware of n materials, numbered in the order they were discovered. Each material can be transformed into some other material (or vice versa). Formally, for each i (2 ≤ i ≤ n) there exist two numbers xi and ki that denote a possible transformation: ki kilograms of material xi can be transformed into 1 kilogram of material i, and 1 kilogram of material i can be transformed into 1 kilogram of material xi. Chemical processing equipment in BerSU allows only such transformation that the amount of resulting material is always an integer number of kilograms.

For each i (1 ≤ i ≤ n) Igor knows that the experiment requires ai kilograms of material i, and the laboratory contains bi kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)?

Input

The first line contains one integer number n (1 ≤ n ≤ 105) — the number of materials discovered by Berland chemists.

The second line contains n integer numbers b1, b2... bn (1 ≤ bi ≤ 1012) — supplies of BerSU laboratory.

The third line contains n integer numbers a1, a2... an (1 ≤ ai ≤ 1012) — the amounts required for the experiment.

Then n - 1 lines follow. j-th of them contains two numbers xj + 1 and kj + 1 that denote transformation of (j + 1)-th material (1 ≤ xj + 1 ≤ j, 1 ≤ kj + 1 ≤ 109).

Output

Print YES if it is possible to conduct an experiment. Otherwise print NO.

Examples
Input
3 1 2 3 3 2 1 1 1 1 1
Output
YES
Input
3 3 2 1 1 2 3 1 1 1 2
Output
NO

对于能用若干个i转化为j的点i点j连一条边,总共n-1条,且满足xj+1<=j,故恰为一棵树。对于叶子节点,不需要转化成别的,只需要使自己满足a[i]的要求即可,少的部分从其父节点取,多的部分退回给父节点。之后就可以删去这个节点。依次进行下去,只要根节点满足a[1]的要求即可。过程中需要注意可能会爆long long,故先做b[i]-a[i],并且每个点用double类型

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 using namespace std; 17 #define rank rankk 18 #define mp make_pair 19 #define pb push_back 20 #define xo(a,b) ((b)&1?(a):0) 21 #define tm tmp 22 //#define LL ll 23 typedef unsigned long long ull; 24 typedef pair
pii; 25 typedef long long ll; 26 typedef pair
pli; 27 typedef pair
pll; 28 const int INF=0x3f3f3f3f; 29 const ll INFF=0x3f3f3f3f3f3f3f3fll; 30 const int MAX=1e5+5; 31 const int MAXN=1000000; 32 const int MAX_N=MAX; 33 const ll MOD=1e9+7; 34 const long double pi=acos(-1.0); 35 //const double eps=0.00000001; 36 int gcd(int a,int b){ return b?gcd(b,a%b):a;} 37 template
inline T abs(T a) { return a>0?a:-a;} 38 template
inline 39 void read(T& num) { 40 bool start=false,neg=false; 41 char c; 42 num=0; 43 while((c=getchar())!=EOF) { 44 if(c=='-') start=neg=true; 45 else if(c>='0' && c<='9') { 46 start=true; 47 num=num*10+c-'0'; 48 } else if(start) break; 49 } 50 if(neg) num=-num; 51 } 52 inline ll powMM(ll a,ll b,ll M){ 53 ll ret=1; 54 a%=M; 55 // b%=M; 56 while (b){ 57 if (b&1) ret=ret*a%M; 58 b>>=1; 59 a=a*a%M; 60 } 61 return ret; 62 } 63 void open() 64 { 65 freopen("1009.in","r",stdin); 66 freopen("out.txt","w",stdout); 67 } 68 int n,x,k; 69 ll b[MAX],sum,tem; 70 double tm; 71 vector
edge[MAX]; 72 bool st; 73 void dfs(int now) 74 { 75 if(st)return; 76 for(auto g:edge[now]) 77 { 78 int to=g.second,cost=g.first; 79 dfs(to); 80 if(b[to]>0)b[now]+=b[to];//多的转移回来 81 else 82 { 83 tm=1.0*b[to]*cost; 84 if(tm<-sum){st=true;return;} 85 else 86 b[now]+=b[to]*cost; 87 } 88 } 89 } 90 int main() 91 { 92 scanf("%d",&n); 93 for(int i=1;i<=n;i++){scanf("%I64d",&b[i]);sum+=b[i];} 94 for(int i=1;i<=n;i++){scanf("%I64d",&tem);b[i]-=tem;} 95 for(int i=1;i

 

判别一下是否其已经超出sum范围,已超出的就已经是NO了。

 

转载于:https://www.cnblogs.com/quintessence/p/7492344.html

你可能感兴趣的文章
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>