Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

const ll INF = 1e9;

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll n, m;
  cin >> n >> m;
  vvl path(n);
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  ll q;
  cin >> q;
  vl v(q), d(q), c(q);
  REP(i, q) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  vl ans(n, -1), ansd(n, -1);
  for (int i = q - 1; i >= 0; --i) {
    using P = tuple<ll, ll, ll>;
    deque<P> q;
    q.push_back(P(v[i], -1, d[i]));
    if (ans[v[i]] != -1) {
      if (ansd[v[i]] >= d[i])
        continue;
    } else {
      ans[v[i]] = c[i];
    }
    ansd[v[i]] = d[i];

    while (!q.empty()) {
      ll cur, prev, curd;
      tie(cur, prev, curd) = q.front();
      q.pop_front();
      if (curd != 0) {
        for (auto next : path[cur])
          if (next != prev) {
            if (ans[next] == -1) {
              q.push_back(P(next, cur, curd - 1));
              ans[next] = c[i];
              ansd[v[i]] = curd - 1;
            } else if (ansd[next] < curd - 1) {
              ansd[v[i]] = curd - 1;
              q.push_back(P(next, cur, curd - 1));
            }
          }
      }
    }
  }
  REP(i, n) cout << (ans[i] == -1 ? 0 : ans[i]) << "\n";
  cout << flush;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

const ll INF = 1e9;

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll n, m;
  cin >> n >> m;
  vvl path(n);
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  ll q;
  cin >> q;
  vl v(q), d(q), c(q);
  REP(i, q) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  vl ans(n, -1), ansd(n, -1);
  for (int i = q - 1; i >= 0; --i) {
    using P = tuple<ll, ll, ll>;
    deque<P> q;
    q.push_back(P(v[i], -1, d[i]));
    if (ans[v[i]] != -1) {
      if (ansd[v[i]] >= d[i])
        continue;
    } else {
      ans[v[i]] = c[i];
    }
    ansd[v[i]] = d[i];

    while (!q.empty()) {
      ll cur, prev, curd;
      tie(cur, prev, curd) = q.front();
      q.pop_front();
      if (curd != 0) {
        for (auto next : path[cur])
          if (next != prev) {
            if (ans[next] == -1) {
              q.push_back(P(next, cur, curd - 1));
              ans[next] = c[i];
              ansd[v[i]] = curd - 1;
            } else if (ansd[next] < curd - 1) {
              ansd[next] = curd - 1;
              q.push_back(P(next, cur, curd - 1));
            }
          }
      }
    }
  }
  REP(i, n) cout << (ans[i] == -1 ? 0 : ans[i]) << "\n";
  cout << flush;
}