Problem p03949 (C++)

Buggy submission (status: Runtime Error):
// Zory-2018
//*******************头文件*******************
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int mymax(int x, int y) { return x > y ? x : y; }
int mymin(int x, int y) { return x < y ? x : y; }
//*******************全局常量*******************
const int MAXN = 11000;
const int INF = 0x3f3f3f3f;
//*******************全局定义*******************
struct nod {
  int hou;

  int val;
  int mi, mx;
  int jo; //-1,0,1
} p[MAXN];
struct edge {
  int y, g;
} e[2 * MAXN];
//*******************实现*******************
int ln = 0;
void ins(int x, int y) {
  ln++;
  e[ln].y = y;
  e[ln].g = p[x].hou;
  p[x].hou = ln;
}
bool bk = 1;
void dfs(int x, int fa) {
  if (bk == 0)
    return;

  for (int k = p[x].hou; k > 0; k = e[k].g) {
    int y = e[k].y;
    if (y == fa)
      continue;
    dfs(y, x);
    if (p[x].jo != -1 and p[x].jo == p[y].jo) {
      bk = 0;
      return;
    } // 本来相邻或者递推得来
    p[x].mi = mymax(p[x].mi, p[y].mi - 1);
    p[x].mx = mymin(p[x].mx, p[y].mx + 1);
    if (p[y].jo != -1)
      p[x].jo = p[y].jo ^ 1; // 信息的合并
  }

  if (p[x].jo != -1 and p[x].mi == p[x].mx and p[x].mi % 2 != p[x].jo) {
    bk = 0;
    return;
  } // 检查
  if (p[x].mi > p[x].mx) {
    bk = 0;
    return;
  }
}
void dfs2(int x, int fa) {
  if (p[x].val == -1) {
    if (fa == 0) {
      if (p[x].mi % 2 == p[x].jo)
        p[x].val = p[x].mi;
      else
        p[x].val = p[x].mi + 1;
    } else {
      if (p[x].mi <= p[fa].val - 1 and p[fa].val - 1 <= p[x].mx)
        p[x].val = p[fa].val - 1; // debug
      else
        p[x].val = p[fa].val + 1;
    }
  }

  for (int k = p[x].hou; k > 0; k = e[k].g) {
    int y = e[k].y;
    if (y == fa)
      continue;
    dfs2(y, x);
  }
}
//*******************主函数*******************
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    p[i].hou = 0;
    p[i].val = -1;
    p[i].mi = -INF;
    p[i].mx = INF;
    p[i].jo = -1;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  int k;
  scanf("%d", &k);
  for (int i = 1; i <= k; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    p[a].val = p[a].mi = p[a].mx = b;
    p[a].jo = b % 2;
  }

  dfs(1, 0); // 自下而上
  if (bk == 0)
    printf("No\n");
  else {
    printf("Yes\n");
    dfs2(1, 0); // 自上而下
    for (int i = 1; i <= n; i++)
      printf("%d\n", p[i].val);
  }
}

Fixed submission:
// Zory-2018
//*******************头文件*******************
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int mymax(int x, int y) { return x > y ? x : y; }
int mymin(int x, int y) { return x < y ? x : y; }
//*******************全局常量*******************
const int MAXN = 110000;
const int INF = 0x3f3f3f3f;
//*******************全局定义*******************
struct nod {
  int hou;

  int val;
  int mi, mx;
  int jo; //-1,0,1
} p[MAXN];
struct edge {
  int y, g;
} e[2 * MAXN];
//*******************实现*******************
int ln = 0;
void ins(int x, int y) {
  ln++;
  e[ln].y = y;
  e[ln].g = p[x].hou;
  p[x].hou = ln;
}
bool bk = 1;
void dfs(int x, int fa) {
  if (bk == 0)
    return;

  for (int k = p[x].hou; k > 0; k = e[k].g) {
    int y = e[k].y;
    if (y == fa)
      continue;
    dfs(y, x);
    if (p[x].jo != -1 and p[x].jo == p[y].jo) {
      bk = 0;
      return;
    } // 本来相邻或者递推得来
    p[x].mi = mymax(p[x].mi, p[y].mi - 1);
    p[x].mx = mymin(p[x].mx, p[y].mx + 1);
    if (p[y].jo != -1)
      p[x].jo = p[y].jo ^ 1; // 信息的合并
  }

  if (p[x].jo != -1 and p[x].mi == p[x].mx and p[x].mi % 2 != p[x].jo) {
    bk = 0;
    return;
  } // 检查
  if (p[x].mi > p[x].mx) {
    bk = 0;
    return;
  }
}
void dfs2(int x, int fa) {
  if (p[x].val == -1) {
    if (fa == 0) {
      if (p[x].mi % 2 == p[x].jo)
        p[x].val = p[x].mi;
      else
        p[x].val = p[x].mi + 1;
    } else {
      if (p[x].mi <= p[fa].val - 1 and p[fa].val - 1 <= p[x].mx)
        p[x].val = p[fa].val - 1; // debug
      else
        p[x].val = p[fa].val + 1;
    }
  }

  for (int k = p[x].hou; k > 0; k = e[k].g) {
    int y = e[k].y;
    if (y == fa)
      continue;
    dfs2(y, x);
  }
}
//*******************主函数*******************
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    p[i].hou = 0;
    p[i].val = -1;
    p[i].mi = -INF;
    p[i].mx = INF;
    p[i].jo = -1;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  int k;
  scanf("%d", &k);
  for (int i = 1; i <= k; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    p[a].val = p[a].mi = p[a].mx = b;
    p[a].jo = b % 2;
  }

  dfs(1, 0); // 自下而上
  if (bk == 0)
    printf("No\n");
  else {
    printf("Yes\n");
    dfs2(1, 0); // 自上而下
    for (int i = 1; i <= n; i++)
      printf("%d\n", p[i].val);
  }
}