prithivMLmods/Coder-Stat huggingface dataset:
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>

using namespace std;

typedef pair<long, long> P;
const long INF = 1000000;

long N, M, Q;
long c[100000], v[100000];
int d[100000];

vector<long> G[100000];

long dp[100000][10];
long col[100000];

void rec(long s, int n, long co);

int main() {
  scanf("%ld %ld", &N, &M);
  for (long i = 0; i < M; i++) {
    long a, b;
    scanf("%ld %ld", &a, &b);
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  scanf("%ld", &Q);
  for (long i = 0; i < Q; i++) {
    scanf("%ld %d %ld", &v[i], &d[i], &c[i]);
    v[i]--;
  }

  for (long i = Q - 1; i >= 0; i--) {
    rec(v[i], d[i], c[i]);
  }

  for (long i = 0; i < N; i++) {
    printf("%ld\n", col[i]);
  }

  return 0;
}

void rec(long s, int n, long co) {
  if (n == 0) {
    if (col[s] == 0)
      col[s] = co;
    return;
  }
  if (dp[s][n] == 0) {
    dp[s][n] = 1;
  }
  rec(s, n - 1, co);
  for (int i = 0; i < G[s].size(); i++) {
    rec(G[s][i], n - 1, co);
  }
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>

using namespace std;

typedef pair<long, long> P;
const long INF = 1000000;

long N, M, Q;
long c[100000], v[100000];
int d[100000];

vector<long> G[100000];

long dp[100000][10];
long col[100000];

void rec(long s, int n, long co);

int main() {
  scanf("%ld %ld", &N, &M);
  for (long i = 0; i < M; i++) {
    long a, b;
    scanf("%ld %ld", &a, &b);
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  scanf("%ld", &Q);
  for (long i = 0; i < Q; i++) {
    scanf("%ld %d %ld", &v[i], &d[i], &c[i]);
    v[i]--;
  }

  for (long i = Q - 1; i >= 0; i--) {
    rec(v[i], d[i], c[i]);
  }

  for (long i = 0; i < N; i++) {
    printf("%ld\n", col[i]);
  }

  return 0;
}

void rec(long s, int n, long co) {
  if (n == 0) {
    if (col[s] == 0)
      col[s] = co;
    return;
  }
  if (dp[s][n] != 0)
    return;
  dp[s][n] = 1;
  rec(s, n - 1, co);
  for (int i = 0; i < G[s].size(); i++) {
    rec(G[s][i], n - 1, co);
  }
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

const ll N = 1e5 + 10;
vec v(N), d(N), c(N);
mat G(N), dp(N, vec(11));
ll n, m, q;

ll dfs(ll f, ll td, ll tc) {
  if (td >= 0 && !dp[f][td]) {
    dp[f][td] = tc;
    for (ll t : G[f]) {
      dfs(t, td - 1, tc);
    }
  }
}

int main() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  while (q--) {
    for (ll i = d[q]; i >= 0; i--) {
      dfs(v[q], i, c[q]);
    }
  }
  for (ll i = 0; i < n; i++)
    cout << dp[i][0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

const ll N = 1e5 + 10;
vec v(N), d(N), c(N);
mat G(N), dp(N, vec(11));
ll n, m, q;

void dfs(ll f, ll td, ll tc) {
  if (td >= 0 && !dp[f][td]) {
    dp[f][td] = tc;
    for (ll t : G[f]) {
      dfs(t, td - 1, tc);
    }
  }
}

int main() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  while (q--) {
    for (ll i = d[q]; i >= 0; i--) {
      dfs(v[q], i, c[q]);
    }
  }
  for (ll i = 0; i < n; i++)
    cout << dp[i][0] << endl;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (int i = 0; i < (int)n; i++)

vector<vector<int>> g;
vector<int> range, ans;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<vector<int>> g(n);
  vector<int> range(n, -1), ans(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int q;
  cin >> q;
  vector<int> v(q), d(q), c(q);
  REP(i, q) {
    scanf("%d %d %d", &v[i], &d[i], &c[i]);
    v[i]--;
  }
  for (int i = q - 1; i >= 0; i--) {
    queue<pair<int, int>> q;
    vector<bool> used(n, false);
    if (range[v[i]] < d[i]) {
      q.push({v[i], d[i]});
    }
    while (!q.empty()) {
      auto t = q.front();
      q.pop();
      if (used[t.first])
        continue;
      if (!ans[t.first])
        ans[t.first] = c[i];
      used[v[i]] = true;
      range[t.first] = t.second;
      if (t.second) {
        for (auto &x : g[t.first]) {
          if (!used[x] && range[x] < t.second) {
            q.push({x, t.second - 1});
          }
        }
      }
    }
  }
  REP(i, n) { printf("%d\n", ans[i]); }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (int i = 0; i < (int)n; i++)

vector<vector<int>> g;
vector<int> range, ans;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<vector<int>> g(n);
  vector<int> range(n, -1), ans(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int q;
  cin >> q;
  vector<int> v(q), d(q), c(q);
  REP(i, q) {
    scanf("%d %d %d", &v[i], &d[i], &c[i]);
    v[i]--;
  }
  for (int i = q - 1; i >= 0; i--) {
    queue<pair<int, int>> q;
    vector<bool> used(n, false);
    if (range[v[i]] < d[i]) {
      q.push({v[i], d[i]});
    }
    while (!q.empty()) {
      auto t = q.front();
      q.pop();
      if (used[t.first])
        continue;
      if (!ans[t.first])
        ans[t.first] = c[i];
      used[v[i]] = true;
      range[t.first] = t.second;
      if (t.second) {
        for (auto &x : g[t.first]) {
          if (!used[x] && range[x] + 1 < t.second) {
            q.push({x, t.second - 1});
          }
        }
      }
    }
  }
  REP(i, n) { printf("%d\n", ans[i]); }
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

#define MAX_N 100000

int N, M, Q;
vector<int> graph[MAX_N];
vector<tuple<int, int, int>> query;
int color[MAX_N];
int dist[MAX_N];

void dfs(int v, int d, int c) {
  if (d < 0)
    return;
  if (d < dist[v])
    return;
  dist[v] = d;
  if (!color[v])
    color[v] = c;
  for (int u : graph[v])
    dfs(u, d - 1, c);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  cin >> Q;
  rep(i, Q) {
    int v, d, c;
    cin >> v >> d >> c;
    --v;
    query.emplace_back(v, d, c);
  }
  reverse(all(query));
  memset(dist, -1, sizeof(dist));
  rep(i, Q) {
    int v, d, c;
    tie(v, d, c) = query[i];
    dfs(v, d, c);
  }
  rep(i, N) cout << color[i] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

#define MAX_N 100000

int N, M, Q;
vector<int> graph[MAX_N];
vector<tuple<int, int, int>> query;
int color[MAX_N];
int dist[MAX_N];

void dfs(int v, int d, int c) {
  if (d < 0)
    return;
  if (d <= dist[v])
    return;
  dist[v] = d;
  if (!color[v])
    color[v] = c;
  for (int u : graph[v])
    dfs(u, d - 1, c);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  cin >> Q;
  rep(i, Q) {
    int v, d, c;
    cin >> v >> d >> c;
    --v;
    query.emplace_back(v, d, c);
  }
  reverse(all(query));
  memset(dist, -1, sizeof(dist));
  rep(i, Q) {
    int v, d, c;
    tie(v, d, c) = query[i];
    dfs(v, d, c);
  }
  rep(i, N) cout << color[i] << endl;

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define X first
#define Y second
#define N 100000
#define M 11

typedef long long ll;
const int INF = 1 << 30;

int n, m;
vector<int> E[N];
int f[N][M];

struct Query {
  int v, d, c;
} Q[N];

void Solve(int v, int d, int c) {
  if (f[v][d])
    return;
  f[v][d] = c;
  if (d == 0)
    return;
  for (int i = 0; i < E[v].size(); i++)
    Solve(E[v][i], d - 1, c);
  Solve(v, d - 1, c);
}

int main() {
  // freopen("in.in","r",stdin);
  // freopen("out.out","w",stdout);

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    E[x].push_back(y);
    E[y].push_back(x);
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++)
    scanf("%d%d%d", &Q[i].v, &Q[i].d, &Q[i].c);
  for (int i = q; i >= 1; i--)
    Solve(Q[i].v, Q[i].d, Q[i].c);
  for (int i = 1; i <= n; i++)
    printf("%d\n", f[i][0]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define X first
#define Y second
#define N 100010
#define M 11

typedef long long ll;
const int INF = 1 << 30;

int n, m;
vector<int> E[N];
int f[N][M];

struct Query {
  int v, d, c;
} Q[N];

void Solve(int v, int d, int c) {
  if (f[v][d])
    return;
  f[v][d] = c;
  if (d == 0)
    return;
  for (int i = 0; i < E[v].size(); i++)
    Solve(E[v][i], d - 1, c);
  Solve(v, d - 1, c);
}

int main() {
  // freopen("in.in","r",stdin);
  // freopen("out.out","w",stdout);

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    E[x].push_back(y);
    E[y].push_back(x);
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++)
    scanf("%d%d%d", &Q[i].v, &Q[i].d, &Q[i].c);
  for (int i = q; i >= 1; i--)
    Solve(Q[i].v, Q[i].d, Q[i].c);
  for (int i = 1; i <= n; i++)
    printf("%d\n", f[i][0]);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
vector<int> edge[100001];
int ans[100001];
int query[100001][3];
int alralr[100001];
int main() {
  for (int i = 0; i < 100001; i++)
    alralr[i] = -1;
  int N, M, Q;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  cin >> Q;
  for (int i = 0; i < Q; i++)
    scanf("%d %d %d", &query[i][0], &query[i][1], &query[i][2]);
  for (int i = Q - 1; 0 <= i; i--) {
    /*
    for(int j=1;j<=N;j++){
        printf("[%d]",ans[j]);
    }
    puts("");
    */
    int v = query[i][0], d = query[i][1], c = query[i][2];
    if (d <= alralr[v])
      continue;
    if (ans[v] == 0)
      ans[v] = c;
    int alr[100001] = {0};
    alr[v] = 1;
    queue<int> Qp, Qd;
    Qp.push(v);
    Qd.push(0);
    while (!Qp.empty()) {
      int karip = Qp.front();
      Qp.pop();
      int karid = Qd.front();
      Qd.pop();
      if (d < karid)
        continue;
      if (ans[karip] == 0)
        ans[karip] = c;
      alr[karip] = 1;
      for (int j = 0; j < edge[karip].size(); j++) {
        if (!alr[edge[karip][j]]) {
          Qp.push(edge[karip][j]);
          Qd.push(karid + 1);
        }
      }
    }
    alralr[v] = d;
  }
  for (int i = 1; i <= N; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
vector<int> edge[100001];
int ans[100001];
int query[100001][3];
int alralr[100001];
int main() {
  for (int i = 0; i < 100001; i++)
    alralr[i] = -1;
  int N, M, Q;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  cin >> Q;
  for (int i = 0; i < Q; i++)
    scanf("%d %d %d", &query[i][0], &query[i][1], &query[i][2]);
  for (int i = Q - 1; 0 <= i; i--) {
    /*
    for(int j=1;j<=N;j++){
        printf("[%d]",ans[j]);
    }
    puts("");
    */
    int v = query[i][0], d = query[i][1], c = query[i][2];
    if (d <= alralr[v])
      continue;
    if (ans[v] == 0)
      ans[v] = c;
    int alr[100001] = {0};
    alr[v] = 1;
    queue<int> Qp, Qd;
    Qp.push(v);
    Qd.push(0);
    while (!Qp.empty()) {
      int karip = Qp.front();
      Qp.pop();
      int karid = Qd.front();
      Qd.pop();
      if (d < karid)
        continue;
      if (ans[karip] == 0)
        ans[karip] = c;
      alr[karip] = 1;
      if (d <= karid)
        continue;
      if (d - karid <= alralr[karip])
        continue;
      alralr[karip] = d - karid;
      for (int j = 0; j < edge[karip].size(); j++) {
        if (!alr[edge[karip][j]]) {
          Qp.push(edge[karip][j]);
          Qd.push(karid + 1);
        }
      }
    }
    alralr[v] = d;
  }
  for (int i = 1; i <= N; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int ans[100003], n, m, q, do1[100003], do2[100003], do3[100003];
vector<int> v[100005];
bool vis[10][100003];
void dfs(int a, int b, int c) {
  if (b == -1)
    return;
  if (vis[b][a])
    return;
  for (int i = 0; i <= b; i++)
    vis[i][a] = 1;
  if (!ans[a])
    ans[a] = c;
  for (int i = 0; i < v[a].size(); i++)
    dfs(v[a][i], b - 1, c);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  cin >> q;
  for (int i = 1; i <= q; i++)
    cin >> do1[i] >> do2[i] >> do3[i];
  for (int i = q; i > 0; i--) {
    dfs(do1[i], do2[i], do3[i]);
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int ans[100003], n, m, q, do1[100003], do2[100003], do3[100003];
vector<int> v[100005];
bool vis[13][100003];
void dfs(int a, int b, int c) {
  if (b == -1)
    return;
  if (vis[b][a])
    return;
  for (int i = 0; i <= b; i++)
    vis[i][a] = 1;
  if (!ans[a])
    ans[a] = c;
  for (int i = 0; i < v[a].size(); i++)
    dfs(v[a][i], b - 1, c);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  cin >> q;
  for (int i = 1; i <= q; i++)
    cin >> do1[i] >> do2[i] >> do3[i];
  for (int i = q; i > 0; i--) {
    dfs(do1[i], do2[i], do3[i]);
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vi> vvi;

int main() {
  int n, m;
  cin >> n >> m;
  vvi e(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  int q;
  cin >> q;
  vvi A(11, vi(n, -1));
  vi C(n);
  for (int i = 0; i < q; i++) {
    int v, d, c;
    cin >> v >> d >> c;
    v--;
    A[d][v] = i;
    C[i] = c;
  }
  for (int d = 10; d > 0; d--) {
    for (int v = 0; v < n; v++) {
      int clr = A[d][v];
      for (int i = 0; i < e[v].size(); i++) {
        int nv = e[v][i];
        A[d - 1][nv] = max(A[d - 1][nv], clr);
      }
      A[d - 1][v] = max(A[d - 1][v], clr);
    }
  }
  for (int i = 0; i < n; i++) {
    int clr;
    if (A[0][i] == -1) {
      clr = 0;
    } else {
      clr = C[A[0][i]];
    }
    cout << clr << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vi> vvi;

int main() {
  int n, m;
  cin >> n >> m;
  vvi e(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  int q;
  cin >> q;
  vvi A(11, vi(n, -1));
  vi C(q);
  for (int i = 0; i < q; i++) {
    int v, d, c;
    cin >> v >> d >> c;
    v--;
    A[d][v] = i;
    C[i] = c;
  }
  for (int d = 10; d > 0; d--) {
    for (int v = 0; v < n; v++) {
      int clr = A[d][v];
      for (int i = 0; i < e[v].size(); i++) {
        int nv = e[v][i];
        A[d - 1][nv] = max(A[d - 1][nv], clr);
      }
      A[d - 1][v] = max(A[d - 1][v], clr);
    }
  }
  for (int i = 0; i < n; i++) {
    int clr;
    if (A[0][i] == -1) {
      clr = 0;
    } else {
      clr = C[A[0][i]];
    }
    cout << clr << endl;
  }

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, m, q;
  cin >> n >> m;
  vector<vector<int>> path(n), dp(n, vector<int>(11, 0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  cin >> q;
  vector<vector<int>> Q(q, vector<int>(3, 0));
  rep(i, q) rep(j, 3) {
    cin >> Q[i][j];
    if (j == 0)
      Q[i][j]--;
  }
  int v, d;
  for (int i = q - 1; i >= 0; i--) {
    queue<pp> que;
    v = Q[i][0];
    d = Q[i][1];
    if (dp[v][d] == 0) {
      dp[v][d] = Q[i][2];
      que.push(mk(v, d));
    }
    while (!(que.empty())) {
      int u, k;
      tie(u, k) = que.front();
      que.pop();
      if (dp[u][k - 1] == 0) {
        dp[u][k - 1] = Q[i][2];
        if (k > 1)
          que.push(mk(u, k - 1));
      }
      for (auto to : path[u]) {
        if (dp[to][k - 1] == 0) {
          dp[to][k - 1] = Q[i][2];
          if (k > 1)
            que.push(mk(to, k - 1));
        }
      }
    }
  }
  rep(i, n) cout << dp[i][0] << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, m, q;
  cin >> n >> m;
  vector<vector<int>> path(n), dp(n, vector<int>(11, 0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  cin >> q;
  vector<vector<int>> Q(q, vector<int>(3, 0));
  rep(i, q) rep(j, 3) {
    cin >> Q[i][j];
    if (j == 0)
      Q[i][j]--;
  }
  int v, d;
  for (int i = q - 1; i >= 0; i--) {
    queue<pp> que;
    v = Q[i][0];
    d = Q[i][1];
    if (d == 0) {
      if (dp[v][d] == 0)
        dp[v][d] = Q[i][2];
      continue;
    }
    if (dp[v][d] == 0) {
      dp[v][d] = Q[i][2];
      que.push(mk(v, d));
    }
    while (!(que.empty())) {
      int u, k;
      tie(u, k) = que.front();
      que.pop();
      if (dp[u][k - 1] == 0) {
        dp[u][k - 1] = Q[i][2];
        if (k > 1)
          que.push(mk(u, k - 1));
      }
      for (auto to : path[u]) {
        if (dp[to][k - 1] == 0) {
          dp[to][k - 1] = Q[i][2];
          if (k > 1)
            que.push(mk(to, k - 1));
        }
      }
    }
  }
  rep(i, n) cout << dp[i][0] << endl;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define maxn 100050
#define add(u, v) (to[++top] = head[u], head[u] = top, w[top] = v)
#define For(x) for (int h = head[x], o = w[h]; h; o = w[h = to[h]])
using namespace std;
int n, m, to[maxn], head[maxn], w[maxn], u, v, top;
int a[maxn], b[maxn], c[maxn], co[maxn], r[maxn], q, i;
void dfs(int x, int rg, int col) {
  if (rg <= r[x] || rg <= 0)
    return;
  r[x] = rg;
  if (co[x] == 0)
    co[x] = col;
  For(x) dfs(o, rg - 1, col);
}
int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  scanf("%d", &q);
  for (i = 1; i <= q; i++)
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
  memset(co, 0, sizeof(co));
  memset(r, 0, sizeof(r));
  for (i = q; i >= 1; i--)
    dfs(a[i], b[i] + 1, c[i]);
  for (i = 1; i <= n; i++)
    printf("%d\n", co[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define maxn 200050
#define add(u, v) (to[++top] = head[u], head[u] = top, w[top] = v)
#define For(x) for (int h = head[x], o = w[h]; h; o = w[h = to[h]])
using namespace std;
int n, m, to[maxn], head[maxn], w[maxn], u, v, top;
int a[maxn], b[maxn], c[maxn], co[maxn], r[maxn], q, i;
void dfs(int x, int rg, int col) {
  if (rg <= r[x] || rg <= 0)
    return;
  r[x] = rg;
  if (co[x] == 0)
    co[x] = col;
  For(x) dfs(o, rg - 1, col);
}
int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  scanf("%d", &q);
  for (i = 1; i <= q; i++)
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
  memset(co, 0, sizeof(co));
  memset(r, 0, sizeof(r));
  for (i = q; i >= 1; i--)
    dfs(a[i], b[i] + 1, c[i]);
  for (i = 1; i <= n; i++)
    printf("%d\n", co[i]);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <vector>
using namespace std;
typedef unsigned int uii;
const int maxn = 100005;
int n, m, Q, u, v, o[maxn], d[maxn], q[maxn], col[maxn], vis[maxn];
vector<int> G[maxn];
void dfs(int u, int l, int c) {
  if (vis[u] >= l)
    return;
  if (!col[u])
    col[u] = c;
  for (uii i = 0; i < G[u].size(); ++i)
    dfs(G[u][i], l - 1, c);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i)
    scanf("%d%d%d", &o[i], &d[i], &q[i]);
  for (int i = Q - 1; i >= 0; --i)
    dfs(o[i], d[i] + 1, q[i]);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", col[i]);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <vector>
using namespace std;
typedef unsigned int uii;
const int maxn = 100005;
int n, m, Q, u, v, o[maxn], d[maxn], q[maxn], col[maxn], vis[maxn];
vector<int> G[maxn];
void dfs(int u, int l, int c) {
  if (vis[u] >= l)
    return;
  vis[u] = l;
  if (!col[u])
    col[u] = c;
  for (uii i = 0; i < G[u].size(); ++i)
    dfs(G[u][i], l - 1, c);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i)
    scanf("%d%d%d", &o[i], &d[i], &q[i]);
  for (int i = Q - 1; i >= 0; --i)
    dfs(o[i], d[i] + 1, q[i]);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", col[i]);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100010

int n, m, q;
int color[MAXN], vq[MAXN], d[MAXN], c[MAXN], maxp[MAXN];
vector<int> adj[MAXN];

void dfs(int u, int p, int col) {
  if (p <= maxp[u])
    return;
  if (color[u] == 0) {
    color[u] = col;
  }
  int tam = adj[u].size();
  for (int i = 0; i < tam; i++) {
    int v = adj[u][i];
    if (p >= 1) {
      dfs(v, p - 1, col);
    }
  }
}

int main() {
  while (scanf("%d %d", &n, &m) != EOF) {
    int u, v;
    for (int i = 0; i < n; i++) {
      adj[i].clear();
    }
    for (int i = 0; i < m; i++) {
      scanf("%d %d", &u, &v);
      u--;
      v--;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
      scanf("%d %d %d", &vq[i], &d[i], &c[i]);
      vq[i]--;
    }
    memset(color, 0, sizeof(color));
    memset(maxp, -1, sizeof(maxp));
    for (int i = q - 1; i >= 0; i--) {
      dfs(vq[i], d[i], c[i]);
    }
    for (int i = 0; i < n; i++) {
      printf("%d\n", color[i]);
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100010

int n, m, q;
int color[MAXN], vq[MAXN], d[MAXN], c[MAXN], maxp[MAXN];
vector<int> adj[MAXN];

void dfs(int u, int p, int col) {
  if (p <= maxp[u])
    return;
  maxp[u] = p;
  if (color[u] == 0) {
    color[u] = col;
  }
  int tam = adj[u].size();
  for (int i = 0; i < tam; i++) {
    int v = adj[u][i];
    if (p >= 1) {
      dfs(v, p - 1, col);
    }
  }
}

int main() {
  while (scanf("%d %d", &n, &m) != EOF) {
    int u, v;
    for (int i = 0; i < n; i++) {
      adj[i].clear();
    }
    for (int i = 0; i < m; i++) {
      scanf("%d %d", &u, &v);
      u--;
      v--;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
      scanf("%d %d %d", &vq[i], &d[i], &c[i]);
      vq[i]--;
    }
    memset(color, 0, sizeof(color));
    memset(maxp, -1, sizeof(maxp));
    for (int i = q - 1; i >= 0; i--) {
      dfs(vq[i], d[i], c[i]);
    }
    for (int i = 0; i < n; i++) {
      printf("%d\n", color[i]);
    }
  }
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
//
//  main.cpp
//  backToProblemSolving
//
//  Created by Khaled Abdelfattah on 6/27/17.
//  Copyright © 2017 Khaled Abdelfattah. All rights reserved.
//

#include <bits/stdc++.h>
// #include <iostream>
using namespace std;
// #include "stdc++.h"

typedef long long ll;
#define MAX 1000000000
#define BASE1 23
#define BASE2 31
#define MOD1 1000000007
#define MOD2 217645177
#define eps 1e-6

int colors[100005];
vector<int> adjList[100005];
bool visited[100005][15];
void DFS(int node, int d, int c) {
  if (d < 0 || visited[node][d])
    return;
  if (colors[node] == 0)
    colors[node] = c;
  for (int i : adjList[node])
    DFS(i, d - 1, c);
}
int main() {
  int n, m;
  cin >> n >> m;
  memset(colors, 0, n);
  memset(visited, 0, sizeof visited);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    v--, u--;
    adjList[u].push_back(v);
    adjList[v].push_back(u);
  }
  int q;
  cin >> q;
  pair<pair<int, int>, int> *quers = new pair<pair<int, int>, int>[q];
  for (int i = 0; i < q; i++) {
    int s, d, c;
    scanf("%d %d %d", &s, &d, &c);
    quers[i] = {{s - 1, d}, c};
  }
  for (int i = q - 1; i >= 0; i--)
    DFS(quers[i].first.first, quers[i].first.second, quers[i].second);
  for (int i = 0; i < n; i++)
    cout << colors[i] << endl;
  return 0;
}


Fixed submission:
//
//  main.cpp
//  backToProblemSolving
//
//  Created by Khaled Abdelfattah on 6/27/17.
//  Copyright © 2017 Khaled Abdelfattah. All rights reserved.
//

#include <bits/stdc++.h>
// #include <iostream>
using namespace std;
// #include "stdc++.h"

typedef long long ll;
#define MAX 1000000000
#define BASE1 23
#define BASE2 31
#define MOD1 1000000007
#define MOD2 217645177
#define eps 1e-6

int colors[100005];
vector<int> adjList[100005];
bool visited[100005][15];
void DFS(int node, int d, int c) {
  if (d < 0 || visited[node][d])
    return;
  visited[node][d] = true;
  if (colors[node] == 0)
    colors[node] = c;
  for (int i : adjList[node])
    DFS(i, d - 1, c);
}
int main() {
  int n, m;
  cin >> n >> m;
  memset(colors, 0, n);
  memset(visited, 0, sizeof visited);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    v--, u--;
    adjList[u].push_back(v);
    adjList[v].push_back(u);
  }
  int q;
  cin >> q;
  pair<pair<int, int>, int> *quers = new pair<pair<int, int>, int>[q];
  for (int i = 0; i < q; i++) {
    int s, d, c;
    scanf("%d %d %d", &s, &d, &c);
    quers[i] = {{s - 1, d}, c};
  }
  for (int i = q - 1; i >= 0; i--)
    DFS(quers[i].first.first, quers[i].first.second, quers[i].second);
  for (int i = 0; i < n; i++)
    cout << colors[i] << endl;
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
/***********************
 *  Date: 2018.11.28    *
 *  Problem:            *
 *  Author: xuziyuan    *
 ***********************/

#include <bits/stdc++.h>

#define rt0 return 0
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define replet(c) for (char c = 'a'; c <= 'z'; c++)
#define LL long long
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair
#define sqr(a) ((a) * (a))

using namespace std;

const LL MOD = 1e9 + 7;

int n, m, q, appe[100010][20], coap[100010], len = 0; // appe:appearance
int list_top[100010], val[100010], nxt[100010];

void add(int x, int y) {
  val[++len] = y;
  nxt[len] = list_top[x];
  list_top[x] = len;
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  cin >> q;
  repn(i, q) {
    int x, y;
    scanf("%d%d%d", &x, &y, &coap[i]);
    appe[x][y] = i;
  }
  for (
      int i = 9; i >= 0;
      i--) // 算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环
  {
    repn(p, n) {
      for (int k = list_top[p]; k != 0; k = nxt[k]) // i在这里代表可以控制的范围
      {
        int j = val[k];
        appe[j][i] = max(
            appe[j][i],
            appe[p]
                [i + 1]); /*远一条边距离就要加1，这里是看后者可不可以覆盖前者，
可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/
      }
    }
  }
  repn(i, n) {
    int lst = 0;
    rep(j, 11) lst = max(lst, appe[i][j]);
    cout << coap[lst] << endl;
  }
  rt0;
}

Fixed submission:
/***********************
 *  Date: 2018.11.28    *
 *  Problem:            *
 *  Author: xuziyuan    *
 ***********************/

#include <bits/stdc++.h>

#define rt0 return 0
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define replet(c) for (char c = 'a'; c <= 'z'; c++)
#define LL long long
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair
#define sqr(a) ((a) * (a))

using namespace std;

const LL MOD = 1e9 + 7;

int n, m, q, appe[100010][20], coap[100010], len = 0; // appe:appearance
int list_top[200010], val[200010], nxt[200010];

void add(int x, int y) {
  val[++len] = y;
  nxt[len] = list_top[x];
  list_top[x] = len;
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  cin >> q;
  repn(i, q) {
    int x, y;
    scanf("%d%d%d", &x, &y, &coap[i]);
    appe[x][y] = i;
  }
  for (
      int i = 9; i >= 0;
      i--) // 算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环
  {
    repn(p, n) {
      for (int k = list_top[p]; k != 0; k = nxt[k]) // i在这里代表可以控制的范围
      {
        int j = val[k];
        appe[j][i] = max(
            appe[j][i],
            appe[p]
                [i + 1]); /*远一条边距离就要加1，这里是看后者可不可以覆盖前者，
可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/
      }
    }
  }
  repn(i, n) {
    int lst = 0;
    rep(j, 11) lst = max(lst, appe[i][j]);
    cout << coap[lst] << endl;
  }
  rt0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int n, m;
vector<int> pathes[100200];
int q;
int a[100200], b[100200], v[100200], d[100200], c[100200];
int colors[100200];
int painted[100020];

void paint(int from, int distance, int color) {
  if (distance <= painted[from]) {
    return;
  }
  if (colors[from] == 0) {
    colors[from] = color;
  }
  if (distance == 0) {
    return;
  }
  for (int i = 0; i < pathes[from].size(); i++) {
    paint(pathes[from][i], distance - 1, color);
  }
  return;
}
int main() {
  // cout.precision(10);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
    pathes[a[i]].push_back(b[i]);
    pathes[b[i]].push_back(a[i]);
  }
  for (int i = 1; i <= n; i++) {
    colors[i] = 0;
    painted[i] = -1;
  }
  cin >> q;
  for (int i = q; i >= 1; i--) {
    cin >> v[i] >> d[i] >> c[i];
  }
  for (int i = 1; i <= q; i++) {
    paint(v[i], d[i], c[i]);
  }
  for (int i = 1; i <= n; i++) {
    cout << colors[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int n, m;
vector<int> pathes[100200];
int q;
int a[100200], b[100200], v[100200], d[100200], c[100200];
int colors[100200];
int painted[100020];

void paint(int from, int distance, int color) {
  if (distance <= painted[from]) {
    return;
  }
  painted[from] = distance;
  if (colors[from] == 0) {
    colors[from] = color;
  }
  if (distance == 0) {
    return;
  }
  for (int i = 0; i < pathes[from].size(); i++) {
    paint(pathes[from][i], distance - 1, color);
  }
  return;
}
int main() {
  // cout.precision(10);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
    pathes[a[i]].push_back(b[i]);
    pathes[b[i]].push_back(a[i]);
  }
  for (int i = 1; i <= n; i++) {
    colors[i] = 0;
    painted[i] = -1;
  }
  cin >> q;
  for (int i = q; i >= 1; i--) {
    cin >> v[i] >> d[i] >> c[i];
  }
  for (int i = 1; i <= q; i++) {
    paint(v[i], d[i], c[i]);
  }
  for (int i = 1; i <= n; i++) {
    cout << colors[i] << endl;
  }
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second

typedef long long LL;

const int N = 1e5 + 10;

int f[N][11], ret[N];
int a[N], b[N], c[N];
int n, m, k;

vector<int> v[N];

void solve(int x, int y, int z) {
  if (~f[x][y])
    return;
  if (!ret[x])
    ret[x] = z;
  if (y <= 0)
    return;
  for (auto u : v[x]) {
    solve(u, y - 1, z);
  }
}

int main() {

  scanf("%d%d", &n, &m);
  rep(i, 1, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }

  scanf("%d", &k);
  rep(i, 1, k) { scanf("%d%d%d", a + i, b + i, c + i); }

  memset(f, -1, sizeof f);
  dec(i, k, 1) solve(a[i], b[i], c[i]);
  rep(i, 1, n) printf("%d\n", ret[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second

typedef long long LL;

const int N = 1e5 + 10;

int f[N][11], ret[N];
int a[N], b[N], c[N];
int n, m, k;

vector<int> v[N];

void solve(int x, int y, int z) {
  if (~f[x][y])
    return;
  if (!ret[x])
    ret[x] = z;
  if (y <= 0)
    return;
  for (auto u : v[x]) {
    solve(u, y - 1, z);
  }
  f[x][y] = 1;
}

int main() {

  scanf("%d%d", &n, &m);
  rep(i, 1, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }

  scanf("%d", &k);
  rep(i, 1, k) { scanf("%d%d%d", a + i, b + i, c + i); }

  memset(f, -1, sizeof f);
  dec(i, k, 1) solve(a[i], b[i], c[i]);
  rep(i, 1, n) printf("%d\n", ret[i]);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
int n, m, cnt, tot, fst[100004], pnt[100004], nxt[100004], ans[100004],
    dp[100004][12];
void add(int x, int y) {
  pnt[++tot] = y;
  nxt[tot] = fst[x];
  fst[x] = tot;
}
int main() {
  cin >> n >> m;
  int i, j, x, y;
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    add(x, y);
    add(y, x);
  }
  cin >> cnt;
  for (i = 1; i <= cnt; i++) {
    cin >> x >> y >> ans[i];
    dp[x][y] = i;
  }
  for (i = 9; i >= 0; i--)
    for (x = 1; x <= n; x++) {
      dp[x][i] = max(dp[x][i], dp[x][i + 1]);
      for (j = fst[x]; j; j = nxt[j])
        dp[x][i] = max(dp[x][i], dp[pnt[j]][i + 1]);
    }
  for (i = 1; i <= n; i++)
    cout << ans[dp[i][0]] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
int n, m, cnt, tot, fst[100004], pnt[100004 << 1], nxt[100004 << 1],
    ans[100004], dp[100004][12];
void add(int x, int y) {
  pnt[++tot] = y;
  nxt[tot] = fst[x];
  fst[x] = tot;
}
int main() {
  cin >> n >> m;
  int i, j, x, y;
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    add(x, y);
    add(y, x);
  }
  cin >> cnt;
  for (i = 1; i <= cnt; i++) {
    cin >> x >> y >> ans[i];
    dp[x][y] = i;
  }
  for (i = 9; i >= 0; i--)
    for (x = 1; x <= n; x++) {
      dp[x][i] = max(dp[x][i], dp[x][i + 1]);
      for (j = fst[x]; j; j = nxt[j])
        dp[x][i] = max(dp[x][i], dp[pnt[j]][i + 1]);
    }
  for (i = 1; i <= n; i++)
    cout << ans[dp[i][0]] << endl;
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int N, M, Q;
vector<int> adj[100000];
int dp[100000][11];
int c_arr[100000];

void go(int now, int d, int q_cnt) {
  for (int i = 0; i < d; i++) {
    dp[now][i] = max(dp[now][i], q_cnt);
  }
  if (d == 0) {
    return;
  }
  for (int i = 0; i < adj[now].size(); i++) {
    int next = adj[now][i];
    dp[next][d - 1] = max(dp[next][d - 1], q_cnt);
    go(next, d - 1, q_cnt);
  }
}

void visit(int now) {
  int m = -1;
  for (int d = 10; d >= 0; d--) {
    if (dp[now][d] > m) {
      go(now, d, dp[now][d]);
      m = dp[now][d];
    }
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= 10; j++) {
      dp[i][j] = -1;
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int v, d, c;
    cin >> v >> d >> c;
    v--;
    c_arr[i] = c;
    dp[v][d] = max(dp[v][d], i);
  }

  for (int i = 0; i < N; i++) {
    visit(i);
  }

  for (int i = 0; i < N; i++) {
    if (dp[i][0] == -1) {
      cout << 0 << endl;
    } else {
      cout << c_arr[dp[i][0]] << endl;
    }
  }
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int N, M, Q;
vector<int> adj[100000];
int dp[100000][11];
int c_arr[100000];

void go(int now, int d, int q_cnt) {
  for (int i = 0; i < d; i++) {
    dp[now][i] = max(dp[now][i], q_cnt);
  }
  if (d == 0) {
    return;
  }
  for (int i = 0; i < adj[now].size(); i++) {
    int next = adj[now][i];
    dp[next][d - 1] = max(dp[next][d - 1], q_cnt);
    go(next, d - 1, q_cnt);
  }
}

void visit(int now) {
  int m = -1;
  for (int d = 10; d >= 0; d--) {
    if (dp[now][d] > m) {
      go(now, d, dp[now][d]);
      m = dp[now][d];
    }
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= 10; j++) {
      dp[i][j] = -1;
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int v, d, c;
    cin >> v >> d >> c;
    v--;
    c_arr[i] = c;
    dp[v][d] = max(dp[v][d], i);
  }

  for (int d = 10; d > 0; d--) {
    for (int i = 0; i < N; i++) {
      dp[i][d - 1] = max(dp[i][d - 1], dp[i][d]);
      for (int j = 0; j < adj[i].size(); j++) {
        int next = adj[i][j];
        dp[next][d - 1] = max(dp[next][d - 1], dp[i][d]);
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (dp[i][0] == -1) {
      cout << 0 << endl;
    } else {
      cout << c_arr[dp[i][0]] << endl;
    }
  }
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#define N 200100

int n, m, q, i, dep[N], col[N], tot;
bool vis[N];
struct C {
  int v, d, c;
} c[N];
struct node {
  int to;
  node *nxt;
} g[N], *last[N];

inline void read(int &p) {
  char c = getchar();
  p = 0;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    p = p * 10 + c - 48, c = getchar();
}

void dfs(int v, int d, int c) {
  vis[v] = true;
  if (!col[v])
    col[v] = c, dep[v] = d;
  for (node *p = last[v]; p; p = p->nxt)
    if (dep[p->to] < d - 1 && !vis[p->to])
      dfs(p->to, d - 1, c);
  vis[v] = false;
}

int main() {
  read(n);
  read(m);
  for (i = 0; i < m; i++) {
    int a, b;
    read(a);
    read(b);
    last[a] = &(g[tot++] = (node){b, last[a]});
    last[b] = &(g[tot++] = (node){a, last[b]});
  }
  read(q);
  for (i = 0; i < q; i++)
    read(c[i].v), read(c[i].d), read(c[i].c);
  while (q--)
    dfs(c[q].v, c[q].d + 1, c[q].c);
  for (i = 1; i <= n; i++)
    printf("%d\n", col[i]);
}

Fixed submission:
#include <cstdio>
#define N 200100

int n, m, q, i, dep[N], col[N], tot;
bool vis[N];
struct C {
  int v, d, c;
} c[N];
struct node {
  int to;
  node *nxt;
} g[N], *last[N];

inline void read(int &p) {
  char c = getchar();
  p = 0;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    p = p * 10 + c - 48, c = getchar();
}

void dfs(int v, int d, int c) {
  vis[v] = true;
  if (!col[v])
    col[v] = c;
  dep[v] = d;
  for (node *p = last[v]; p; p = p->nxt)
    if (dep[p->to] < d - 1 && !vis[p->to])
      dfs(p->to, d - 1, c);
  vis[v] = false;
}

int main() {
  read(n);
  read(m);
  for (i = 0; i < m; i++) {
    int a, b;
    read(a);
    read(b);
    last[a] = &(g[tot++] = (node){b, last[a]});
    last[b] = &(g[tot++] = (node){a, last[b]});
  }
  read(q);
  for (i = 0; i < q; i++)
    read(c[i].v), read(c[i].d), read(c[i].c);
  while (q--)
    dfs(c[q].v, c[q].d + 1, c[q].c);
  for (i = 1; i <= n; i++)
    printf("%d\n", col[i]);
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <vector>

using namespace std;

int a[100000];
int c[100000];
int d[100000][3];
vector<int> v[100000];

void dfs(int x, int y, int z) {
  int i;

  if (c[x] == 0)
    c[x] = z;

  if (y == 0 || y < a[x])
    return;

  a[x] = y;

  for (i = 0; i < v[x].size(); i++)
    dfs(v[x][i], y - 1, z);
}

int main() {
  int n, m, q, i;

  scanf("%d %d", &n, &m);

  for (i = 0; i < m; i++) {
    int x, y;

    scanf("%d %d", &x, &y);

    x--;
    y--;

    v[x].push_back(y);
    v[y].push_back(x);
  }

  scanf("%d", &q);

  for (i = 0; i < q; i++)
    scanf("%d %d %d", &d[i][0], &d[i][1], &d[i][2]);

  for (i = q - 1; i >= 0; i--)
    dfs(d[i][0] - 1, d[i][1], d[i][2]);

  for (i = 0; i < n; i++)
    printf("%d\n", c[i]);

  return 0;
}


Fixed submission:
#include <cstdio>
#include <vector>

using namespace std;

int a[100000];
int c[100000];
int d[100000][3];
vector<int> v[100000];

void dfs(int x, int y, int z) {
  int i;

  if (c[x] == 0)
    c[x] = z;

  if (y == 0 || y <= a[x])
    return;

  a[x] = y;

  for (i = 0; i < v[x].size(); i++)
    dfs(v[x][i], y - 1, z);
}

int main() {
  int n, m, q, i;

  scanf("%d %d", &n, &m);

  for (i = 0; i < m; i++) {
    int x, y;

    scanf("%d %d", &x, &y);

    x--;
    y--;

    v[x].push_back(y);
    v[y].push_back(x);
  }

  scanf("%d", &q);

  for (i = 0; i < q; i++)
    scanf("%d %d %d", &d[i][0], &d[i][1], &d[i][2]);

  for (i = q - 1; i >= 0; i--)
    dfs(d[i][0] - 1, d[i][1], d[i][2]);

  for (i = 0; i < n; i++)
    printf("%d\n", c[i]);

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mk make_pair
#define pb push_back
#define pf push_front
typedef pair<int, int> pii;
const int mod = 1000000007;
const int INF = 1000000009;
const long long INFL = 1000000000000000018ll;

int N, M, Q;
int v[100100], d[100100], c[100100];
int dp[100100][11];
int col[100100];
vector<int> G[100100];

void dfs(int V, int D, int C) {
  if (~dp[V][D]) {
    return;
  }
  if (col[V] == 0) {
    col[V] = C;
  }
  if (D == 0)
    return;
  for (int i = 0; i < (int)G[V].size(); i++) {
    int to = G[V][i];
    dfs(to, D - 1, C);
  }
  return;
}

signed main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  memset(dp, -1, sizeof(dp));
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> d[i] >> c[i];
  }
  for (int i = Q - 1; i >= 0; i--) {
    v[i]--;
    dfs(v[i], d[i], c[i]);
  }
  for (int i = 0; i < N; i++) {
    printf("%d\n", col[i]);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mk make_pair
#define pb push_back
#define pf push_front
typedef pair<int, int> pii;
const int mod = 1000000007;
const int INF = 1000000009;
const long long INFL = 1000000000000000018ll;

int N, M, Q;
int v[100100], d[100100], c[100100];
int dp[100100][11];
int col[100100];
vector<int> G[100100];

void dfs(int V, int D, int C) {
  if (~dp[V][D]) {
    return;
  }
  dp[V][D] = 0;
  if (col[V] == 0) {
    col[V] = C;
  }
  if (D == 0)
    return;
  for (int i = 0; i < (int)G[V].size(); i++) {
    int to = G[V][i];
    dfs(to, D - 1, C);
  }
  return;
}

signed main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  memset(dp, -1, sizeof(dp));
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> d[i] >> c[i];
  }
  for (int i = Q - 1; i >= 0; i--) {
    v[i]--;
    dfs(v[i], d[i], c[i]);
  }
  for (int i = 0; i < N; i++) {
    printf("%d\n", col[i]);
  }

  return 0;
}