E. Chemistry in Berland

Codeforces
IDCF846E
Time2000ms
Memory256MB
Difficulty
dfs and similargreedytrees
English · Original
Chinese · Translation
Formal · Original
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)? ## 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 _b_1, _b_2... _b__n_ (1 ≤ _b__i_ ≤ 1012) — supplies of BerSU laboratory. The third line contains _n_ integer numbers _a_1, _a_2... _a__n_ (1 ≤ _a__i_ ≤ 1012) — 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 ≤ 109). ## Output Print _YES_ if it is possible to conduct an experiment. Otherwise print _NO_. [samples]
Igor 是贝尔兰国立大学(BerSU)化学系的研究生。他需要进行一项复杂的实验来撰写论文,但 BerSU 的实验室并不包含该实验所需的所有材料。 幸运的是,化学定律允许物质转化(是的,贝尔兰的化学与我们的不同)。但转化规则有些奇怪。 贝尔兰化学家已发现 #cf_span[n] 种物质,按发现顺序编号。每种物质可以转化为其他某种物质(或反之)。形式上,对于每个 #cf_span[i] #cf_span[(2 ≤ i ≤ n)],存在两个数 #cf_span[xi] 和 #cf_span[ki],表示一种可能的转化:#cf_span[ki] 千克的物质 #cf_span[xi] 可以转化为 #cf_span[1] 千克的物质 #cf_span[i],而 #cf_span[1] 千克的物质 #cf_span[i] 可以转化为 #cf_span[1] 千克的物质 #cf_span[xi]。BerSU 的化学处理设备仅允许进行这样的转化:生成的物质质量始终为整数千克。 对于每个 #cf_span[i] (#cf_span[1 ≤ i ≤ n]),Igor 知道实验需要 #cf_span[ai] 千克的物质 #cf_span[i],而实验室现有 #cf_span[bi] 千克该物质。是否可以通过转化某些物质(或不转化)来完成实验? 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105]) —— 贝尔兰化学家发现的物质种类数。 第二行包含 #cf_span[n] 个整数 #cf_span[b1, b2... bn] (#cf_span[1 ≤ bi ≤ 1012]) —— BerSU 实验室的现有库存。 第三行包含 #cf_span[n] 个整数 #cf_span[a1, a2... an] (#cf_span[1 ≤ ai ≤ 1012]) —— 实验所需的量。 接下来有 #cf_span[n - 1] 行。第 #cf_span[j] 行包含两个数 #cf_span[xj + 1] 和 #cf_span[kj + 1],表示第 #cf_span[(j + 1)] 种物质的转化关系(#cf_span[1 ≤ xj + 1 ≤ j, 1 ≤ kj + 1 ≤ 109])。 如果可以完成实验,请输出 _YES_;否则输出 _NO_。 ## Input 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105]) —— 贝尔兰化学家发现的物质种类数。第二行包含 #cf_span[n] 个整数 #cf_span[b1, b2... bn] (#cf_span[1 ≤ bi ≤ 1012]) —— BerSU 实验室的现有库存。第三行包含 #cf_span[n] 个整数 #cf_span[a1, a2... an] (#cf_span[1 ≤ ai ≤ 1012]) —— 实验所需的量。接下来有 #cf_span[n - 1] 行。第 #cf_span[j] 行包含两个数 #cf_span[xj + 1] 和 #cf_span[kj + 1],表示第 #cf_span[(j + 1)] 种物质的转化关系(#cf_span[1 ≤ xj + 1 ≤ j, 1 ≤ kj + 1 ≤ 109])。 ## Output 如果可以完成实验,请输出 _YES_;否则输出 _NO_。 [samples]
**Definitions:** - Let $ n \in \mathbb{N} $ be the number of materials, indexed $ 1 $ to $ n $. - For each material $ i \in \{1, 2, \dots, n\} $: - $ b_i \in \mathbb{N} $: initial supply in the lab. - $ a_i \in \mathbb{N} $: required amount for the experiment. - For each $ i \in \{2, 3, \dots, n\} $, a transformation rule is given by: - $ x_i \in \{1, 2, \dots, i-1\} $: source material. - $ k_i \in \mathbb{N} $: conversion factor such that: - $ k_i $ kg of material $ x_i $ → 1 kg of material $ i $, - 1 kg of material $ i $ → 1 kg of material $ x_i $. **Constraints:** - All transformations must result in integer kilograms. - Transformation is bidirectional but with asymmetric mass ratios: - Forward: $ k_i \cdot m_{x_i} \to m_i $ (consumes $ k_i \cdot m $ kg of $ x_i $ to produce $ m $ kg of $ i $). - Backward: $ m_i \to m_{x_i} $ (consumes $ m $ kg of $ i $ to produce $ m $ kg of $ x_i $). **Objective:** Determine whether it is possible to achieve $ a_i \leq \text{final amount of } i $ for all $ i \in \{1, \dots, n\} $, via any sequence of valid transformations. **Graph Structure:** - The transformation rules define a directed tree rooted at material 1: for each $ i \geq 2 $, there is an edge $ x_i \to i $ with weight $ k_i $, meaning material $ i $ depends on $ x_i $. - The graph is a tree with root 1 and edges $ x_i \to i $ for $ i = 2 $ to $ n $. **Formal Problem:** Let $ d_i = a_i - b_i $ be the deficit (if positive) or surplus (if negative) of material $ i $. We must determine if there exists an assignment of integer flows $ f_{i \to x_i} \in \mathbb{Z} $ (net kg of material $ i $ sent to $ x_i $) and $ f_{x_i \to i} \in \mathbb{Z} $ (net kg of material $ x_i $ sent to $ i $) such that: - For each $ i \geq 2 $, define net flow from $ x_i $ to $ i $: $ \Delta_i \in \mathbb{Z} $, meaning: - If $ \Delta_i > 0 $: $ \Delta_i $ kg of material $ i $ are produced from $ k_i \cdot \Delta_i $ kg of $ x_i $. - If $ \Delta_i < 0 $: $ |\Delta_i| $ kg of material $ i $ are converted to $ |\Delta_i| $ kg of $ x_i $. Then the net change at each node $ i $ must satisfy: $$ b_i + \sum_{j: x_j = i} (-k_j \cdot \Delta_j) + \Delta_i = a_i $$ That is: $$ \Delta_i - \sum_{j: x_j = i} k_j \cdot \Delta_j = a_i - b_i = d_i $$ This gives a system of linear equations over $ \mathbb{Z} $, one per node $ i = 1, \dots, n $, with the constraint that for $ i = 1 $, there is no outgoing transformation (no $ \Delta_1 $ defined via a parent), so: - For $ i = 1 $: $ -\sum_{j: x_j = 1} k_j \cdot \Delta_j = d_1 $ - For $ i \geq 2 $: $ \Delta_i - \sum_{j: x_j = i} k_j \cdot \Delta_j = d_i $ We can solve this system **bottom-up** (from leaves to root) because the dependency graph is a tree. **Recursive Formulation (Bottom-Up):** Define for each node $ i $, the net **deficit** that must be resolved by its parent: $ r_i \in \mathbb{Z} $, representing the net amount of material $ i $ that must be supplied to (if positive) or sent to (if negative) its parent $ x_i $, after satisfying its own requirement $ a_i $ and accounting for all descendants. We compute $ r_i $ recursively: - For a leaf $ i $: $ r_i = d_i = a_i - b_i $ - For an internal node $ i $: First, collect all child deficits: each child $ j $ with $ x_j = i $ contributes a requirement of $ k_j \cdot r_j $ kg of material $ i $ to produce $ r_j $ kg of material $ j $ (since $ k_j $ kg of $ i $ → 1 kg of $ j $, so to get $ r_j $ kg of $ j $, we need $ k_j \cdot r_j $ kg of $ i $). Then the net deficit at node $ i $ is: $$ r_i = d_i + \sum_{j: x_j = i} k_j \cdot r_j $$ This is because: - $ d_i $: direct deficit/surplus at $ i $, - $ \sum k_j \cdot r_j $: total $ i $-kg needed to produce the net $ r_j $ kg of each child $ j $. Then, if $ r_i > 0 $, we need $ r_i $ kg of material $ i $ from its parent. If $ r_i < 0 $, we can send $ |r_i| $ kg of material $ i $ to its parent. **Crucially**, we must be able to achieve integer flows — which is naturally satisfied if all $ r_i \in \mathbb{Z} $, which they are, since all inputs are integers. **Final Condition:** After computing $ r_1 $ for the root (material 1), the experiment is possible **if and only if**: $$ r_1 \leq 0 $$ Because: - $ r_1 $ represents the net amount of material 1 that must be supplied from outside the system (since root has no parent). - If $ r_1 > 0 $, we need more of material 1 than we can generate internally — impossible. - If $ r_1 \leq 0 $, we have sufficient or surplus material 1 (can discard excess). --- **Final Formal Statement:** Let $ T $ be a tree on $ n $ nodes with root 1, and for each $ i \in \{2, \dots, n\} $, an edge $ x_i \to i $ with weight $ k_i $. Define $ d_i = a_i - b_i $. Define recursively for each node $ i $: $$ r_i = d_i + \sum_{\substack{j=2 \\ x_j = i}}^n k_j \cdot r_j $$ Then the answer is **YES** if and only if: $$ r_1 \leq 0 $$ Otherwise, **NO**.
Samples
Input #1
3
1 2 3
3 2 1
1 1
1 1
Output #1
YES
Input #2
3
3 2 1
1 2 3
1 1
1 2
Output #2
NO
API Response (JSON)
{
  "problem": {
    "name": "E. Chemistry in Berland",
    "description": {
      "content": "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 ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF846E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Igor 是贝尔兰国立大学(BerSU)化学系的研究生。他需要进行一项复杂的实验来撰写论文,但 BerSU 的实验室并不包含该实验所需的所有材料。\n\n幸运的是,化学定律允许物质转化(是的,贝尔兰的化学与我们的不同)。但转化规则有些奇怪。\n\n贝尔兰化学家已发现 #cf_span[n] 种物质,按发现顺序编号。每种物质可以转化为其他某种物质(或反之)。形式上,对于每个 #cf_span[i] #cf_...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions:**\n\n- Let $ n \\in \\mathbb{N} $ be the number of materials, indexed $ 1 $ to $ n $.\n- For each material $ i \\in \\{1, 2, \\dots, n\\} $:\n  - $ b_i \\in \\mathbb{N} $: initial supply in the lab...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments