Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#define C

#ifdef C
#include <stdio.h>
#define abs(a) ((a) < 0 ? -(a) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define isdigit(a) ((a) >= '0' && (a) <= '9' ? true : false)
#endif
#ifdef Cpp
#include <bits/stdc++.h>
using namespace std;
#endif

#define freopen(x)                                                             \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
#define repl(i, t) for (int i = fi[t]; i; i = ne[i])
#define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define per(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define _r read()
#define readtype long long

typedef long long var;

const int INF = 0x3f3f3f3f;
const int N = 2e5;

int top, fi[N], ne[N], to[N];
int have[N], res[N];

readtype read();
void add(int u, int v);
void DFS(int t, int s = 0, int f = 0);

int main() {
  int n = _r, q = _r;
  rep(i, 2, n) add(_r, _r);
  rep(i, 1, q) {
    int u = _r, v = _r;
    have[u] += v;
  }
  DFS(1);
  rep(i, 1, n - 1) printf("%d ", res[i]);
  printf("%d\n", res[n]);
  return 0;
}

readtype read() {
  readtype a = 0, c = getchar(), s = 0;
  while (!isdigit(c))
    s |= c == '-', c = getchar();
  while (isdigit(c))
    a = a * 10 + c - 48, c = getchar();
  return s ? -a : a;
}

void add(int u, int v) {
  ne[++top] = fi[u], fi[u] = top, to[top] = v;
  ne[++top] = fi[v], fi[v] = top, to[top] = u;
}

void DFS(int t, int s, int f) {
  res[t] = s + have[t];
  repl(i, t) {
    if (to[i] == f)
      continue;
    DFS(to[i], res[t], t);
  }
}


Fixed submission:
#define C

#ifdef C
#include <stdio.h>
#define abs(a) ((a) < 0 ? -(a) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define isdigit(a) ((a) >= '0' && (a) <= '9' ? true : false)
#endif
#ifdef Cpp
#include <bits/stdc++.h>
using namespace std;
#endif

#define freopen(x)                                                             \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
#define repl(i, t) for (int i = fi[t]; i; i = ne[i])
#define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define per(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define _r read()
#define readtype long long

typedef long long var;

const int INF = 0x3f3f3f3f;
const int N = 1e6 + 1;

int top, fi[N], ne[N], to[N];
int have[N], res[N];

readtype read();
void add(int u, int v);
void DFS(int t, int s = 0, int f = 0);

int main() {
  int n = _r, q = _r;
  rep(i, 2, n) add(_r, _r);
  rep(i, 1, q) {
    int u = _r, v = _r;
    have[u] += v;
  }
  DFS(1);
  rep(i, 1, n - 1) printf("%d ", res[i]);
  printf("%d\n", res[n]);
  return 0;
}

readtype read() {
  readtype a = 0, c = getchar(), s = 0;
  while (!isdigit(c))
    s |= c == '-', c = getchar();
  while (isdigit(c))
    a = a * 10 + c - 48, c = getchar();
  return s ? -a : a;
}

void add(int u, int v) {
  ne[++top] = fi[u], fi[u] = top, to[top] = v;
  ne[++top] = fi[v], fi[v] = top, to[top] = u;
}

void DFS(int t, int s, int f) {
  res[t] = s + have[t];
  repl(i, t) {
    if (to[i] == f)
      continue;
    DFS(to[i], res[t], t);
  }
}