Submission #1693676


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
ll n,a[111111];
vector<int> v[111111];

ll dfs(int x,int p) {
  ll cnt=0;
  bool f=0;
  rep(i,v[x].size()) {
    int y=v[x][i];
    if(y==p) continue;
    f=1;
    ll d=dfs(y,x);
    if(d>a[x]) return MAX;
    cnt+=d;
  }
  if(a[x]>cnt||cnt>a[x]*2) return MAX;
  if(f) a[x]-=cnt-a[x];
  return a[x];
}

void Main() {
  cin >> n;
  for(int i=0; i<n; i++) cin >> a[i];
  for(int i=0; i<n-1; i++) {
    int x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(y);
    v[y].pb(x);
  }
  if(n==2) {
    if(a[0]==a[1]) pr("YES");
    else pr("NO");
    return;
  }
  rep(i,n) {
    if(v[i].size()==1) continue;
    if(!dfs(i,-1)) pr("YES");
    else pr("NO");
    return;
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}

Submission Info

Submission Time
Task C - Cleaning
User kzyKT
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1811 Byte
Status WA
Exec Time 44 ms
Memory 13440 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 1
WA × 2
AC × 32
WA × 19
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, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt WA 40 ms 6912 KB
in10.txt WA 40 ms 6912 KB
in11.txt WA 44 ms 12160 KB
in12.txt WA 42 ms 9216 KB
in13.txt AC 43 ms 13440 KB
in14.txt AC 43 ms 12544 KB
in15.txt WA 3 ms 2816 KB
in16.txt AC 3 ms 2816 KB
in17.txt AC 3 ms 2816 KB
in18.txt AC 3 ms 2816 KB
in19.txt AC 40 ms 6912 KB
in2.txt WA 40 ms 6912 KB
in20.txt AC 41 ms 6912 KB
in21.txt AC 41 ms 6912 KB
in22.txt AC 40 ms 6912 KB
in23.txt AC 40 ms 6912 KB
in24.txt AC 41 ms 6912 KB
in25.txt AC 3 ms 2816 KB
in26.txt AC 42 ms 6912 KB
in27.txt AC 42 ms 6912 KB
in28.txt AC 3 ms 2816 KB
in29.txt AC 39 ms 6912 KB
in3.txt WA 40 ms 6912 KB
in30.txt AC 3 ms 2816 KB
in31.txt AC 44 ms 6912 KB
in32.txt AC 41 ms 6912 KB
in33.txt WA 41 ms 6912 KB
in34.txt WA 40 ms 6912 KB
in35.txt AC 42 ms 6912 KB
in36.txt AC 40 ms 6912 KB
in37.txt AC 3 ms 2944 KB
in38.txt AC 40 ms 6912 KB
in39.txt AC 40 ms 6912 KB
in4.txt WA 41 ms 6912 KB
in40.txt AC 40 ms 6912 KB
in41.txt AC 41 ms 6912 KB
in42.txt AC 41 ms 6912 KB
in43.txt AC 40 ms 6912 KB
in44.txt AC 40 ms 6912 KB
in45.txt AC 41 ms 6912 KB
in5.txt WA 41 ms 6912 KB
in6.txt WA 40 ms 6912 KB
in7.txt WA 36 ms 6400 KB
in8.txt WA 11 ms 3840 KB
in9.txt WA 40 ms 6912 KB
sample1.txt WA 3 ms 2816 KB
sample2.txt AC 3 ms 2816 KB
sample3.txt WA 3 ms 2816 KB