Problem p03768 (C++)

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int N, M;
vector<vector<int>> G;
bool vis[10000][11];

struct Query {
  int v, d, c;
};

void dfs(int v, int d, int c, vector<int> &color) {
  if (vis[v][d])
    return;
  rep(i, d + 1) vis[v][i] = true;
  if (color[v] == 0)
    color[v] = c;
  if (d == 0)
    return;

  for (auto u : G[v]) {
    if (vis[u][d - 1])
      continue;
    dfs(u, d - 1, c, color);
  }
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N >> M;
  G.resize(N);
  rep(i, M) {
    int A, B;
    cin >> A >> B;
    A--, B--;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  int Q;
  cin >> Q;
  vector<Query> query(Q);

  rep(i, Q) {
    int V, D, C;
    cin >> V >> D >> C;
    V--;
    query[i] = {V, D, C};
  }

  vector<int> color(N);

  reverse(query.begin(), query.end());
  for (auto q : query) {
    dfs(q.v, q.d, q.c, color);
  }

  rep(i, N) { cout << color[i] << endl; }
}


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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int N, M;
vector<vector<int>> G;
bool vis[100000][11];

struct Query {
  int v, d, c;
};

void dfs(int v, int d, int c, vector<int> &color) {
  if (vis[v][d])
    return;
  rep(i, d + 1) vis[v][i] = true;
  if (color[v] == 0)
    color[v] = c;
  if (d == 0)
    return;

  for (auto u : G[v]) {
    if (vis[u][d - 1])
      continue;
    dfs(u, d - 1, c, color);
  }
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N >> M;
  G.resize(N);
  rep(i, M) {
    int A, B;
    cin >> A >> B;
    A--, B--;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  int Q;
  cin >> Q;
  vector<Query> query(Q);

  rep(i, Q) {
    int V, D, C;
    cin >> V >> D >> C;
    V--;
    query[i] = {V, D, C};
  }

  vector<int> color(N);

  reverse(query.begin(), query.end());
  for (auto q : query) {
    dfs(q.v, q.d, q.c, color);
  }

  rep(i, N) { cout << color[i] << endl; }
}