Problem p03809 (C++)

Buggy submission (status: Runtime Error):
/* Never Say Die. */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
using namespace std;
int _c = 0;
template <class _T> inline void read(_T &_a) {
  _a = 0;
  bool _f = 0;
  while (_c < '0' || _c > '9')
    _f |= _c == '-', _c = getchar();
  while (_c >= '0' && _c <= '9')
    _a = _a * 10 + _c - '0', _c = getchar();
  if (_f)
    _a = -_a;
}
typedef long long LL;
typedef long double LD;
#define MAXN 200005

struct edge {
  int next, to;
} e[MAXN * 2];

int n, ecnt = 0;
int root, d[MAXN], head[MAXN];
void add(int u, int v) {
  d[u]++, d[v]++;
  e[++ecnt].to = v;
  e[ecnt].next = head[u];
  head[u] = ecnt;
  e[++ecnt].to = u;
  e[ecnt].next = head[v];
  head[v] = ecnt;
}
LL a[MAXN], tot[MAXN];

bool dfs(int x, int fa) {
  bool lf = 1;
  for (int now = head[x]; now; now = e[now].next) {
    if (e[now].to != fa) {
      lf = 0;
      if (dfs(e[now].to, x))
        return 1;
      tot[x] += a[e[now].to];
    }
  }
  if (!lf) {
    LL ta = 2 * a[x] - tot[x], su = 0;
    if (ta < 0)
      return 1;
    for (int now = head[x]; now; now = e[now].next) {
      if (e[now].to != fa) {
        su = min(2 * (a[x] - ta), su + min(a[x] - ta, a[e[now].to]));
      }
    }
    if (su < 2 * (a[x] - ta))
      return 1;
    a[x] = ta;
  }
  return 0;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  if (n == 2)
    return puts(a[1] == a[2] ? "YES" : "NO");
  for (int i = 1, u, v; i < n; i++)
    cin >> u >> v, add(u, v);
  for (int i = 1; i <= n; i++)
    if (d[i] != 1) {
      root = i;
      break;
    }
  if (!root)
    return puts("NO"), 0;
  if (dfs(root, 0) || a[root])
    return puts("NO"), 0;
  puts("YES");
  return 0;
}


Fixed submission:
/* Never Say Die. */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
using namespace std;
int _c = 0;
template <class _T> inline void read(_T &_a) {
  _a = 0;
  bool _f = 0;
  while (_c < '0' || _c > '9')
    _f |= _c == '-', _c = getchar();
  while (_c >= '0' && _c <= '9')
    _a = _a * 10 + _c - '0', _c = getchar();
  if (_f)
    _a = -_a;
}
typedef long long LL;
typedef long double LD;
#define MAXN 200005

struct edge {
  int next, to;
} e[MAXN * 2];

int n, ecnt = 0;
int root, d[MAXN], head[MAXN];
void add(int u, int v) {
  d[u]++, d[v]++;
  e[++ecnt].to = v;
  e[ecnt].next = head[u];
  head[u] = ecnt;
  e[++ecnt].to = u;
  e[ecnt].next = head[v];
  head[v] = ecnt;
}
LL a[MAXN], tot[MAXN];

bool dfs(int x, int fa) {
  bool lf = 1;
  for (int now = head[x]; now; now = e[now].next) {
    if (e[now].to != fa) {
      lf = 0;
      if (dfs(e[now].to, x))
        return 1;
      tot[x] += a[e[now].to];
    }
  }
  if (!lf) {
    LL ta = 2 * a[x] - tot[x], su = 0;
    if (ta < 0)
      return 1;
    for (int now = head[x]; now; now = e[now].next) {
      if (e[now].to != fa) {
        su = min(2 * (a[x] - ta), su + min(a[x] - ta, a[e[now].to]));
      }
    }
    if (su < 2 * (a[x] - ta))
      return 1;
    a[x] = ta;
  }
  return 0;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  if (n == 2)
    return puts(a[1] == a[2] ? "YES" : "NO"), 0;
  for (int i = 1, u, v; i < n; i++)
    cin >> u >> v, add(u, v);
  for (int i = 1; i <= n; i++)
    if (d[i] != 1) {
      root = i;
      break;
    }
  if (!root)
    return puts("NO"), 0;
  if (dfs(root, 0) || a[root])
    return puts("NO"), 0;
  puts("YES");
  return 0;
}