stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<iostream>

#include<iomanip>

#include<algorithm>

#include<cstring>

#include<string>

#include<cmath>

#include<vector>

#include<queue>

#include<stack>

#include<map>

#include<set>

using namespace std;

const int inf=2147483647,dx[]={-1,0,1,0},dy[]={0,-1,0,1};

const long long llinf=9223372036854775807ll;

const int N=100005;

int n,m,q,tot,fst[N],pnt[N*2],nxt[N*2],ans[N],dp[N][11],a,b;

int main(){

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>a>>b;

		pnt[++tot]=b;

		nxt[tot]=fst[a];

		fst[a]=tot;

		pnt[++tot]=a;

		nxt[tot]=fst[b];

		fst[b]=tot;

	}

	cin>>q;

	for(int i=1;i<=q;i++)

		cin>>a>>b>>ans[i],dp[a][b]=i;

	for(int i=9;i>=0;i--)

		for(int x=1;x<=n;x++)

		{

			dp[x][i]=max(dp[x][i],dp[x][i+1]);

			for(int j=fst[x];j;j=nxt[j])

				dp[x][i]=max(dp[x][i],dp[pnt[j]][i+1]);

		}

	for(int i=1;i<=n;i++)

		cout<<ans[dp[i][0]]<<'\n';

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

const int maxn = 100000;

int n, m, qu;

struct node

{

	int nxt;

	int to;

}edge[2 * maxn + 10];

int head[maxn + 10];

int top;

void add_e(const int &u, const int &v)

{

	++top;

	edge[top].nxt = head[u];

	edge[top].to = v;

	head[u] = top;

	return ;

}



int ques[maxn + 10][3];

bool ok[maxn + 10][20];

int q[maxn + 10][3];

int he, ta;

int color[maxn + 10];



void bfs(const int &p)

{

	++ta;

	q[ta][0] = ques[p][0];

	q[ta][1] = ques[p][1];

	q[ta][2] = ques[p][2];

	while(he != ta)

	{

		++he;

		if(ok[q[he][0]][q[he][1]])

			continue;

		if(color[q[he][0]] == 0)

			color[q[he][0]] = q[he][2];

		for(int i = 0; i <= q[he][1]; ++i)

		{

			ok[q[he][0]][i] = 1;

		}

		if(q[he][1] == 0)

			continue;

		for(int i = head[q[he][0]]; i; i = edge[i].nxt)

		{

			if(ok[edge[i].to][q[he][1] - 1])

				continue;

			++ta;

			q[ta][0] = edge[i].to;

			q[ta][1] = q[he][1] - 1;

			q[ta][2] = q[he][2];

			

		}

	}

	return ;

}



int main()

