Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <time.h>
#include <vector>
#define MAXN 100000
#define MAXD 10

#define ll long long
using namespace std;

int N, M, Q;

int BFS[2][MAXN * MAXD + 200];
bool done[(MAXN + 100) * MAXD + 200];
int colors[MAXN + 200];

vector<int> edges[MAXN + 2];

void doBFS(int u, int dist, int color) {
  BFS[0][0] = u * 11 + dist;
  int sz = 1;

  for (int iters = 1; sz > 0; iters++) {
    int nsz = 0;
    for (int j = 0; j < sz; j++) {
      int curr = BFS[(iters - 1) & 1][j];

      int cu = curr / 11;
      int cdist = curr % 11;

      if (colors[cu] == -1)
        colors[cu] = color;

      if (done[cu * 11 + cdist])
        continue;

      for (int d = 0; d <= cdist; d++)
        done[cu * 11 + cdist] = true;

      if (cdist == 0)
        continue;

      for (int j = 0; j < edges[cu].size(); j++) {
        int v = edges[cu][j];

        if (done[v * 11 + cdist - 1])
          continue;

        BFS[iters & 1][nsz++] = v * 11 + cdist - 1;
      }
    }
    sz = nsz;
  }
}

int v[MAXN + 2];
int dists[MAXN + 2];
int splatter_colors[MAXN + 2];

int main() {
  cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  cin >> Q;

  v[0] = 1;
  dists[0] = N + 2;
  colors[0] = 0;

  for (int i = 1; i <= Q; i++) {
    cin >> v[i] >> dists[i] >> splatter_colors[i];
  }

  for (int i = 1; i <= N; i++)
    colors[i] = -1;

  for (int i = 0; i <= (N + 2) * 11 + 11; i++)
    done[i] = false;

  for (int q = Q; q >= 0; q--) {
    doBFS(v[q], dists[q], splatter_colors[q]);
  }

  for (int i = 1; i <= N; i++)
    if (colors[i] == -1)
      colors[i] = 0;

  for (int i = 1; i <= N; i++)
    printf("%d\n", colors[i]);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <time.h>
#include <vector>
#define MAXN 100000
#define MAXD 12

#define ll long long
using namespace std;

int N, M, Q;

int BFS[2][MAXN * MAXD + 200];
bool done[(MAXN + 100) * MAXD + 200];
int colors[MAXN + 200];

vector<int> edges[MAXN + 2];

void doBFS(int u, int dist, int color) {
  BFS[0][0] = u * 11 + dist;
  int sz = 1;

  for (int iters = 1; sz > 0; iters++) {
    int nsz = 0;
    for (int j = 0; j < sz; j++) {
      int curr = BFS[(iters - 1) & 1][j];

      int cu = curr / 11;
      int cdist = curr % 11;

      if (colors[cu] == -1)
        colors[cu] = color;

      if (done[cu * 11 + cdist])
        continue;

      for (int d = 0; d <= cdist; d++)
        done[cu * 11 + cdist] = true;

      if (cdist == 0)
        continue;

      for (int j = 0; j < edges[cu].size(); j++) {
        int v = edges[cu][j];

        if (done[v * 11 + cdist - 1])
          continue;

        BFS[iters & 1][nsz++] = v * 11 + cdist - 1;
      }
    }
    sz = nsz;
  }
}

int v[MAXN + 2];
int dists[MAXN + 2];
int splatter_colors[MAXN + 2];

int main() {
  cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  cin >> Q;

  v[0] = 1;
  dists[0] = N + 2;
  colors[0] = 0;

  for (int i = 1; i <= Q; i++) {
    cin >> v[i] >> dists[i] >> splatter_colors[i];
  }

  for (int i = 1; i <= N; i++)
    colors[i] = -1;

  for (int i = 0; i <= (N + 2) * 11 + 11; i++)
    done[i] = false;

  for (int q = Q; q >= 0; q--) {
    doBFS(v[q], dists[q], splatter_colors[q]);
  }

  for (int i = 1; i <= N; i++)
    if (colors[i] == -1)
      colors[i] = 0;

  for (int i = 1; i <= N; i++)
    printf("%d\n", colors[i]);
}