Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

map<int, vector<int>> g;
vector<int> color;
vector<int> depth;

void func(int v, int d, int c) {
  if (!color[v]) {
    color[v] = c;
  }
  if (d == 0 || depth[v] >= d) {
    return;
  }
  FOE(u, g[v]) { func(u, d - 1, c); }
}

int main() {
  int N, M, a, b;
  cin >> N >> M;

  FOR(_, 0, M) {
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int Q;
  cin >> Q;
  vector<int> v(Q, 0), d(Q, 0), c(Q, 0);
  FOR(i, 0, Q) { cin >> v[Q - i - 1] >> d[Q - i - 1] >> c[Q - i - 1]; }

  color = vector<int>(N + 1, 0);
  depth = vector<int>(N + 1, 0);

  FOR(i, 0, Q) { func(v[i], d[i], c[i]); }

  FOR(i, 1, N + 1) { cout << color[i] << endl; }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

map<int, vector<int>> g;
vector<int> color;
vector<int> depth;

void func(int v, int d, int c) {
  if (!color[v]) {
    color[v] = c;
  }
  if (d == 0 || depth[v] >= d) {
    return;
  }
  depth[v] = d;
  FOE(u, g[v]) { func(u, d - 1, c); }
}

int main() {
  int N, M, a, b;
  cin >> N >> M;

  FOR(_, 0, M) {
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int Q;
  cin >> Q;
  vector<int> v(Q, 0), d(Q, 0), c(Q, 0);
  FOR(i, 0, Q) { cin >> v[Q - i - 1] >> d[Q - i - 1] >> c[Q - i - 1]; }

  color = vector<int>(N + 1, 0);
  depth = vector<int>(N + 1, 0);

  FOR(i, 0, Q) { func(v[i], d[i], c[i]); }

  FOR(i, 1, N + 1) { cout << color[i] << endl; }

  return 0;
}