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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

int n, m, Q;
vector<int> G[MAX_N];
int color[MAX_N];
int op[MAX_N][3];
int af[MAX_N];

void dfs(int num, int u, int dep, int D) {
  if (af[u] >= D - dep) {
    return;
  }
  if (color[u] == 0) {
    color[u] = num;
  }
  if (D - dep > 0) {
    rep(i, G[u].size()) { dfs(num, G[u][i], dep + 1, D); }
  }
}

int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  rep(i, m) {
    scanf("%d%d", &x, &y);
    G[x - 1].push_back(y - 1);
    G[y - 1].push_back(x - 1);
  }
  scanf("%d", &Q);
  rep(i, Q) { scanf("%d%d%d", &op[i][0], &op[i][1], &op[i][2]); }
  rep(i, n) { af[i] = -1; }
  for (int i = Q - 1; i >= 0; i--) {
    dfs(op[i][2], op[i][0] - 1, 0, op[i][1]);
  }
  rep(i, n) { printf("%d\n", color[i]); }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

int n, m, Q;
vector<int> G[MAX_N];
int color[MAX_N];
int op[MAX_N][3];
int af[MAX_N];

void dfs(int num, int u, int dep, int D) {
  if (af[u] >= D - dep) {
    return;
  }
  af[u] = D - dep;
  if (color[u] == 0) {
    color[u] = num;
  }
  if (D - dep > 0) {
    rep(i, G[u].size()) { dfs(num, G[u][i], dep + 1, D); }
  }
}

int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  rep(i, m) {
    scanf("%d%d", &x, &y);
    G[x - 1].push_back(y - 1);
    G[y - 1].push_back(x - 1);
  }
  scanf("%d", &Q);
  rep(i, Q) { scanf("%d%d%d", &op[i][0], &op[i][1], &op[i][2]); }
  rep(i, n) { af[i] = -1; }
  for (int i = Q - 1; i >= 0; i--) {
    dfs(op[i][2], op[i][0] - 1, 0, op[i][1]);
  }
  rep(i, n) { printf("%d\n", color[i]); }
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>

using namespace std;
typedef long long ll;
#define pl pair<ll, ll>
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define foreach(itr, c)                                                        \
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); itr++)
#define dbg(x) cout << #x "=" << (x) << endl
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back(a)
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second

ll dp[10010][11];
ll c[10010];
int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<ll>> edge(n + 1);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    edge[a].pb(b);
    edge[b].pb(a);
  }
  ll q;
  cin >> q;
  FOR(i, 1, q + 1) {
    ll v, d;
    cin >> v >> d >> c[i];
    dp[v][d] = i;
  }

  for (ll i = 9; i >= 0; i--) {
    FOR(j, 1, n + 1) {
      // 一つ上のものが塗られているか.
      dp[j][i] = max(dp[j][i], dp[j][i + 1]);
      // 接続先が塗られているか.
      for (auto to : edge[j]) {
        dp[j][i] = max(dp[j][i], dp[to][i + 1]);
      }
    }
  }
  //(N+M)d

  FOR(i, 1, n + 1) { cout << c[dp[i][0]] << endl; }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>

using namespace std;
typedef long long ll;
#define pl pair<ll, ll>
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define foreach(itr, c)                                                        \
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); itr++)
#define dbg(x) cout << #x "=" << (x) << endl
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back(a)
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second

ll dp[100010][11];
ll c[100010];
int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<ll>> edge(n + 1);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    edge[a].pb(b);
    edge[b].pb(a);
  }
  ll q;
  cin >> q;
  FOR(i, 1, q + 1) {
    ll v, d;
    cin >> v >> d >> c[i];
    dp[v][d] = i;
  }

  for (ll i = 9; i >= 0; i--) {
    FOR(j, 1, n + 1) {
      // 一つ上のものが塗られているか.
      dp[j][i] = max(dp[j][i], dp[j][i + 1]);
      // 接続先が塗られているか.
      for (auto to : edge[j]) {
        dp[j][i] = max(dp[j][i], dp[to][i + 1]);
      }
    }
  }
  //(N+M)d

  FOR(i, 1, n + 1) { cout << c[dp[i][0]] << endl; }

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
typedef long long int ll;
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define EREP(i, n) for (int i = 1; i <= (n); i++)
const ll MOD = 1000000007;

ll N, C, M, K, temp, ans = 0;
ll Q, query[10010][3];
vector<ll> T;
bool fir = true, go = false;
vector<int> g[100010];
int flag[100010];
int a[100010] = {0};