{

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

	int a, b;

	for(int i = 1; i <= m; ++i)

	{

		scanf("%d%d", &a, &b);

		add_e(a, b);

		add_e(b, a);

	}

	scanf("%d", &qu);

	for(int i = 1; i <= qu; ++i)

	{

		scanf("%d%d%d", &ques[i][0], &ques[i][1], &ques[i][2]);

	}

	for(int i = qu; i > 0; --i)

	{

		he = 0;

		ta = 0;

		bfs(i);

	}

	for(int i = 1; i <= n; ++i)

		printf("%d\n", color[i]);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define regi register int

int n,m,q;

int pos[200001],d[200001],c[200001];

int color[200001][20];

std::vector<int>e[300001];

inline int read(){int r=0,c=0,w;for(;!isdigit(c=getchar());r=c);for(w=c^48;isdigit(c=getchar());w=w*10+(c^48));return r^45?w:-w;}

void dfs(int x, int d, int c){

	if(color[x][d]||d==-1)

	  return;

	color[x][d]=c;

	for(regi i=0;i<e[x].size();++i) {

		regi y=e[x][i];

		dfs(y,d-1,c);

	}

	dfs(x,d-1,c);

}

main(){

  n=read();

  m=read();

	for(regi i=1,x,y;i<=m;++i){

		x=read();

		y=read();

		e[x].push_back(y);

		e[y].push_back(x);

	}

	q=read();

	for(regi i=1;i<=q;++i){

		pos[i]=read();

		d[i]=read();

		c[i]=read();

	}

	for(regi i=q;i;--i)

		dfs(pos[i],d[i],c[i]);

	for(regi i=1;i<=n;++i)

		printf("%d\n",color[i][0]);

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define sc(x) scanf("%d", &x)

#define pr(x) printf("%d\n", x)

#define lowbit(x) (x & -x)

#define ll long long

#define N 100010

using namespace std;

int n, m, col[N], tot = 0, head[N], maxx[N], q;

struct Xiao

{

	int next, to;

}e[N * 2];

struct Wang

{

	int v, d, c;

}qvq[N];

inline void add(int x, int y)

{

	e[++tot].next = head[x];

	e[tot].to = y;

	head[x] = tot;

}

inline void dfs(int d, int sc, int x, int fa)

{

	if (d <= maxx[x]) return;

	maxx[x] = d;

	if (col[x] == 0) col[x] = sc;

	for (int i = head[x]; i; i = e[i].next)

	{

		int y = e[i].to;

		if (y == fa) continue;

		dfs(d - 1, sc, y, x);

	}

}

int main()

{

	sc(n);

	sc(m);

	for (int i = 1; i <= m; i++)

	{

		int a, b;

		sc(a);

		sc(b);

		add(a, b);

		add(b, a);

	}

	sc(q);

	for (int i = 1; i <= q; i++)

	{

		sc(qvq[i].v);

		sc(qvq[i].d);

		sc(qvq[i].c);

	}

	memset(maxx, -1, sizeof maxx);

	for (int i = q; i >= 1; i--)

	dfs(qvq[i].d, qvq[i].c, qvq[i].v, 0);

	for (int i = 1; i <= n; i++)

	printf("%d\n", col[i]);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



const int N = 300001;

int n, m, tot = 0, q;

int head[N], Next[N], ver[N], num[N];

int ans[N][20] = {};

struct edge{

	int x, y, z;

}a[N];



vector <int> vis[N];



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

	if (y == -1) return;

	if (ans[x][y]) return;

	ans[x][y] = z;

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

}



int main() {

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

	for (int i=1;i<=m;i++) {

		int x, y;

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

		vis[x].push_back(y);

		vis[y].push_back(x);

	}

	for (int i=1;i<=n;i++) vis[i].push_back(i);

	scanf("%d",&q);

	for (int i=1;i<=q;i++) {

		int x, y, z;

		scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);

	}

	for (int i=q;i;--i) dfs(a[i].x, a[i].y, a[i].z);

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define sc(x) scanf("%d", &x)

#define pr(x) printf("%d\n", x)

#define lowbit(x) (x & -x)

#define ll long long

#define N 100010

using namespace std;

int n, m, col[N], tot = 0, head[N], maxx[N], q;

struct Xiao

{

	int next, to;

}e[N * 2];

struct Wang

{

	int v, d, c;

}qvq[N];

inline void add(int x, int y)

{

	e[++tot].next = head[x];

	e[tot].to = y;

	head[x] = tot;

}

inline void dfs(int d, int sc, int x, int fa)

{

	if (d <= maxx[x]) return;

	maxx[x] = d;

	if (col[x] == 0) col[x] = sc;

	for (int i = head[x]; i; i = e[i].next)

	{

		int y = e[i].to;

		if (y == fa) continue;

		dfs(d - 1, sc, y, x);

	}

}

int main()

{

	sc(n);

	sc(m);

	for (int i = 1; i <= m; i++)

	{

		int a, b;

		sc(a);

		sc(b);

		add(a, b);

		add(b, a);

	}

	sc(q);

	for (int i = 1; i <= q; i++)

	{

		sc(qvq[i].v);

		sc(qvq[i].d);

		sc(qvq[i].c);

	}

	memset(maxx, -1, sizeof maxx);

	for (int i = q; i >= 1; i--)

	dfs(qvq[i].d, qvq[i].c, qvq[i].v, 0);

	for (int i = 1; i <= n; i++)

	printf("%d\n", col[i]);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <memory.h>

#include <vector>

using namespace std;

const int maxn = 200005;

struct Query{

    int v, d, c;

}q[maxn];

int n, m, Q;

vector<int> G[maxn];

bool vis[maxn][15];

int color[maxn];



void solve(int v, int d, int c){

    if(vis[v][d]) return;

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

    if(d <= 0) return;



    for(int i = 0; i < G[v].size(); i++)

    {

        solve(G[v][i], d-1, c);

    }

    vis[v][d] = 1;



}

int main(){

//freopen("input.txt", "r", stdin);

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

    memset(color, 0, sizeof(color));

    memset(vis, false, sizeof(vis));

    int a, b;

    for(int i = 0; i < m; i++)

    {

        scanf("%d %d", &a, &b);

        G[a].push_back(b);

        G[b].push_back(a);

    }

    scanf("%d", &Q);

    for(int i = 0; i < Q; i++)

    {

        scanf("%d %d %d", &q[i].v, &q[i].d, &q[i].c);

    }

    for(int i = Q-1; i >= 0; i--)

       solve(q[i].v, q[i].d, q[i].c);

    for(int i = 1; i <= n; i++)

        printf("%d\n", color[i]);

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio> 

#define For(x) for (int h=head[x],o=V[h]; h; o=V[h=to[h]])

#define add(u,v) (to[++num]=head[u],head[u]=num,V[num]=v)

int num,head[100005],to[200005],V[200005];

int Q,n,m,v[100005],d[100005],c[100005],cl[100005],rg[100005];



void dfs(int x,int ran,int col){

	if (rg[x]>=ran || ran<=0) return;

	rg[x]=ran;

	if (!cl[x]) cl[x]=col;

	For(x) dfs(o,ran-1,col);

}



int main(){

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

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

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

		add(x,y); add(y,x);

	}

	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; i--) dfs(v[i],d[i]+1,c[i]);

	for (int i=1; i<=n; i++) printf("%d\n",cl[i]);

}



//http://agc012.contest.atcoder.jp/tasks/agc012_b

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <vector>

#include <list>

#include <map>

#include <set>

#include <deque>

#include <stack>

#include <bitset>

#include <algorithm>

#include <functional>

#include <numeric>

#include <utility>

#include <sstream>

#include <iostream>

#include <iomanip>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <ctime>

#include <memory.h>

#include <queue>



#define fr first

#define sc second

#define mk make_pair



using namespace std;



int n, m, color[100001], t[100001], dis[100001];

bool u[100001];

vector<int> vec[100001];

pair<int, int> ar[100001];



int main()

{

    cin >> n >> m;

    for(int i = 0; i < m; i++)

    {

        int a, b;

        scanf("%d%d", &a, &b);

        vec[a].push_back(b);

        vec[b].push_back(a);

    }



    int q;

    cin >> q;

    for(int i = 0; i < q; i++)

        scanf("%d%d%d", &ar[i].fr, &ar[i].sc, &t[i]);



    int v, d, col;

    queue <int> a, b, c;

    for(int i = q - 1; i >= 0; i--)

    {

        a.push(ar[i].fr);

        u[ar[i].fr] = 1;

        b.push(ar[i].sc);

        c.push(t[i]);

        while(!a.empty())

        {

            v = a.front(), d = b.front(), col = c.front();

           // cout << v << " " << d << " " << col << endl;

            u[v] = 0;

            a.pop();

            b.pop();

            c.pop();

            if(color[v] == 0 || dis[v] < d)

            {

                if(color[v] == 0)

                {

                    color[v] = col;

                    dis[v] = d;

                }else

                {

                    dis[v] = d;

                }

                if(d > 0)

                for(int i = 0; i < vec[v].size(); i++)

                {

                    if(color[vec[v][i]] == 0 || dis[vec[v][i]] < d - 1 && u[vec[v][i]] == 0)

                    {

                        u[vec[v][i]] = 1;

                        a.push(vec[v][i]);

                        b.push(d - 1);

                        c.push(col);

                    }

                }

            }

        }

    }



    for(int i = 1; i <= n; i++)

    {

        //cout << color[i] << endl;

        printf("%d\n", color[i]);

    }

    return 0;

}




// Optimized solution to the same problem (faster):
#include <cstdio> 

#define For(x) for (int h=head[x],o=V[h]; h; o=V[h=to[h]])

#define add(u,v) (to[++num]=head[u],head[u]=num,V[num]=v)

int num,head[100005],to[200005],V[200005];

int Q,n,m,v[100005],d[100005],c[100005],cl[100005],rg[100005];



void dfs(int x,int ran,int col){

	if (rg[x]>=ran || ran<=0) return;

	rg[x]=ran;

	if (!cl[x]) cl[x]=col;

	For(x) dfs(o,ran-1,col);

}



int main(){

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

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

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

		add(x,y); add(y,x);

	}

	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; i--) dfs(v[i],d[i]+1,c[i]);

	for (int i=1; i<=n; i++) printf("%d\n",cl[i]);

}



