Problem p03949 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N (500)

int n, m;
vector<int> e[N];
int l[N];
int r[N];
bool used[N];
int a[N];
int b[N];
bool z;

void init(void) {
  memset(used, 0, sizeof(used));
  return;
}

void dfs(int x) {
  used[x] = true;
  if (a[x] >= 0) {
    if (a[x] < l[x] || a[x] > r[x]) {
      z = false;
      return;
    } else if ((a[x] - l[x]) % 2 == 1) {
      z = false;
      return;
    }
    l[x] = a[x];
    r[x] = a[x];
  }
  f(i, e[x].size()) {
    if (!used[e[x][i]]) {
      l[e[x][i]] = l[x] - 1;
      r[e[x][i]] = r[x] + 1;
      dfs(e[x][i]);
      if (!z)
        return;
      l[x] = max(l[x], l[e[x][i]] - 1);
      r[x] = min(r[x], r[e[x][i]] + 1);
      if (l[x] > r[x]) {
        z = false;
        return;
      }
    }
  }
  return;
}

void dfsv(int x) {
  used[x] = true;
  f(i, e[x].size()) {
    if (!used[e[x][i]]) {
      if (l[e[x][i]] <= b[x] + 1 && b[x] + 1 <= r[e[x][i]]) {
        b[e[x][i]] = b[x] + 1;
        dfsv(e[x][i]);
        if (!z)
          return;
      } else if (l[e[x][i]] <= b[x] - 1 && b[x] - 1 <= r[e[x][i]]) {
        b[e[x][i]] = b[x] - 1;
        dfsv(e[x][i]);
        if (!z)
          return;
      } else {
        z = false;
        return;
      }
    }
  }
}

int main(void) {
  int k;
  int x, y;
  long long s, ans;
  scanf("%d", &n);
  f(i, n) a[i] = -1;
  init();
  z = true;
  f(i, n - 1) {
    scanf("%d %d", &x, &y);
    x--;
    y--;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  scanf("%d", &k);
  f(i, k) {
    scanf("%d %d", &x, &y);
    x--;
    a[x] = y;
  }
  l[x] = y;
  r[x] = y;
  dfs(x);
  init();
  b[x] = l[x];
  dfsv(x);
  if (z) {
    printf("Yes\n");
    f(i, n) { printf("%d\n", b[i]); }
  } else {
    printf("No\n");
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N (100010)

int n, m;
vector<int> e[N];
int l[N];
int r[N];
bool used[N];
int a[N];
int b[N];
bool z;

void init(void) {
  memset(used, 0, sizeof(used));
  return;
}

void dfs(int x) {
  used[x] = true;
  if (a[x] >= 0) {
    if (a[x] < l[x] || a[x] > r[x]) {
      z = false;
      return;
    } else if ((a[x] - l[x]) % 2 == 1) {
      z = false;
      return;
    }
    l[x] = a[x];
    r[x] = a[x];
  }
  f(i, e[x].size()) {
    if (!used[e[x][i]]) {
      l[e[x][i]] = l[x] - 1;
      r[e[x][i]] = r[x] + 1;
      dfs(e[x][i]);
      if (!z)
        return;
      l[x] = max(l[x], l[e[x][i]] - 1);
      r[x] = min(r[x], r[e[x][i]] + 1);
      if (l[x] > r[x]) {
        z = false;
        return;
      }
    }
  }
  return;
}

void dfsv(int x) {
  used[x] = true;
  f(i, e[x].size()) {
    if (!used[e[x][i]]) {
      if (l[e[x][i]] <= b[x] + 1 && b[x] + 1 <= r[e[x][i]]) {
        b[e[x][i]] = b[x] + 1;
        dfsv(e[x][i]);
        if (!z)
          return;
      } else if (l[e[x][i]] <= b[x] - 1 && b[x] - 1 <= r[e[x][i]]) {
        b[e[x][i]] = b[x] - 1;
        dfsv(e[x][i]);
        if (!z)
          return;
      } else {
        z = false;
        return;
      }
    }
  }
}

int main(void) {
  int k;
  int x, y;
  long long s, ans;
  scanf("%d", &n);
  f(i, n) a[i] = -1;
  init();
  z = true;
  f(i, n - 1) {
    scanf("%d %d", &x, &y);
    x--;
    y--;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  scanf("%d", &k);
  f(i, k) {
    scanf("%d %d", &x, &y);
    x--;
    a[x] = y;
  }
  l[x] = y;
  r[x] = y;
  dfs(x);
  init();
  b[x] = l[x];
  dfsv(x);
  if (z) {
    printf("Yes\n");
    f(i, n) { printf("%d\n", b[i]); }
  } else {
    printf("No\n");
  }

  return 0;
}