void dfs(int v, int dis, int color) {
  if (flag[v] >= dis || dis < 0)
    return;
  if (!a[v])
    a[v] = color;

  flag[v] = dis;
  for (auto x : g[v]) {
    dfs(x, dis - 1, color);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  for (int i = 0; i < 100010; i++)
    flag[i] = -10;
  cin >> N >> M;
  REP(i, M) {
    cin >> C >> K;
    g[C].push_back(K);
    g[K].push_back(C);
  }
  cin >> Q;
  REP(i, Q) { cin >> query[i][0] >> query[i][1] >> query[i][2]; }
  for (int i = Q - 1; i >= 0; i--) {
    dfs(query[i][0], query[i][1], query[i][2]);
  }
  EREP(i, N) { cout << a[i] << endl; }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
typedef long long int ll;
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define EREP(i, n) for (int i = 1; i <= (n); i++)
const ll MOD = 1000000007;

ll N, C, M, K, temp, ans = 0;
ll Q, query[100010][3];
vector<ll> T;
bool fir = true, go = false;
vector<int> g[100010];
int flag[100010];
int a[100010] = {0};

void dfs(int v, int dis, int color) {
  if (flag[v] >= dis || dis < 0)
    return;
  if (!a[v])
    a[v] = color;

  flag[v] = dis;
  for (auto x : g[v]) {
    dfs(x, dis - 1, color);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  for (int i = 0; i < 100010; i++)
    flag[i] = -10;
  cin >> N >> M;
  REP(i, M) {
    cin >> C >> K;
    g[C].push_back(K);
    g[K].push_back(C);
  }
  cin >> Q;
  REP(i, Q) { cin >> query[i][0] >> query[i][1] >> query[i][2]; }
  for (int i = Q - 1; i >= 0; i--) {
    dfs(query[i][0], query[i][1], query[i][2]);
  }
  EREP(i, N) { cout << a[i] << endl; }
  return 0;
}
Problem p03768 (C++)

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

const int maxn = 200000;

vector<int> graph[maxn];
int color[maxn];
bool visited[maxn][20];

void dfs(int v, int d, int c) {
  if (color[v] == 0) {
    color[v] = c;
  }
  if (visited[v][d] or d == 0)
    return;
  dfs(v, d - 1, c);

  for (int to : graph[v]) {
    dfs(to, d - 1, c);
  }
}

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

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
  }

  int Q;
  cin >> Q;

  vector<int> v(Q), d(Q), c(Q);
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }

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

  for (int i = 0; i < N; i++) {
    cout << color[i] << endl;
  }

  return 0;
}

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

const int maxn = 200000;

vector<int> graph[maxn];
int color[maxn];
bool visited[maxn][20];

void dfs(int v, int d, int c) {
  if (color[v] == 0) {
    color[v] = c;
  }
  if (visited[v][d] or d == 0)
    return;
  visited[v][d] = true;

  dfs(v, d - 1, c);

  for (int to : graph[v]) {
    dfs(to, d - 1, c);
  }
}

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

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
  }

  int Q;
  cin >> Q;

  vector<int> v(Q), d(Q), c(Q);
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }

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

  for (int i = 0; i < N; i++) {
    cout << color[i] << endl;
  }

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
const int N = 200002;
int n, m, q, i, j, k, a, b, h[N], t[N], v[N];
int col[N], vis[N], x[N], d[N], c[N];
void add(int a, int b) {
  t[++k] = h[a];
  h[a] = k;
  v[k] = b;
}
void init() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &a, &b);
    add(a, b);
    add(b, a);
  }
  scanf("%d", &q);
  for (i = 1; i <= q; i++)
    scanf("%d%d%d", x + i, d + i, c + i);
}
void dfs(int i, int d, int c) {
  if (vis[i] >> d)
    return;
  vis[i] != 1 << d;
  if (!col[i])
    col[i] = c;
  if (d)
    for (int j = h[i]; j; j = t[j])
      dfs(v[j], d - 1, c);
}
void work() {
  for (i = q; i; i--)
    dfs(x[i], d[i], c[i]);
  for (i = 1; i <= n; i++)
    printf("%d\n", col[i]);
}
int main() {
  init();
  work();
  return 0;
}