//http://agc012.contest.atcoder.jp/tasks/agc012_b

// Fastest known solution for this problem:
// Original C++ solution (slower):
//#pragma GCC optimize("O3")

#include <bits/stdc++.h>



using namespace std;



typedef long long ll;



const int N = (int) 1e5 + 5;

const int mod = (int) 1e9 + 7;



vector<int> g[N];

int n, m, Q, ans[N], ds[N], u[N];

int q[N * 100], qd[N * 100], qb, qe, t = 1, f[N];



void paint (int c) {

        while (qb < qe) {

                int v = q[qb], d = qd[qb++];

                if (ans[v] != 0 && ds[v] >= d) {

                        continue;

                }

                if (!ans[v]) {

                        ans[v] = c; ds[v] = d;

                }else



                {

                        ds[v] = d;

                }

                if (d) {

                        for (int to : g[v]) {

                                if (u[to] != t) {

                                        u[to] = t;

                                        q[qe] = to;

                                        qd[qe++] = d - 1;

                                }

                        }

                }

        }

        qb = qe = 0; ++t;

}



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



inline void solve () {

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

        for (int i = 1; i <= m; i++) {

                int x, y;

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

                g[x].emplace_back (y);

                g[y].emplace_back (x);

        }

        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--) {

                q[qe] = v[i]; qd[qe++] = d[i];

                u[v[i]] = t;

                if (c[i] != c[i - 1]) {

                        paint (c[i]);

                }

        }

        for (int i = 1; i <= n; i++) {

                printf ("%d\n", ans[i]);

        }

}



