Problem p03809 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#if DEBUG && !ONLINE_JUDGE
#include "header.h"
#else
#define debug(args...)
#endif

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long lli;
typedef long double ld;

#define pb push_back
#define all(x) x.begin(), x.end()
#define SZ(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define rep(i, a, b) for (auto i = (a); i < b; i++)

#define INF (int)1e9
#define EPS 1e-9
#define MOD 1000000007
void preprocess(void) { return; }

vector<vi> adj;
vector<lli> c, a;
int ff = 0;
void dfs(int v, int par = -1) {
  for (auto x : adj[v]) {
    if (x != par)
      dfs(x, v);
  }
  long long sum = 0;
  int flag = 0;
  for (auto x : adj[v]) {
    if (x != par) {
      sum += c[x];
      flag = 1;
    }
  }
  if (!flag) {
    c[v] = a[v];
  } else {
    c[v] = 2 * a[v] - sum;
    if (c[v] < 0)
      ff = 1;
    if (par == -1 && c[v] != 0)
      ff = 1;
  }
}

void dfs1(int v, int par = -1) {
  for (auto x : adj[v]) {
    if (x != par) {
      dfs1(x, v);
    }
  }
  if (SZ(adj[v]) == 1)
    return;
  long long s = 0;
  long long m = 0;
  for (auto x : adj[v]) {
    if (x != par) {
      s += c[x];
      m = max(m, c[x]);
    }
  }
  if (SZ(adj[v]) != 1) {
    s += c[v];
    m = max(m, c[v]);
  }
  if (SZ(adj[v]) != 1 && (2 * m > s || s % 2 == 1)) {
    ff = 1;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(20);
  preprocess();
  int n;
  cin >> n;
  adj.resize(n + 1, vi(0));
  c.resize(n + 1, 0);
  a.resize(n + 1);
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 0, n - 1) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  int root = -1;
  rep(i, 1, n + 1) if (SZ(adj[i]) != 1) root = i;
  debug(root);
  assert(root != -1);
  dfs(root, -1);
  debug(c);
  if (ff) {
    cout << "NO" << endl;
    return 0;
  }
  dfs1(root, -1);
  if (ff)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#if DEBUG && !ONLINE_JUDGE
#include "header.h"
#else
#define debug(args...)
#endif

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long lli;
typedef long double ld;

#define pb push_back
#define all(x) x.begin(), x.end()
#define SZ(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define rep(i, a, b) for (auto i = (a); i < b; i++)

#define INF (int)1e9
#define EPS 1e-9
#define MOD 1000000007
void preprocess(void) { return; }

vector<vi> adj;
vector<lli> c, a;
int ff = 0;
void dfs(int v, int par = -1) {
  for (auto x : adj[v]) {
    if (x != par)
      dfs(x, v);
  }
  long long sum = 0;
  int flag = 0;
  for (auto x : adj[v]) {
    if (x != par) {
      sum += c[x];
      flag = 1;
    }
  }
  if (!flag) {
    c[v] = a[v];
  } else {
    c[v] = 2 * a[v] - sum;
    if (c[v] < 0)
      ff = 1;
    if (par == -1 && c[v] != 0)
      ff = 1;
  }
}

void dfs1(int v, int par = -1) {
  for (auto x : adj[v]) {
    if (x != par) {
      dfs1(x, v);
    }
  }
  if (SZ(adj[v]) == 1)
    return;
  long long s = 0;
  long long m = 0;
  for (auto x : adj[v]) {
    if (x != par) {
      s += c[x];
      m = max(m, c[x]);
    }
  }
  if (SZ(adj[v]) != 1) {
    s += c[v];
    m = max(m, c[v]);
  }
  if (SZ(adj[v]) != 1 && (2 * m > s || s % 2 == 1)) {
    ff = 1;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(20);
  preprocess();
  int n;
  cin >> n;
  adj.resize(n + 1, vi(0));
  c.resize(n + 1, 0);
  a.resize(n + 1);
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 0, n - 1) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  int root = -1;
  if (n == 2) {
    if (a[1] != a[2])
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
    return 0;
  }
  rep(i, 1, n + 1) if (SZ(adj[i]) != 1) root = i;
  debug(root);
  assert(root != -1);
  dfs(root, -1);
  debug(c);
  if (ff) {
    cout << "NO" << endl;
    return 0;
  }
  dfs1(root, -1);
  if (ff)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
}