Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); ++itr)
#define mp make_pair
#define INF 2000000000
#define INFL 2000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
typedef pair<int, pair<int, int>> Edge;

int N, M, Q;
vvi G;
vector<Edge> q;
vi col;
vb used;
vi painted;

void paint(int v, int d, int c) {
  queue<P> q;
  q.push(mp(v, d));
  while (!q.empty()) {
    int pos = q.front().first;
    int dist = q.front().second;
    q.pop();
    if (painted[pos] >= dist)
      continue;
    painted[pos] = dist;
    if (col[pos] == 0) {
      col[pos] = c;
    }
    used[pos] = true;

    rep(i, G[pos].size()) {
      int next = G[pos][i];
      if (used[next])
        continue;
      if (dist > 0)
        q.push(mp(next, dist - 1));
    }
  }
}

int main() {
  cin >> N >> M;
  G = vvi(N);
  col = vi(N, 0);
  painted = vi(N, -1);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> Q;
  if (max(N, max(M, Q)) > 2000) {
    vi dummy = vi(INFL);
    return 0;
  }
  q = vector<Edge>(Q);
  rep(i, Q) {
    int v, d, c;
    cin >> v >> d >> c;
    v--;
    q.push_back(mp(v, mp(d, c)));
  }
  reverse(q.begin(), q.end());

  rep(i, Q) {
    used = vb(N, false);
    paint(q[i].first, q[i].second.first, q[i].second.second);
  }

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

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); ++itr)
#define mp make_pair
#define INF 2000000000
#define INFL 2000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
typedef pair<int, pair<int, int>> Edge;

int N, M, Q;
vvi G;
vector<Edge> q;
vi col;
vb used;
vi painted;

void paint(int v, int d, int c) {
  queue<P> q;
  q.push(mp(v, d));
  while (!q.empty()) {
    int pos = q.front().first;
    int dist = q.front().second;
    q.pop();
    if (painted[pos] >= dist)
      continue;
    painted[pos] = dist;
    if (col[pos] == 0) {
      col[pos] = c;
    }
    used[pos] = true;

    rep(i, G[pos].size()) {
      int next = G[pos][i];
      if (used[next])
        continue;
      if (dist > 0)
        q.push(mp(next, dist - 1));
    }
  }
}

int main() {
  cin >> N >> M;
  G = vvi(N);
  col = vi(N, 0);
  painted = vi(N, -1);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> Q;
  q = vector<Edge>(Q);
  rep(i, Q) {
    int v, d, c;
    cin >> v >> d >> c;
    v--;
    q.push_back(mp(v, mp(d, c)));
  }
  reverse(q.begin(), q.end());

  rep(i, Q) {
    used = vb(N, false);
    paint(q[i].first, q[i].second.first, q[i].second.second);
  }

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

  return 0;
}