Submission #1093059


Source Code Expand

// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else 
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
#define N 101010
void build(){

}
LL n , a[ N ];
vector<int> v[ N ];
void init(){
  n = getint();
  for( int i = 1 ; i <= n ; i ++ )
    a[ i ] = getint();
  for( int i = 1 ; i < n ; i ++ ){
    int ai = getint();
    int bi = getint();
    v[ ai ].push_back( bi );
    v[ bi ].push_back( ai );
  }
}
LL go( int now , int prt ){
  bool leaf = true;
  LL ret = 0 , mx = 0 , res = 0;
  for( int son : v[ now ] ){
    if( son == prt ) continue;
    leaf = false;
    LL tmp = go( son , now );
    if( tmp < 0 ){
      puts( "NO" );
      Bye;
    }
    LL mr = min( ret , tmp );
    mr = min( mr , ( res + tmp ) / 2 );
    ret += tmp;
    mx += mr;
    res = res + tmp - mr - mr;
  }
  if( leaf ) return a[ now ];
  if( ret < a[ now ] || ret - a[ now ] > mx ){
    puts( "NO" );
    Bye;
  }
  return ret - 2 * ( ret - a[ now ] );
}
void solve(){
  int rt = -1;
  for( int i = 1 ; i <= n ; i ++ )
    if( v[ i ].size() > 1u ){
      rt = i;
      break;
    }
  if( rt == -1 ){
    puts( a[ 1 ] == a[ 2 ] ? "YES" : "NO" );
    Bye;
  }
  if( go( rt , rt ) == 0 )
    puts( "YES" );
  else
    puts( "NO" );
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}

Submission Info

Submission Time
Task C - Cleaning
User eddy1021
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2881 Byte
Status AC
Exec Time 46 ms
Memory 13184 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 48
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt
Case Name Status Exec Time Memory
in1.txt AC 38 ms 6656 KB
in10.txt AC 39 ms 6656 KB
in11.txt AC 46 ms 11904 KB
in12.txt AC 45 ms 11904 KB
in13.txt AC 40 ms 13184 KB
in14.txt AC 39 ms 12288 KB
in15.txt AC 5 ms 2560 KB
in16.txt AC 5 ms 2560 KB
in17.txt AC 5 ms 2560 KB
in18.txt AC 5 ms 2560 KB
in19.txt AC 37 ms 6656 KB
in2.txt AC 38 ms 6656 KB
in20.txt AC 37 ms 6656 KB
in21.txt AC 36 ms 6656 KB
in22.txt AC 38 ms 6656 KB
in23.txt AC 37 ms 6656 KB
in24.txt AC 37 ms 6656 KB
in25.txt AC 5 ms 2560 KB
in26.txt AC 38 ms 6656 KB
in27.txt AC 39 ms 6656 KB
in28.txt AC 5 ms 2560 KB
in29.txt AC 32 ms 6656 KB
in3.txt AC 39 ms 6656 KB
in30.txt AC 5 ms 2560 KB
in31.txt AC 38 ms 6656 KB
in32.txt AC 36 ms 6656 KB
in33.txt AC 39 ms 6656 KB
in34.txt AC 38 ms 6656 KB
in35.txt AC 40 ms 6656 KB
in36.txt AC 39 ms 6656 KB
in37.txt AC 5 ms 2688 KB
in38.txt AC 37 ms 6656 KB
in39.txt AC 38 ms 6656 KB
in4.txt AC 39 ms 6656 KB
in40.txt AC 36 ms 6656 KB
in41.txt AC 36 ms 6656 KB
in42.txt AC 39 ms 6656 KB
in43.txt AC 37 ms 6656 KB
in44.txt AC 39 ms 6656 KB
in45.txt AC 39 ms 6656 KB
in5.txt AC 39 ms 6656 KB
in6.txt AC 40 ms 6656 KB
in7.txt AC 35 ms 6144 KB
in8.txt AC 13 ms 3584 KB
in9.txt AC 38 ms 6656 KB
sample1.txt AC 5 ms 2560 KB
sample2.txt AC 5 ms 2560 KB
sample3.txt AC 5 ms 2560 KB