zoukankan      html  css  js  c++  java
  • (DFS)Educational Codeforces Round 28 E. Chemistry in Berland

    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 <cstdio>
      2 #include <iostream>
      3 #include <algorithm>
      4 #include <vector>
      5 #include <set>
      6 #include <map>
      7 #include <string>
      8 #include <cstring>
      9 #include <stack>
     10 #include <queue>
     11 #include <cmath>
     12 #include <ctime>
     13 #include <bitset>
     14 #include <utility>
     15 #include <assert.h>
     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<int,int> pii;
     25 typedef long long ll;
     26 typedef pair<ll,int> pli;
     27 typedef pair<ll,ll> 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<typename T>inline T abs(T a) {return a>0?a:-a;}
     38 template<class T> 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<pii> 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<n;i++){scanf("%d%d",&x,&k);edge[x].pb(mp(k,i+1));}
     96     dfs(1);
     97     st|=(b[1]<0);
     98     if(st)printf("NO
    ");
     99     else printf("YES
    ");
    100 
    101 }
    102 /*
    103 3
    104 1 1 1
    105 */

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

  • 相关阅读:
    react 中cookie的使用
    使用react-router-dom 来搭建react路由
    取消浏览器的自动缓存
    vue 的$nextTick方法,与$set()方法
    vue监听data以及prop中的参数变化
    iview树的修改某个节点,树刷新后自动展开你刚才展开的所有节点
    Linux vim常用命令
    Linux yum源配置
    博客园鼠标点击特效
    Linux 简介
  • 原文地址:https://www.cnblogs.com/quintessence/p/7492344.html
Copyright © 2011-2022 走看看