Fixed submission:
#include <cstdio>
const int N = 200002;
int n, m, q, i, j, k, a, b, h[N], t[N], v[N];
int col[N], vis[N], x[N], d[N], c[N];
void add(int a, int b) {
  t[++k] = h[a];
  h[a] = k;
  v[k] = b;
}
void init() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &a, &b);
    add(a, b);
    add(b, a);
  }
  scanf("%d", &q);
  for (i = 1; i <= q; i++)
    scanf("%d%d%d", x + i, d + i, c + i);
}
void dfs(int i, int d, int c) {
  if (vis[i] >> d)
    return;
  vis[i] |= 1 << d;
  if (!col[i])
    col[i] = c;
  if (d)
    for (int j = h[i]; j; j = t[j])
      dfs(v[j], d - 1, c);
}
void work() {
  for (i = q; i; i--)
    dfs(x[i], d[i], c[i]);
  for (i = 1; i <= n; i++)
    printf("%d\n", col[i]);
}
int main() {
  init();
  work();
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define N 1000005
using namespace std;

int n, m, q, dp[15][N], u[N], a[N], v[N], d[N], c[N];
vector<int> g[N];

void yap(int v, int d, int c) {
  if (dp[d][v])
    return;
  dp[v][d] = 1;
  if (!u[v]) {
    a[v] = c;
    u[v] = 1;
  }
  if (d == 0)
    return;
  for (int i = 0; i < g[v].size(); i++)
    yap(g[v][i], d - 1, c);
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  scanf("%d %d", &n, &m);
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++)
    scanf("%d %d %d", v + i, d + i, c + i);
  for (int i = q; i >= 1; i--)
    yap(v[i], d[i], c[i]);
  for (int i = 1; i <= n; i++)
    printf("%d\n", a[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define N 1000005
using namespace std;

int n, m, q, dp[15][N], u[N], a[N], v[N], d[N], c[N];
vector<int> g[N];

void yap(int v, int d, int c) {
  if (dp[d][v])
    return;
  dp[d][v] = 1;
  if (!u[v]) {
    a[v] = c;
    u[v] = 1;
  }
  if (d == 0)
    return;
  for (int i = 0; i < g[v].size(); i++)
    yap(g[v][i], d - 1, c);
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  scanf("%d %d", &n, &m);
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++)
    scanf("%d %d %d", v + i, d + i, c + i);
  for (int i = q; i >= 1; i--)
    yap(v[i], d[i], c[i]);
  for (int i = 1; i <= n; i++)
    printf("%d\n", a[i]);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class point {
public:
  int color;
  int nf;
  vector<int> road;
  int lu;
  point() : color(0), nf(-1), lu(-1) {}
};
#define pa pair<int, int>
int main() {
  int N, M;
  cin >> N >> M;
  vector<point> map(N + 1);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    map[a].road.push_back(b);
    map[b].road.push_back(a);
  }
  int time;
  cin >> time;
  vector<int> place(time), distance(time), newcolor(time);
  for (int i = time - 1; i >= 0; --i) {
    cin >> place[i] >> distance[i] >> newcolor[i];
  }
  queue<pa> que;
  for (int i = 0; i < time; ++i) {
    if (map[place[i]].nf >= distance[i]) {
      continue;
    }
    map[place[i]].lu = i;
    que.push(pa(place[i], 0));
    while (!que.empty()) {
      pa dummy = que.front();
      que.pop();
      if (dummy.second > distance[i])
        continue;
      map[dummy.first].nf =
          max(distance[i] - dummy.second, map[dummy.first].nf);
      if (map[dummy.first].color == 0) {
        map[dummy.first].color = newcolor[i];
      }
      for (int j = 0, nmax = map[dummy.first].road.size(); j < nmax; ++j) {
        if (map[map[dummy.first].road[j]].lu != i) {
          map[map[dummy.first].road[j]].lu = i;
          que.push(pa(map[dummy.first].road[j], dummy.second + 1));
        }
      }
    }
  }
  for (int i = 1; i <= N; ++i) {
    cout << map[i].color << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class point {
public:
  int color;
  int nf;
  vector<int> road;
  int lu;
  point() : color(0), nf(-1), lu(-1) {}
};
#define pa pair<int, int>
int main() {
  int N, M;
  cin >> N >> M;
  vector<point> map(N + 1);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    map[a].road.push_back(b);
    map[b].road.push_back(a);
  }
  int time;
  cin >> time;
  vector<int> place(time), distance(time), newcolor(time);
  for (int i = time - 1; i >= 0; --i) {
    cin >> place[i] >> distance[i] >> newcolor[i];
  }
  queue<pa> que;
  for (int i = 0; i < time; ++i) {
    if (map[place[i]].nf >= distance[i]) {
      continue;
    }
    map[place[i]].lu = i;
    que.push(pa(place[i], 0));
    while (!que.empty()) {
      pa dummy = que.front();
      que.pop();
      if (dummy.second > distance[i])
        continue;
      if (map[dummy.first].nf >= distance[i] - dummy.second) {
        continue;
      }
      map[dummy.first].nf = distance[i] - dummy.second;
      if (map[dummy.first].color == 0) {
        map[dummy.first].color = newcolor[i];
      }
      for (int j = 0, nmax = map[dummy.first].road.size(); j < nmax; ++j) {
        if (map[map[dummy.first].road[j]].lu != i) {
          map[map[dummy.first].road[j]].lu = i;
          que.push(pa(map[dummy.first].road[j], dummy.second + 1));
        }
      }
    }
  }
  for (int i = 1; i <= N; ++i) {
    cout << map[i].color << endl;
  }
  return 0;
}
Problem p03768 (C++)

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

const int N = 100000 + 5;

int n, m;
int d[N], v[N], c[N];

int cnt, first[N], to[2 * N], next[2 * N];
void add(int u, int v) {
  to[++cnt] = v;
  next[cnt] = first[u];
  first[u] = cnt;
  to[++cnt] = u;
  next[cnt] = first[v];
  first[v] = cnt;
}

int col[N], mark[N];
void work(int now, int len, int color) {
  if (len < 0 || (col[now] > 0 && mark[now] >= len))
    return;
  if (col[now] == 0)
    col[now] = color, mark[now] = len;
  for (int p = first[now]; p; p = next[p])
    work(to[p], len - 1, color);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b; i <= m; i++) {
    scanf("%d%d", &a, &b);
    add(a, b);
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++)
    scanf("%d%d%d", &v[i], &d[i], &c[i]);
  for (int i = Q; i >= 1; i--)
    work(v[i], d[i], c[i]);
  for (int i = 1; i <= n; i++)
    printf("%d\n", col[i]);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;

const int N = 100000 + 5;

int n, m;
int d[N], v[N], c[N];

int cnt, first[N], to[2 * N], next[2 * N];
void add(int u, int v) {
  to[++cnt] = v;
  next[cnt] = first[u];
  first[u] = cnt;
  to[++cnt] = u;
  next[cnt] = first[v];
  first[v] = cnt;
}

int col[N], mark[N];
void work(int now, int len, int color) {
  if (len < 0 || (col[now] > 0 && mark[now] >= len))
    return;
  if (col[now] == 0)
    col[now] = color;
  mark[now] = len;
  for (int p = first[now]; p; p = next[p])
    work(to[p], len - 1, color);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b; i <= m; i++) {
    scanf("%d%d", &a, &b);
    add(a, b);
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++)
    scanf("%d%d%d", &v[i], &d[i], &c[i]);
  for (int i = Q; i >= 1; i--)
    work(v[i], d[i], c[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>
#define ll long long
#define inf 0x3f3f3f3f
#define re register
#define N 100007
using namespace std;
struct node {
  int u, d;
};
struct edge {
  int to, next;
} e[N << 1];
inline int read() {
  int x = 0, w = 0;
  char ch = getchar();
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return w ? -x : x;
}
int cnt, head[N], dis[N], vis[N], r[N], s[N], d[N], c[N], col[N], tot, n, m,
    ttt;
inline void add(int u, int v) {
  e[++cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void dfs(int u, int d, int c) {
  if (!d || d < r[u])
    return;
  r[u] = d;
  if (!col[u])
    col[u] = c;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    dfs(v, d - 1, c);
  }
}
signed main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  int Q = read();
  for (int i = 1; i <= Q; ++i) {
    s[i] = read(), d[i] = read(), c[i] = read();
  }
  for (int i = Q; i; --i) {
    dfs(s[i], d[i] + 1, c[i]);
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\n", col[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define re register
#define N 100007
using namespace std;
struct node {
  int u, d;
};
struct edge {
  int to, next;
} e[N << 1];
inline int read() {
  int x = 0, w = 0;
  char ch = getchar();
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return w ? -x : x;
}
int cnt, head[N], dis[N], vis[N], r[N], s[N], d[N], c[N], col[N], tot, n, m,
    ttt;
inline void add(int u, int v) {
  e[++cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void dfs(int u, int d, int c) {
  if (!d || d <= r[u])
    return;
  r[u] = d;
  if (!col[u])
    col[u] = c;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    dfs(v, d - 1, c);
  }
}
signed main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  int Q = read();
  for (int i = 1; i <= Q; ++i) {
    s[i] = read(), d[i] = read(), c[i] = read();
  }
  for (int i = Q; i; --i) {
    dfs(s[i], d[i] + 1, c[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 <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <stdio.h>
using namespace std;
int N, M, Q;
vector<int> E[100010];
int V[100010];
int D[100010];
int C[100010];
int ans[100010];
int memo[100010]; // これ未満の値が来たら打ち切ってよい
void bfs(int i) {
  if (memo[V[i]] > D[i])
    return;
  queue<int> que;
  int dist[100010] = {};
  que.push(V[i]);
  dist[V[i]] = D[i] + 1;
  memo[V[i]] = D[i] + 1;
  if (!ans[V[i]])
    ans[V[i]] = C[i];
  while (que.size()) {
    int p = que.front();
    que.pop();
    if (dist[p] <= 1)
      break;
    for (int j = 0; j < E[p].size(); j++) {
      int q = E[p].at(j);
      if (memo[q] < dist[p] && dist[q] == 0) {
        que.push(q);
        dist[q] = dist[p] - 1;
        memo[q] = dist[q];
        if (!ans[q])
          ans[q] = C[i];
      }
    }
  }
  return;
}
int main() {
  cin >> N >> M;
  fill(memo, memo + 100010, -1);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    scanf("%d%d%d", &V[i], &D[i], &C[i]);
    V[i]--;
  }
  for (int i = Q - 1; i >= 0; i--) {
    bfs(i);
  }
  for (int i = 0; i < N; i++) {
    printf("%d\n", ans[i]);
  }
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <stdio.h>
using namespace std;
int N, M, Q;
vector<int> E[100010];
int V[100010];
int D[100010];
int C[100010];
int ans[100010];
int memo[100010]; // これ未満の値が来たら打ち切ってよい
void bfs(int i) {
  if (memo[V[i]] > D[i])
    return;
  queue<int> que;
  int dist[100010] = {};
  que.push(V[i]);
  dist[V[i]] = D[i] + 1;
  memo[V[i]] = D[i] + 1;
  if (!ans[V[i]])
    ans[V[i]] = C[i];
  while (que.size()) {
    int p = que.front();
    que.pop();
    if (dist[p] <= 1)
      break;
    for (int j = 0; j < E[p].size(); j++) {
      int q = E[p].at(j);
      if (memo[q] < dist[p] - 1 && dist[q] == 0) {
        que.push(q);
        dist[q] = dist[p] - 1;
        memo[q] = dist[q];
        if (!ans[q])
          ans[q] = C[i];
      }
    }
  }
  return;
}
int main() {
  cin >> N >> M;
  fill(memo, memo + 100010, -1);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    scanf("%d%d%d", &V[i], &D[i], &C[i]);
    V[i]--;
  }
  for (int i = Q - 1; i >= 0; i--) {
    bfs(i);
  }
  for (int i = 0; i < N; i++) {
    printf("%d\n", ans[i]);
  }
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
// ### In The Name Of GOD ###

//%% Remember; (((He))) is The (((Accepter))) %%

// GOD IS GREATER ...

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define sz(a) (int)(a.size())

const int MAXN = 1e5 + 10, MAXD = 11;
int mark[MAXN], ans[MAXN];

struct relax {
  int v, d, c;
} a[MAXN];

vector<int> adj[MAXN];

void bfs(int v, int d, int c) {
  if (mark[v] >= d)
    return;
  if (!ans[v])
    ans[v] = c;
  mark[v] = d;
  queue<pair<int, int>> q;
  q.push({v, d});
  while (sz(q)) {
    int v = q.front().first;
    int d = q.front().second;
    if (!ans[v])
      ans[v] = c;
    for (int i : adj[v]) {
      if (mark[i] < d - 1) {
        mark[v] = d - 1;
        q.push({i, d - 1});
      }
    }
    q.pop();
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  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);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i].v >> a[i].d >> a[i].c;
    a[i].v--;
  }
  for (int i = 0; i < n; i++)
    mark[i] = -1;
  reverse(a, a + q);
  for (int i = 0; i < q; i++) {
    bfs(a[i].v, a[i].d, a[i].c);
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << '\n';
}


Fixed submission:
// ### In The Name Of GOD ###

//%% Remember; (((He))) is The (((Accepter))) %%

// GOD IS GREATER ...

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define sz(a) (int)(a.size())

const int MAXN = 1e5 + 10, MAXD = 11;
int mark[MAXN], ans[MAXN];

struct relax {
  int v, d, c;
} a[MAXN];

vector<int> adj[MAXN];

void bfs(int v, int d, int c) {
  if (mark[v] >= d)
    return;
  if (!ans[v])
    ans[v] = c;
  mark[v] = d;
  queue<pair<int, int>> q;
  q.push({v, d});
  while (sz(q)) {
    int v = q.front().first;
    int d = q.front().second;
    if (!ans[v])
      ans[v] = c;
    for (int i : adj[v]) {
      if (mark[i] < d - 1) {
        mark[i] = d - 1;
        q.push({i, d - 1});
      }
    }
    q.pop();
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  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);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i].v >> a[i].d >> a[i].c;
    a[i].v--;
  }
  for (int i = 0; i < n; i++)
    mark[i] = -1;
  reverse(a, a + q);
  for (int i = 0; i < q; i++) {
    bfs(a[i].v, a[i].d, a[i].c);
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << '\n';
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
int n, m, q;
vector<int> e[100005];
struct node {
  int v, d, c;
};
node t[100005];
void paint(node no) {
  auto cur = no.v;
  // cout << cur << endl;
  if (t[cur].c != -1 && t[cur].d >= no.d) {
    return;
  } else if (t[cur].c != -1) {
    if (no.d == 0)
      return;
    node ne{no.v, no.d - 1, no.c};
    for (auto to : e[cur]) {
      ne.v = to;
      paint(ne);
    }
  } else {
    t[cur] = no;
    if (no.d == 0)
      return;
    node ne{no.v, no.d - 1, no.c};
    for (auto to : e[cur]) {
      ne.v = to;
      paint(ne);
    }
  }
}
vector<node> query;
int main() {
  cin >> n >> m;
  int a, b;
  int v, d, c;
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  cin >> q;
  node ini{0, 0, -1};
  rep(i, n) {
    ini.v = i;
    t[i] = ini;
  }
  rep(i, q) {
    cin >> v >> d >> c;
    v--;
    query.push_back({v, d, c});
  }
  reverse(query.begin(), query.end());
  rep(i, q) { paint(query[i]); }
  rep(i, n) {
    if (t[i].c == -1)
      t[i].c = 0;
    cout << t[i].c << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
int n, m, q;
vector<int> e[100005];
struct node {
  int v, d, c;
};
node t[100005];
void paint(node no) {
  auto cur = no.v;
  // cout << cur << endl;
  if (t[cur].c != -1 && t[cur].d >= no.d) {
    return;
  } else if (t[cur].c != -1) {
    t[cur].d = no.d;
    if (no.d == 0)
      return;
    node ne{no.v, no.d - 1, no.c};
    for (auto to : e[cur]) {
      ne.v = to;
      paint(ne);
    }
  } else {
    t[cur] = no;
    if (no.d == 0)
      return;
    node ne{no.v, no.d - 1, no.c};
    for (auto to : e[cur]) {
      ne.v = to;
      paint(ne);
    }
  }
}
vector<node> query;
int main() {
  cin >> n >> m;
  int a, b;
  int v, d, c;
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  cin >> q;
  node ini{0, 0, -1};
  rep(i, n) {
    ini.v = i;
    t[i] = ini;
  }
  rep(i, q) {
    cin >> v >> d >> c;
    v--;
    query.push_back({v, d, c});
  }
  reverse(query.begin(), query.end());
  rep(i, q) { paint(query[i]); }
  rep(i, n) {
    if (t[i].c == -1)
      t[i].c = 0;
    cout << t[i].c << endl;
  }
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#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, q;
vector<int> g[100004];
int juv[100004];
int col[100004], qu;
void bfs(int s1, int d1, int c1) {
  queue<pair<int, int>> q;
  q.push(make_pair(s1, 0));
  while (!q.empty()) {
    pair<int, int> x = q.front();
    q.pop();
    if (d1 - x.second < juv[x.first])
      continue;
    else
      juv[x.first] = d1 - x.second;
    if (col[x.first] > 0)
      ;
    else
      col[x.first] = c1;
    if (x.second >= d1)
      continue;
    else
      for (int i = 0; i < g[x.first].size(); i++)
        q.push(make_pair(g[x.first][i], x.second + 1));
  }
}
int s[100004], d[100004], c[100004];
int main() {
  memset(col, 0, sizeof(col));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  cin >> q;
  for (int i = 0; i < q; i++)
    cin >> s[i] >> d[i] >> c[i];
  reverse(s, s + q);
  reverse(d, d + q);
  reverse(c, c + q);
  for (int i = 0; i < q; i++)
    bfs(s[i], d[i], c[i]);
  for (int i = 1; i <= n; i++)
    cout << col[i] << 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, q;
vector<int> g[100004];
int juv[100004];
int col[100004], qu;
void bfs(int s1, int d1, int c1) {
  queue<pair<int, int>> q;
  q.push(make_pair(s1, 0));
  while (!q.empty()) {
    pair<int, int> x = q.front();
    q.pop();
    if (x.second != d1 && d1 - x.second <= juv[x.first])
      continue;
    else
      juv[x.first] = d1 - x.second;
    if (col[x.first] > 0)
      ;
    else
      col[x.first] = c1;
    if (x.second >= d1)
      continue;
    else
      for (int i = 0; i < g[x.first].size(); i++)
        q.push(make_pair(g[x.first][i], x.second + 1));
  }
}
int s[100004], d[100004], c[100004];
int main() {
  memset(col, 0, sizeof(col));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  cin >> q;
  for (int i = 0; i < q; i++)
    cin >> s[i] >> d[i] >> c[i];
  reverse(s, s + q);
  reverse(d, d + q);
  reverse(c, c + q);
  for (int i = 0; i < q; i++)
    bfs(s[i], d[i], c[i]);
  for (int i = 1; i <= n; i++)
    cout << col[i] << endl;
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int n, m, Q, D[101000], head, tail;
vector<int> E[101000];
int v[101000][12], Col[101000];
struct point {
  int a, d, c;
} w[101000];
int QQ[101000][2];
void Ins(int a, int d, int c) {
  if (v[a][d])
    return;
  v[a][d] = 1;
  if (!Col[a])
    Col[a] = c;
  tail++;
  QQ[tail][0] = a, QQ[tail][1] = d;
}
int main() {
  int i, a, b;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    E[a].push_back(b);
    E[b].push_back(a);
  }
  scanf("%d", &Q);
  for (i = 0; i < Q; i++) {
    scanf("%d%d%d", &w[i].a, &w[i].d, &w[i].c);
  }
  for (i = Q - 1; i >= 0; i--) {
    head = tail = 0;
    Ins(w[i].a, w[i].d, w[i].c);
    while (head < tail) {
      head++;
      int x = QQ[head][0], d = QQ[head][1];
      if (!d)
        continue;
      for (int j = 0; j < E[x].size(); j++) {
        Ins(E[x][j], d - 1, w[i].c);
      }
    }
  }
  for (i = 1; i <= n; i++)
    printf("%d ", Col[i]);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int n, m, Q, D[101000], head, tail;
vector<int> E[101000];
int v[101000][12], Col[101000];
struct point {
  int a, d, c;
} w[101000];
int QQ[1010000][2];
void Ins(int a, int d, int c) {
  if (v[a][d])
    return;
  v[a][d] = 1;
  if (!Col[a])
    Col[a] = c;
  tail++;
  QQ[tail][0] = a, QQ[tail][1] = d;
}
int main() {
  int i, a, b;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    E[a].push_back(b);
    E[b].push_back(a);
  }
  scanf("%d", &Q);
  for (i = 0; i < Q; i++) {
    scanf("%d%d%d", &w[i].a, &w[i].d, &w[i].c);
  }
  for (i = Q - 1; i >= 0; i--) {
    head = tail = 0;
    Ins(w[i].a, w[i].d, w[i].c);
    while (head < tail) {
      head++;
      int x = QQ[head][0], d = QQ[head][1];
      if (!d)
        continue;
      for (int j = 0; j < E[x].size(); j++) {
        Ins(E[x][j], d - 1, w[i].c);
      }
    }
  }
  for (i = 1; i <= n; i++)
    printf("%d ", Col[i]);
}
Problem p03768 (C++)

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vec vector
#define ll long long
#define pb push_back

int dp[200000][12];
vec<vec<int>> x;

void change(int v, int d, int q) {
  if (dp[v][d + 1] >= q)
    return;
  rep(i, d + 1) if (!dp[v][i]) dp[v][i] = q;
  if (d == 0)
    return;
  rep(i, x[v].size()) change(x[v][i], d - 1, q);
}

int main(void) {
  int n, m, q;
  cin >> n >> m;
  x.resize(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    x[a].pb(b);
    x[b].pb(a);
  }
  cin >> q;
  vec<int> v(q + 1), d(q + 1), c(q + 1);
  c[0] = 0;
  rep(i, q) cin >> v[i + 1] >> d[i + 1] >> c[i + 1];

  for (int i = q; i >= 1; i--)
    change(v[i] - 1, d[i], i);
  rep(i, n) cout << c[dp[i][0]] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vec vector
#define ll long long
#define pb push_back

int dp[200000][12];
vec<vec<int>> x;

void change(int v, int d, int q) {
  if (dp[v][d + 1] >= q)
    return;
  for (int i = d + 1; i >= 0; i--) {
    if (dp[v][i] < q)
      dp[v][i] = q;
    else
      break;
  }
  if (d == 0)
    return;
  rep(i, x[v].size()) change(x[v][i], d - 1, q);
}

int main(void) {
  int n, m, q;
  cin >> n >> m;
  x.resize(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    x[a].pb(b);
    x[b].pb(a);
  }
  cin >> q;
  vec<int> v(q + 1), d(q + 1), c(q + 1);
  c[0] = 0;
  rep(i, q) cin >> v[i + 1] >> d[i + 1] >> c[i + 1];

  for (int i = q; i >= 1; i--)
    change(v[i] - 1, d[i], i);
  rep(i, n) cout << c[dp[i][0]] << endl;
  return 0;
}
Problem p03768 (C++)

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

using namespace std;

const int MAX_N = 1e5 + 5;

vector<int> adj[MAX_N];
bool vis[MAX_N][12];
int color[MAX_N];

void paint(int v, int d, int c) {
  if (vis[v][d])
    return;
  if (color[v] == 0)
    color[v] = c;

  if (d != 0) {
    for (int nxt : adj[v]) {
      paint(nxt, d - 1, c);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;

    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  vector<pair<int, pair<int, int>>> queries;
  int qc;
  cin >> qc;

  for (int i = 0; i < qc; i++) {
    int v, d, c;
    cin >> v >> d >> c;
    queries.push_back(make_pair(v, make_pair(d, c)));
  }

  reverse(queries.begin(), queries.end());
  for (auto pr : queries) {
    paint(pr.first, pr.second.first, pr.second.second);
  }

  for (int i = 1; i <= n; i++) {
    cout << color[i] << '\n';
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 1e5 + 5;

vector<int> adj[MAX_N];
bool vis[MAX_N][12];
int color[MAX_N];

void paint(int v, int d, int c) {
  if (vis[v][d])
    return;
  vis[v][d] = 1;
  if (color[v] == 0)
    color[v] = c;

  if (d != 0) {
    for (int nxt : adj[v]) {
      paint(nxt, d - 1, c);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;

    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  vector<pair<int, pair<int, int>>> queries;
  int qc;
  cin >> qc;

  for (int i = 0; i < qc; i++) {
    int v, d, c;
    cin >> v >> d >> c;
    queries.push_back(make_pair(v, make_pair(d, c)));
  }

  reverse(queries.begin(), queries.end());
  for (auto pr : queries) {
    paint(pr.first, pr.second.first, pr.second.second);
  }

  for (int i = 1; i <= n; i++) {
    cout << color[i] << '\n';
  }
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h> //这题是倒着染色吧
using namespace std;

const int N = 1e5 + 10;
int n, m;
struct edge {
  int y, nxt;
} e[N << 1];
int lin[N], len;
int ans[N][20];
struct QUE {
  int v, d, c;
} q[N];

inline int read() {
  int s = 0, f = 1;
  char ch;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    s = (s << 1) + (s << 3) + ch - '0';
  return s * f;
}

inline void insert(int xx, int yy) {
  e[++len] = (edge){yy, lin[xx]};
  lin[xx] = len;
}

inline void dfs(int v, int d, int c) {
  if (d == -1)
    return;
  if (ans[v][d])
    return; // 因为是倒着染，所以不用再染一遍
  ans[v][d] = c;
  for (int i = lin[v]; i; i = e[i].nxt)
    dfs(e[i].y, d - 1, c);
}

int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read();
    insert(x, y);
    insert(y, x);
  }
  for (int i = 1; i <= n; ++i)
    insert(i, i);
  int Q = read();
  for (int i = 1; i <= Q; ++i)
    q[i].v = read(), q[i].d = read(), q[i].c = read();
  for (int i = Q; i >= 1; --i)
    dfs(q[i].v, q[i].d, q[i].c);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i][0]);
  return 0;
}
// 感谢ZJH大佬提供思路

Fixed submission:
#include <bits/stdc++.h> //这题是倒着染色吧
using namespace std;

const int N = 1e5 + 10;
int n, m;
struct edge {
  int y, nxt;
} e[N << 2];
int lin[N], len;
int ans[N][20];
struct QUE {
  int v, d, c;
} q[N];

inline int read() {
  int s = 0, f = 1;
  char ch;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    s = (s << 1) + (s << 3) + ch - '0';
  return s * f;
}

inline void insert(int xx, int yy) {
  e[++len] = (edge){yy, lin[xx]};
  lin[xx] = len;
}

inline void dfs(int v, int d, int c) {
  if (d == -1)
    return;
  if (ans[v][d])
    return; // 因为是倒着染，所以不用再染一遍
  ans[v][d] = c;
  for (int i = lin[v]; i; i = e[i].nxt)
    dfs(e[i].y, d - 1, c);
}

int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read();
    insert(x, y);
    insert(y, x);
  }
  for (int i = 1; i <= n; ++i)
    insert(i, i);
  int Q = read();
  for (int i = 1; i <= Q; ++i)
    q[i].v = read(), q[i].d = read(), q[i].c = read();
  for (int i = Q; i >= 1; --i)
    dfs(q[i].v, q[i].d, q[i].c);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i][0]);
  return 0;
}
// 感谢ZJH大佬提供思路
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

vector<vector<int>> memo;
vector<vector<int>> v;

void f(int d, int vv, int c) {
  if (d < 0 || memo[d][vv] != 0)
    return;
  memo[d][vv] = c;
  rep(i, d) f(i, vv, c);
  rep(i, v[vv].size()) f(d - 1, v[vv][i], c);

  return;
}

int main(void) {
  int n, mm;
  cin >> n >> mm;
  v.resize(n);
  memo.assign(10, vector<int>(n, 0));
  rep(i, mm) {
    int ta, tb;
    scanf("%d%d", &ta, &tb);
    ta--;
    tb--;
    v[ta].push_back(tb);
    v[tb].push_back(ta);
  }
  int qq;
  cin >> qq;
  vector<int> qv(qq), qd(qq), qc(qq);
  rep(i, qq) scanf("%d%d%d", &qv[i], &qd[i], &qc[i]), qv[i]--;
  irep(i, qq) f(qd[i], qv[i], qc[i]);
  rep(i, n) cout << memo[0][i] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

vector<vector<int>> memo;
vector<vector<int>> v;

void f(int d, int vv, int c) {
  if (d < 0 || memo[d][vv] != 0)
    return;
  memo[d][vv] = c;
  rep(i, d) f(i, vv, c);
  rep(i, v[vv].size()) f(d - 1, v[vv][i], c);

  return;
}

int main(void) {
  int n, mm;
  cin >> n >> mm;
  v.resize(n);
  memo.assign(11, vector<int>(n, 0));
  rep(i, mm) {
    int ta, tb;
    scanf("%d%d", &ta, &tb);
    ta--;
    tb--;
    v[ta].push_back(tb);
    v[tb].push_back(ta);
  }
  int qq;
  cin >> qq;
  vector<int> qv(qq), qd(qq), qc(qq);
  rep(i, qq) scanf("%d%d%d", &qv[i], &qd[i], &qc[i]), qv[i]--;
  irep(i, qq) f(qd[i], qv[i], qc[i]);
  rep(i, n) cout << memo[0][i] << endl;

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll n, m, q;
vector<ll> G[20000];
bool dp[200000][11];
ll color[200000];

void query(ll v, ll d, ll c) {
  if (dp[v][d])
    return;

  REP(i, d + 1)
  dp[v][i] = true;

  if (color[v] == 0) {
    color[v] = c;
  }

  if (d == 0)
    return;

  REP(i, G[v].size()) { query(G[v][i], d - 1, c); }
}

ll v[200000], d[200000], c[200000];

int main() {
  cin >> n >> m;
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;

    G[a].pb(b);
    G[b].pb(a);
  }
  cin >> q;

  REP(i, q) { cin >> v[i] >> d[i] >> c[i]; }

  RREP(i, q) { query(v[i] - 1, d[i], c[i]); }

  REP(i, n) { cout << color[i] << endl; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll n, m, q;
vector<ll> G[200000];
bool dp[200000][11];
ll color[200000];

void query(ll v, ll d, ll c) {
  if (dp[v][d])
    return;

  REP(i, d + 1)
  dp[v][i] = true;

  if (color[v] == 0) {
    color[v] = c;
  }

  if (d == 0)
    return;

  REP(i, G[v].size()) { query(G[v][i], d - 1, c); }
}

ll v[200000], d[200000], c[200000];

int main() {
  cin >> n >> m;
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;

    G[a].pb(b);
    G[b].pb(a);
  }
  cin >> q;

  REP(i, q) { cin >> v[i] >> d[i] >> c[i]; }

  RREP(i, q) { query(v[i] - 1, d[i], c[i]); }

  REP(i, n) { cout << color[i] << endl; }
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
#include <bits/stdc++.h>
#define N 100005
#define int long long
// #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1 ==
// p2)?EOF:*p1++)
using namespace std;
// char buf[(1 << 22)] , *p1 = buf , *p2 = buf ;
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, m, ver[N << 1], nxt[N << 1], hed[N], tot, ans[N], Dis[N];
struct as {
  int v, d, c;
} a[N];
void add(int x, int y) {
  ver[++tot] = y;
  nxt[tot] = hed[x];
  hed[x] = tot;
}
void dfs(int x, int col, int dis) {
  if (!ans[x])
    ans[x] = col;
  if (!dis)
    return;
  Dis[x] = dis;
  for (int i = hed[x]; i; i = nxt[i]) {
    int y = ver[i];
    dfs(y, col, dis - 1);
  }
}
signed main() {
  n = read(), m = read();
  for (int i = 1, u, v; i <= m;
       u = read(), v = read(), add(u, v), add(v, u), i++)
    ;
  int q = read();
  for (int i = 1; i <= q; i++)
    a[i].v = read(), a[i].d = read(), a[i].c = read();
  for (int i = q; i >= 1; i--)
    dfs(a[i].v, a[i].c, a[i].d);
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
}
/*
7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2
*/

Fixed submission:
// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
#include <bits/stdc++.h>
#define N 100005
#define int long long
// #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1 ==
// p2)?EOF:*p1++)
using namespace std;
// char buf[(1 << 22)] , *p1 = buf , *p2 = buf ;
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, m, ver[N << 1], nxt[N << 1], hed[N], tot, ans[N], Dis[N];
struct as {
  int v, d, c;
} a[N];
void add(int x, int y) {
  ver[++tot] = y;
  nxt[tot] = hed[x];
  hed[x] = tot;
}
void dfs(int x, int col, int dis) {
  if (!ans[x])
    ans[x] = col;
  if (!dis || Dis[x] >= dis)
    return;
  Dis[x] = dis;
  for (int i = hed[x]; i; i = nxt[i]) {
    int y = ver[i];
    dfs(y, col, dis - 1);
  }
}
signed main() {
  n = read(), m = read();
  for (int i = 1, u, v; i <= m;
       u = read(), v = read(), add(u, v), add(v, u), i++)
    ;
  int q = read();
  for (int i = 1; i <= q; i++)
    a[i].v = read(), a[i].d = read(), a[i].c = read();
  for (int i = q; i >= 1; i--)
    dfs(a[i].v, a[i].c, a[i].d);
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
}
/*
7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2
*/