int tests = 1;



int main () {

//        freopen (".in", "r", stdin);

//        freopen (".out", "w", stdout);

//        scanf ("%d", &tests);

        while (tests--) {

                solve ();

        }

        return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio> 

#define For(x) for (int h=head[x],o=V[h]; h; o=V[h=to[h]])

#define add(u,v) (to[++num]=head[u],head[u]=num,V[num]=v)

int num,head[100005],to[200005],V[200005];

int Q,n,m,v[100005],d[100005],c[100005],cl[100005],rg[100005];



void dfs(int x,int ran,int col){

	if (rg[x]>=ran || ran<=0) return;

	rg[x]=ran;

	if (!cl[x]) cl[x]=col;

	For(x) dfs(o,ran-1,col);

}



int main(){

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

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

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

		add(x,y); add(y,x);

	}

	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; i--) dfs(v[i],d[i]+1,c[i]);

	for (int i=1; i<=n; i++) printf("%d\n",cl[i]);

}



//http://agc012.contest.atcoder.jp/tasks/agc012_b

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

int t[100100], us[100100][20];

vector <int> g[100100];

void func(int v, int d, int c)

{

    if(us[v][d])

        return;

    us[v][d] = 1;

    if(d == 0)

    {

        t[v] = c;

        return;

    }

    func(v, d - 1, c);

    for(int i = 0; i < g[v].size(); ++i)

        func(g[v][i], d - 1, c);

}



int main()

{

    int n, m, i, x, y, q, z;

    vector <pair <pair <int, int>, int> > v;



    cin >> n >> m;

    for(i = 0; i < m; ++i)

    {

        cin >> x >> y;

        g[x].push_back(y);

        g[y].push_back(x);

    }

    cin >> q;

    for(i = 0; i < q; ++i)

    {

        cin >> x >> y >> z;

        v.push_back(make_pair(make_pair(x, y), z));

    }

    while(!v.empty())

    {

        func(v.back().first.first, v.back().first.second, v.back().second);

        v.pop_back();

    }

    for(i = 1; i <= n; ++i)

        cout << t[i] << endl;



return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio> 

#define For(x) for (int h=head[x],o=V[h]; h; o=V[h=to[h]])

#define add(u,v) (to[++num]=head[u],head[u]=num,V[num]=v)

int num,head[100005],to[200005],V[200005];

int Q,n,m,v[100005],d[100005],c[100005],cl[100005],rg[100005];



void dfs(int x,int ran,int col){

	if (rg[x]>=ran || ran<=0) return;

	rg[x]=ran;

	if (!cl[x]) cl[x]=col;

	For(x) dfs(o,ran-1,col);

}



int main(){

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

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

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

		add(x,y); add(y,x);

	}

	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; i--) dfs(v[i],d[i]+1,c[i]);

	for (int i=1; i<=n; i++) printf("%d\n",cl[i]);

}



//http://agc012.contest.atcoder.jp/tasks/agc012_b

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define INF 0x3f3f3f3f

#define maxn 100100

#define lson root << 1

#define rson root << 1 | 1

#define lent (t[root].r - t[root].l + 1)

#define lenl (t[lson].r - t[lson].l + 1)

#define lenr (t[rson].r - t[rson].l + 1)

#define N 1111

#define eps 1e-6

#define pi acos(-1.0)

#define e exp(1.0)

using namespace std;

const int mod = 1e9 + 7;

typedef long long ll;

typedef unsigned long long ull;

vector<int> g[maxn];

int vis[maxn], col[maxn];

tuple<int, int, int> op[maxn];

void dfs(int v, int d, int c)

{

	if (vis[v] >= d)

		return;

	vis[v] = d;

	if (!col[v])

		col[v] = c;

	if (d)

	{

		int sz = g[v].size();

		for (int i = 0; i < sz; i++)

			dfs(g[v][i], d - 1, c);

	}

}

int main()

{

	int n, m, q;

	while (~scanf("%d%d", &n, &m))

	{

		for (int i = 1; i <= n; i++)

			g[i].clear();

		fill(vis, vis + n + 1, -1);

		fill(col, col + n + 1, 0);

		while (m--)

		{

			int u, v;

			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++)

		{

			int v, d, c;

			scanf("%d%d%d", &v, &d, &c);

			op[i] = tuple<int, int, int>(v, d, c);

		}

		for (int i = q - 1; i >= 0; i--)

		{

			int v, d, c;

			tie(v, d, c) = op[i];

			dfs(v, d, c);

		}

		for (int i = 1; i <= n; i++)

			printf("%d\n", col[i]);

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio> 

#define For(x) for (int h=head[x],o=V[h]; h; o=V[h=to[h]])

#define add(u,v) (to[++num]=head[u],head[u]=num,V[num]=v)

int num,head[100005],to[200005],V[200005];

int Q,n,m,v[100005],d[100005],c[100005],cl[100005],rg[100005];



void dfs(int x,int ran,int col){

	if (rg[x]>=ran || ran<=0) return;

	rg[x]=ran;

	if (!cl[x]) cl[x]=col;

	For(x) dfs(o,ran-1,col);

}



int main(){

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

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

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

		add(x,y); add(y,x);

	}

	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; i--) dfs(v[i],d[i]+1,c[i]);

	for (int i=1; i<=n; i++) printf("%d\n",cl[i]);

}



//http://agc012.contest.atcoder.jp/tasks/agc012_b

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

#include<string.h>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=(int)1e5+10;

int read()

{

    int x=0,f=1;char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

    return x*f;

}

struct node{

	int to,nxt;

}ed[maxn<<1];

int head[maxn],cnt;

void addedge(int u,int v){

	ed[cnt].to=v;

	ed[cnt].nxt=head[u];

	head[u]=cnt++;

}

int st[maxn],d[maxn],c[maxn],mark[maxn],dis[maxn],col[maxn];

vector<int>vx;

int main()

{

	int n,m;

	memset(head,-1,sizeof(head));

	memset(mark,-1,sizeof(mark));

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

	for(int i=1;i<=m;i++){

		int u,v;

		u=read(),v=read();

		addedge(u,v);

		addedge(v,u);

	}

	int q;

	scanf("%d",&q);

	for(int i=1;i<=q;i++){

		st[i]=read();d[i]=read();c[i]=read();

	}

	for(int i=q;i>=1;i--){

		if(d[i]<=mark[st[i]])continue;

		queue<int>q;

		q.push(st[i]);

		while(!q.empty()){

			int u=q.front();q.pop();

			if(dis[u]+mark[u]>=d[i])continue;

			if(col[u]==0)col[u]=c[i];	

			mark[u]=d[i]-dis[u];

			for(int j=head[u];~j;j=ed[j].nxt){

				int v=ed[j].to;

				if(dis[v]==0&&v!=st[i]&&dis[u]<d[i]){

					dis[v]=dis[u]+1;

					vx.push_back(v);

					q.push(v);

				}

			}

		}

		for(int j=0;j<vx.size();j++)dis[vx[j]]=0;

		vx.clear();

	}

	for(int i=1;i<=n;i++)printf("%d\n",col[i]);

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio> 

#define For(x) for (int h=head[x],o=V[h]; h; o=V[h=to[h]])

#define add(u,v) (to[++num]=head[u],head[u]=num,V[num]=v)

int num,head[100005],to[200005],V[200005];

int Q,n,m,v[100005],d[100005],c[100005],cl[100005],rg[100005];



void dfs(int x,int ran,int col){

	if (rg[x]>=ran || ran<=0) return;

	rg[x]=ran;

	if (!cl[x]) cl[x]=col;

	For(x) dfs(o,ran-1,col);

}



int main(){

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

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

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

		add(x,y); add(y,x);

	}

	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; i--) dfs(v[i],d[i]+1,c[i]);

	for (int i=1; i<=n; i++) printf("%d\n",cl[i]);

}



//http://agc012.contest.atcoder.jp/tasks/agc012_b

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

vector<int>q[200000];

int color[200000];

int flag[200000];

int n,m;

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

void dfs(int x,int cnt,int c)

{

   if(!color[x])

       color[x]=c;

   if(flag[x]>=cnt)

       return;

   if(cnt==0)

       return;

   flag[x]=cnt;

   for(int i=0;i<q[x].size();i++)

       dfs(q[x][i],cnt-1,c);

}

int main()

{

    cin>>n>>m;

    for(int i=1;i<=m;i++)

    {

        int x,y;

        cin>>x>>y;

        q[x].push_back(y);

        q[y].push_back(x);

    }

    int q;

    cin>>q;

    for(int i=1;i<=q;i++)

       cin>>v[i]>>d[i]>>c[i];

    for(int i=q;i>=1;i--)

        dfs(v[i],d[i],c[i]);

    for(int i=1;i<=n;i++)

        cout<<color[i]<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio> 

#define For(x) for (int h=head[x],o=V[h]; h; o=V[h=to[h]])

#define add(u,v) (to[++num]=head[u],head[u]=num,V[num]=v)

int num,head[100005],to[200005],V[200005];

int Q,n,m,v[100005],d[100005],c[100005],cl[100005],rg[100005];



void dfs(int x,int ran,int col){

	if (rg[x]>=ran || ran<=0) return;

	rg[x]=ran;

	if (!cl[x]) cl[x]=col;

	For(x) dfs(o,ran-1,col);

}



int main(){

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

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

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

		add(x,y); add(y,x);

	}

	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; i--) dfs(v[i],d[i]+1,c[i]);

	for (int i=1; i<=n; i++) printf("%d\n",cl[i]);

}



//http://agc012.contest.atcoder.jp/tasks/agc012_b

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<algorithm>

#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;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

struct E{int v,next;}g[200005];

int n,m,q,h[100005],v[100005],d[100005],c[100005],color[100005],t,dis[100005];

inline int read(){

    int x=0,f=1;char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}

    return x*f;

}

void ins(int u,int v){g[++t].v=v,g[t].next=h[u],h[u]=t;}

void dfs(int v,int d,int c){

	if(d==0||dis[v]>=d) return;

	dis[v]=d; if(color[v]==0) color[v]=c;

	for(int i=h[v];i;i=g[i].next) dfs(g[i].v,d-1,c);

}

int main(){

	n=read(),m=read();

	for(int i=1;i<=m;++i){

		int u,v;

		u=read(),v=read();

		ins(u,v),ins(v,u);

	}

	q=read();

	for(int i=1;i<=q;++i) v[i]=read(),d[i]=read(),c[i]=read();

	for(int i=q;i>=1;--i) dfs(v[i],d[i]+1,c[i]);

	for(int i=1;i<=n;++i) printf("%d\n",color[i]);

}

// Fastest known solution for this problem: