Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Graph {
private:
  int V;
  vvi g, dp;
  vi res;
  void f(int v, int d, int c) {
    if (dp[v][d])
      return;
    if (!res[v])
      res[v] = c;
    for (int i = 0; i < g[v].size(); i++) {
      f(g[v][i], d - 1, c);
    }
    dp[v][d] = 1;
  }

public:
  Graph(int v) {
    V = v;
    g = vvi(v);
  }
  void add_edge(int s, int t) {
    g[s].push_back(t);
    g[t].push_back(s);
  }
  void solve() {
    int q;
    cin >> q;
    vi v, d, c;
    v = d = c = vi(q);
    res = vi(V);
    dp = vvi(V, vi(11));
    for (int i = 0; i < q; i++)
      cin >> v[i] >> d[i] >> c[i];
    for (int i = q - 1; i >= 0; i--)
      f(v[i] - 1, d[i], c[i]);
    for (int i = 0; i < V; i++)
      cout << res[i] << endl;
  }
};

int n, m;

int main() {
  cin >> n >> m;
  Graph g(n);
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    g.add_edge(s - 1, t - 1);
  }
  g.solve();
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Graph {
private:
  int V;
  vvi g, dp;
  vi res;
  void f(int v, int d, int c) {
    if (dp[v][d] || d == -1)
      return;
    if (!res[v])
      res[v] = c;
    for (int i = 0; i < g[v].size(); i++) {
      f(g[v][i], d - 1, c);
    }
    dp[v][d] = 1;
  }

public:
  Graph(int v) {
    V = v;
    g = vvi(v);
  }
  void add_edge(int s, int t) {
    g[s].push_back(t);
    g[t].push_back(s);
  }
  void solve() {
    int q;
    cin >> q;
    vi v, d, c;
    v = d = c = vi(q);
    res = vi(V);
    dp = vvi(V, vi(11));
    for (int i = 0; i < q; i++)
      cin >> v[i] >> d[i] >> c[i];
    for (int i = q - 1; i >= 0; i--)
      f(v[i] - 1, d[i], c[i]);
    for (int i = 0; i < V; i++)
      cout << res[i] << endl;
  }
};

int n, m;

int main() {
  cin >> n >> m;
  Graph g(n);
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    g.add_edge(s - 1, t - 1);
  }
  g.solve();
}