A11101 | Chemistry in Berland
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $x_{i}$ and $k_{i}$ that denote a possible transformation: $k_{i}$ kilograms of material $x_{i}$ can be transformed into $1$ kilogram of material $i$ , and $1$ kilogram of material $i$ can be transformed into $1$ kilogram of material $x_{i}$ . 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 $a_{i}$ kilograms of material $i$ , and the laboratory contains $b_{i}$ kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)?
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 $x_{i}$ and $k_{i}$ that denote a possible transformation: $k_{i}$ kilograms of material $x_{i}$ can be transformed into $1$ kilogram of material $i$ , and $1$ kilogram of material $i$ can be transformed into $1$ kilogram of material $x_{i}$ . 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 $a_{i}$ kilograms of material $i$ , and the laboratory contains $b_{i}$ kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)?
输入格式
The first line contains one integer number $n$ ( $1<=n<=10^{5}$ ) — the number of materials discovered by Berland chemists.
The second line contains $n$ integer numbers $b_{1},b_{2}...\ b_{n}$ ( $1<=b_{i}<=10^{12}$ ) — supplies of BerSU laboratory.
The third line contains $n$ integer numbers $a_{1},a_{2}...\ a_{n}$ ( $1<=a_{i}<=10^{12}$ ) — the amounts required for the experiment.
Then $n-1$ lines follow. $j$ -th of them contains two numbers $x_{j+1}$ and $k_{j+1}$ that denote transformation of $(j+1)$ -th material ( $1<=x_{j+1}<=j,1<=k_{j+1}<=10^{9}$ ).
The second line contains $n$ integer numbers $b_{1},b_{2}...\ b_{n}$ ( $1<=b_{i}<=10^{12}$ ) — supplies of BerSU laboratory.
The third line contains $n$ integer numbers $a_{1},a_{2}...\ a_{n}$ ( $1<=a_{i}<=10^{12}$ ) — the amounts required for the experiment.
Then $n-1$ lines follow. $j$ -th of them contains two numbers $x_{j+1}$ and $k_{j+1}$ that denote transformation of $(j+1)$ -th material ( $1<=x_{j+1}<=j,1<=k_{j+1}<=10^{9}$ ).
输出格式
Print YES if it is possible to conduct an experiment. Otherwise print NO.
输入输出样例
输入 #1
3 1 2 3 3 2 1 1 1 1 1
输出 #1
YES
输入 #2
3 3 2 1 1 2 3 1 1 1 2
输出 #2
NO
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted