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



using namespace std;

const int N = 2e5;



int n, q, m;

int vis[N][20], color[N], v[N], d[N], c[N];

vector < int > a[N];



int read(void)

{

	int s = 0, w = 0; char c = getchar();

	while (c < '0' || c > '9') w |= c == '-', c = getchar();

	while (c >= '0' && c <= '9') s = s*10+c-48, c = getchar();

	return w ? -s : s;

}



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

{

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

	vis[v][d] = 1;

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

	if (d == 0) return;

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

	{

		int y = a[v][i];

		dfs(y,d-1,c);	

	} 

	return;

}



int main(void)

{

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

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

	{

		x = read(), y = read();

		a[x].push_back(y);

		a[y].push_back(x);

	}

	int q = read();

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

		v[i] = read();

		d[i] = read();

		c[i] = read();

	}

	while (q) dfs(v[q],d[q],c[q]), q --;

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

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

	return 0;

}

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

using namespace std;

#define C getchar()

inline int read() {

	int num = 0;

	char c = C;

	for(;c<'0' || c>'9';c=C);

	for(;c>='0' && c<='9';c=C) num = (num << 1) + (num << 3) + (c ^ 48);

	return num;

}



//int nxt[200010],head[100010],to[200010],tot;

//

//inline void add(int x,int y) {

//	nxt[++tot] = head[x],head[x] = tot,to[tot] = y;

//}



int vis[100010][12];



vector<int> edge[100010];



inline void putc(int x,int d,int cl) {

	if (d == -1) return ;

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

	vis[x][d] = cl;

	for(int i=0;i<edge[x].size();++i) putc(edge[x][i] , d - 1 , cl);

}



int n,m,Q;

struct node {

	int rt,d,cl;

}a[100010];



int main() {

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

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

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

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

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

		edge[u].push_back(v);

		edge[v].push_back(u);

//		add(u , v);add(v , u);

	}

//	for(int i=1;i<=n;++i) add(i , i);

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

	

	Q = read();

	for(int i=1;i<=Q;++i) a[i].rt = read(),a[i].d = read(),a[i].cl = read();

	for(int i=Q;i>=1;--i) putc(a[i].rt , a[i].d , a[i].cl);

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

	return 0;

}

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

#include <bits/stdc++.h>

#define LL long long

#define pb push_back

#define mp make_pair

#define INF 0x3f3f3f3f

#define Inf 1000000000000000000LL

#define rep(i,x) for(int (i)=1;(i)<=(x);(i)++)

#define repp(i,x,y) for(int (i)=(x);(i)<=(y);(i)++)

#define per(i,x,y) for(int (i)=(x);(i)>=(y);(i)--)

#define forn(i,x) for(int (i)=0;(i)<(x);(i)++)

#define foreach(i,x) for(auto i:x)

using namespace std;

typedef pair<int,int>pii;

int n,m,q;

vector<int>G[100010];

int v[100010],d[100010],c[100010],col[100010];

int now;

int used[100010];

inline void read(int &x){

    int f=1;x=0;char s=getchar();

    while(s<'0'||s>'9'){s=getchar();}

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

}

inline void dfs(int x,int d){

	if(used[x]>=d)return;

	used[x]=d;

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

	foreach(to,G[x])if(d)dfs(to,d-1);

}

int main(){

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

	read(n),read(m);

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

		read(x),read(y);

		G[x].pb(y),G[y].pb(x);

	}

	read(q);

	rep(i,q)read(v[i]),read(d[i]),read(c[i]);

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

		now=c[i];

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

		// rep(i,n)cout<<col[i]<<' ';

		// puts("");

	}

	rep(i,n)cout<<col[i]<<endl;

	return 0;

}

/*

7 7

1 2

1 3

1 4

4 5

5 6

5 7

2 3

2

6 1 1

1 2 2

*/

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

#define LL long long

#define pb push_back

#define mp make_pair

#define INF 0x3f3f3f3f

#define Inf 1000000000000000000LL

#define rep(i,x) for(int (i)=1;(i)<=(x);(i)++)

#define repp(i,x,y) for(int (i)=(x);(i)<=(y);(i)++)

#define forn(i,x) for(int (i)=0;(i)<(x);(i)++)

#define foreach(i,x) for(auto (i):(x))

using namespace std;

typedef pair<int,int>pii;

int n,m,q;

int c[100010][20];

int co[100010];

vector<int>G[100010];

int main(){

	cin>>n>>m;

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

		int x,y;

		cin>>x>>y;

		x--,y--;

		G[x].pb(y);

		G[y].pb(x);

	}

	cin>>q;

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

		int v,d;

		cin>>v>>d>>co[i+1];

		v--;

		c[v][d]=i+1;

	}

	for(int i=11-2;i>=0;i--)

		for(int v=0;v<n;v++)

			for(int u:G[v]){

				c[u][i]=max(c[u][i],c[v][i+1]);

			}

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

		int mx=0;

		for(int j=0;j<11;j++)mx=max(mx,c[i][j]);

		cout<<co[mx]<<endl;

	}

	return 0;

}


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

using namespace std;

vector<int> a[100002];

int n,m,q,ans[100002],maindis[100002],v[100002],d[100002],c[100002];

void dfs(int k,int cc,int to){

	if(k<0){

		return;

	}

	if(k<=maindis[to]){

		return;

	}

	if(ans[to]==0){

		ans[to]=cc;

	}

	maindis[to]=k;

	for(int i=0;i<a[to].size();i++){

		dfs(k-1,cc,a[to][i]);

	}

}

int main(){

	cin>>n>>m;

	fill(maindis,maindis+100000,-1);

	memset(ans,0,sizeof(ans));

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

		int t1,t2;

		cin>>t1>>t2;

		a[t1].push_back(t2);

		a[t2].push_back(t1);

	}

	cin>>q;

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

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

	}

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

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

	}

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

		cout<<ans[i]<<endl;

	}

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

//		cout<<maindis[i]<<" ";

//	}

	return 0;

}

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

using namespace std ;

#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )

#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )

#define re register

int gi() {

	char cc = getchar() ; int cn = 0, flus = 1 ;

	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }

	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;

	return cn * flus ;

}

const int N = 1e5 + 5 ; 

const int M = 10 + 5 ; 

int n, m, cnt, dp[N][M], head[N], col[N] ; 

struct Q {

	int v, d, c ; 

} q[N] ;

struct E {

	int to, next ; 

} e[N << 1] ;

void add( int x, int y ) {

	e[++ cnt] = (E){ y, head[x] }, head[x] = cnt,

	e[++ cnt] = (E){ x, head[y] }, head[y] = cnt ; 

}

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

	if( d < 0 ) return ;

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

	if( dp[x][d] ) return ;

	dp[x][d] = 1 ; 

	Next( i, x ) {

		int v = e[i].to ; 

		dfs( v, d - 1, c ) ;

	}

}

signed main()

{

	n = gi(), m = gi() ; int u, v ;

	rep( i, 1, m ) u = gi(), v = gi(), add( u, v ) ;

	int T = gi() ;

	rep( i, 1, T ) q[i].v = gi(), q[i].d = gi(), q[i].c = gi() ;

	for( re int i = T; i; -- i ) {

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

	}

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

	return 0 ;

}

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



#define ll long long

#define ull unsigned long long

#define inf 0x3f3f3f3f

#define met(a, x) memset(a,x,sizeof(a))

#define mp make_pair;



using namespace std;

const int mod = 19e+7;

const int N = 1e5 + 10;

const int M = 1e6 + 10;

int m,n;

struct rec{

    int y,nex;

}edge[2*N];

int head[N],q1[N],q2[N],q3[N];

int val[N],maxd[N];

int cnt=0;

void add(int u,int v){

    edge[++cnt].y=v;

    edge[cnt].nex=head[u];

    head[u]=cnt;

}

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

    if(maxd[u]>=d)

        return ;

    if(!val[u])

        val[u]=x;

    if(d==0)

        return;

    maxd[u]=d;

    for(int i=head[u];i;i=edge[i].nex){

        dfs(edge[i].y,d-1,x);

    }

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    cin>>n>>m;

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

        int a,b;

        cin>>a>>b;

        add(a,b);

        add(b,a);

    }

    int q;

    cin>>q;

    met(maxd,-1);

    met(val,0);

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

        cin>>q1[i]>>q2[i]>>q3[i];

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

        dfs(q1[i],q2[i],q3[i]);

    }

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

        cout<<val[i]<<endl;

    }

    return 0;

}

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

using namespace std ;

#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )

#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )

#define re register

int gi() {

	char cc = getchar() ; int cn = 0, flus = 1 ;

	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }

	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;

	return cn * flus ;

}

const int N = 1e5 + 5 ; 

const int M = 10 + 5 ; 

int n, m, cnt, dp[N][M], head[N], col[N] ; 

struct Q {

	int v, d, c ; 

} q[N] ;

struct E {

	int to, next ; 

} e[N << 1] ;

void add( int x, int y ) {

	e[++ cnt] = (E){ y, head[x] }, head[x] = cnt,

	e[++ cnt] = (E){ x, head[y] }, head[y] = cnt ; 

}

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

	if( d < 0 ) return ;

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

	if( dp[x][d] ) return ;

	dp[x][d] = 1 ; 

	Next( i, x ) {

		int v = e[i].to ; 

		dfs( v, d - 1, c ) ;

	}

}

signed main()

{

	n = gi(), m = gi() ; int u, v ;

	rep( i, 1, m ) u = gi(), v = gi(), add( u, v ) ;

	int T = gi() ;

	rep( i, 1, T ) q[i].v = gi(), q[i].d = gi(), q[i].c = gi() ;

	for( re int i = T; i; -- i ) {

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

	}

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

	return 0 ;

}

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

#include <cstring>

#include <cstdio>

using namespace std;



int head[100005], tot, n, m, q, v[100005], d[100005], c[100005], ou[100005], maxd[100005];

struct edge {

	int to, nxt;

} G[300005];



inline void add(int u, int v) {

	tot++;

	G[tot].to = v;

	G[tot].nxt = head[u];

	head[u] = tot;

}



void dfs(int un, int ds, int c) {

	if(maxd[un] >= ds) return;

	if(!ou[un]) ou[un] = c;

	if(!ds) return;

	maxd[un] = ds;

	for (int i = head[un]; i; i = G[i].nxt) dfs(G[i].to, ds - 1, c);

}



int main() {

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

	int u, vd;

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

		scanf ("%d %d", &u, &vd);

		add(u, vd); add(vd, u);

	}

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

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

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

	return 0;

}

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

using namespace std ;

#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )

#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )

#define re register

int gi() {

	char cc = getchar() ; int cn = 0, flus = 1 ;

	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }

	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;

	return cn * flus ;

}

const int N = 1e5 + 5 ; 

const int M = 10 + 5 ; 

int n, m, cnt, dp[N][M], head[N], col[N] ; 

struct Q {

	int v, d, c ; 

} q[N] ;

struct E {

	int to, next ; 

} e[N << 1] ;

void add( int x, int y ) {

	e[++ cnt] = (E){ y, head[x] }, head[x] = cnt,

	e[++ cnt] = (E){ x, head[y] }, head[y] = cnt ; 

}

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

	if( d < 0 ) return ;

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

	if( dp[x][d] ) return ;

	dp[x][d] = 1 ; 

	Next( i, x ) {

		int v = e[i].to ; 

		dfs( v, d - 1, c ) ;

	}

}

signed main()

{

	n = gi(), m = gi() ; int u, v ;

	rep( i, 1, m ) u = gi(), v = gi(), add( u, v ) ;

	int T = gi() ;

	rep( i, 1, T ) q[i].v = gi(), q[i].d = gi(), q[i].c = gi() ;

	for( re int i = T; i; -- i ) {

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

	}

	rep( i, 1, n ) 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>

using namespace std ;

#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )

#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )

#define re register

int gi() {

	char cc = getchar() ; int cn = 0, flus = 1 ;

	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }

	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;

	return cn * flus ;

}

const int N = 1e5 + 5 ; 

const int M = 10 + 5 ; 

int n, m, cnt, dp[N][M], head[N], col[N] ; 

struct Q {

	int v, d, c ; 

} q[N] ;

struct E {

	int to, next ; 

} e[N << 1] ;

void add( int x, int y ) {

	e[++ cnt] = (E){ y, head[x] }, head[x] = cnt,

	e[++ cnt] = (E){ x, head[y] }, head[y] = cnt ; 

}

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

	if( d < 0 ) return ;

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

	if( dp[x][d] ) return ;

	dp[x][d] = 1 ; 

	Next( i, x ) {

		int v = e[i].to ; 

		dfs( v, d - 1, c ) ;

	}

}

signed main()

{

	n = gi(), m = gi() ; int u, v ;

	rep( i, 1, m ) u = gi(), v = gi(), add( u, v ) ;

	int T = gi() ;

	rep( i, 1, T ) q[i].v = gi(), q[i].d = gi(), q[i].c = gi() ;

	for( re int i = T; i; -- i ) {

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

	}

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

	return 0 ;

}

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

    using namespace std;

    int ans[100003],n,m,q,do1[100003],do2[100003],do3[100003];

    vector<int>v[100005];

    int vis[100003];

    void dfs(int a,int b,int c){

    	if(b==-1)return;

    	if(b<=vis[a])return;

    	vis[a]=b;

    	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<=100002;i++)vis[i]=-1;

    	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;

    }

// Optimized solution to the same problem (faster):
//**************************

//*** writer  :  Alex Hu **

//************************



#include<bits/stdc++.h>



using namespace std;



#define CLEAN(a,b) memset(a,b,sizeof(a))

#define mkp make_pair

#define pb push_back

#define rept(i,a,b) for(int i=(int)(a);i<(int)(b);i++)



typedef long long ll;

typedef pair<int, int> pii;

typedef vector<int> vi;



const int maxn=1e5+10;



int n,m,cnt,tot,fst[maxn],pnt[maxn<<1],nxt[maxn<<1],ans[maxn],dp[maxn][11];



void add(int x,int y)

{

	pnt[++tot]=y;

	nxt[tot]=fst[x];

	fst[x]=tot;

} 



int main()

{

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

	int x,y;

	rept(i,1,m+1)

	{

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

		add(x,y);

		add(y,x);

	}

	scanf("%d",&cnt);

	rept(i,1,cnt+1)

	{

		scanf("%d%d%d",&x,&y,&ans[i]);

		dp[x][y]=i;

	}

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

	{

		rept(x,1,n+1)

		{

			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]);

		}

	}

	rept(i,1,n+1) printf("%d\n",ans[dp[i][0]]);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//**************************

//*** writer  :  Alex Hu **

//************************



#include<bits/stdc++.h>



using namespace std;



#define CLEAN(a,b) memset(a,b,sizeof(a))

#define mkp make_pair

#define pb push_back

#define rept(i,a,b) for(int i=(int)(a);i<(int)(b);i++)



typedef long long ll;

typedef pair<int, int> pii;

typedef vector<int> vi;

 

const int maxn=100005,maxd=11;

int c[maxn][maxd];

int col[maxn];

vector<int> e[maxn];

 

int main(){

 

	int n,m,q;

	cin>>n>>m;

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

	}

	cin>>q;

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

		int v,d;

		cin>>v>>d>>col[i+1];

		--v;

		c[v][d]=i+1;

	}

	for(int i=maxd-2;i>=0;i--){

		for(int v=0;v<n;v++){

			for(int u=0;u<e[v].size();u++){

				c[e[v][u]][i]=max(c[e[v][u]][i],c[v][i+1]);

			}

		}

	}

	for(int v=0;v<n;v++){

		int res=0;

		for(int d=0;d<maxd;d++){

			res=max(res,c[v][d]);

		}

		cout<<col[res]<<endl;

	}

	return 0;

}

// Optimized solution to the same problem (faster):
//**************************

//*** writer  :  Alex Hu **

//************************



#include<bits/stdc++.h>



using namespace std;



#define CLEAN(a,b) memset(a,b,sizeof(a))

#define mkp make_pair

#define pb push_back

#define rept(i,a,b) for(int i=(int)(a);i<(int)(b);i++)



typedef long long ll;

typedef pair<int, int> pii;

typedef vector<int> vi;



const int maxn=1e5+10;



int n,m,cnt,tot,fst[maxn],pnt[maxn<<1],nxt[maxn<<1],ans[maxn],dp[maxn][11];



void add(int x,int y)

{

	pnt[++tot]=y;

	nxt[tot]=fst[x];

	fst[x]=tot;

} 



int main()

{

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

	int x,y;

	rept(i,1,m+1)

	{

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

		add(x,y);

		add(y,x);

	}

	scanf("%d",&cnt);

	rept(i,1,cnt+1)

	{

		scanf("%d%d%d",&x,&y,&ans[i]);

		dp[x][y]=i;

	}

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

	{

		rept(x,1,n+1)

		{

			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]);

		}

	}

	rept(i,1,n+1) printf("%d\n",ans[dp[i][0]]);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/******************

*  Author: BD747  *

******************/

#include<algorithm>

#include<cstdio>

#include<iostream>

#include<cstring>

#include<cmath>

#include<map>

#include<queue>

#include<set>

#include<sstream>

#include<stack>

#include<string>

#include<vector>

using namespace std;



int col[100005];

vector<int> v[100005];

int n,m;

int __d,_col;

int use[100005];

int _v[100005],d[100005],c[100005];

inline void dfs(int x,int d){

	if(use[x]>=d) return;

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

	use[x]=d;

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

		int e=v[x][i];

		if(d) dfs(e,d-1);

	}

}



int main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	

	int i,j,q;

	cin>>n>>m;

    int x,y,z;

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

    	cin>>x>>y;

    	v[x].push_back(y);

    	v[y].push_back(x);

	}

	cin>>q;

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

	for(j=1;j<=q;++j) cin>>_v[j]>>d[j]>>c[j];

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

		_col=c[i];

		dfs(_v[i],d[i]);

	}

	for(i=1;i<=n;++i) cout<<col[i]<<endl;

	

	return 0;

}


// Optimized solution to the same problem (faster):
/******************

*  Author: BD747  *

******************/

//Endagorion

#include<algorithm>

#include<cstdio>

#include<iostream>

#include<cstring>

#include<cmath>

#include<map>

#include<queue>

#include<set>

#include<sstream>

#include<stack>

#include<string>

#include<vector>

#include<bits/stdc++.h>

using namespace std;



const int maxn=100005,maxd=11;

int c[maxn][maxd];

int col[maxn];

vector<int> e[maxn];



int main(){



	int n,m,q;

	cin>>n>>m;

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

	}

	cin>>q;

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

		int v,d;

		cin>>v>>d>>col[i+1];

		--v;

		c[v][d]=i+1;

	}

	for(int i=maxd-2;i>=0;i--){

		for(int v=0;v<n;v++){

			for(int u=0;u<e[v].size();u++){

				c[e[v][u]][i]=max(c[e[v][u]][i],c[v][i+1]);

			}

		}

	}

	

	for(int v=0;v<n;v++){

		int res=0;

		for(int d=0;d<maxd;d++){

			res=max(res,c[v][d]);

		}

		cout<<col[res]<<endl;

	}



	return 0;

}


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

using namespace std;



#define iii pair<pair<int,int>,int> 

#define FIR first.first

#define SEC first.second

#define THI second



int N,M;

vector<int> e[100010];

int Q;

iii q[100010];



int g[100010];

int maxd[100010];



void input()

{

	scanf("%d%d",&N,&M);

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

	{

		maxd[i]=-1;

	}

	for(int i=1,tmp1,tmp2;i<=M;i++)

	{

		scanf("%d%d",&tmp1,&tmp2);

		e[tmp1].push_back(tmp2);

		e[tmp2].push_back(tmp1);

	}

	scanf("%d",&Q);

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

	{

		scanf("%d%d%d",&q[i].FIR,&q[i].SEC,&q[i].THI);

	}

}



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

{

	if(d<=maxd[v])

	{

		return;

	}

	maxd[v]=d;

	

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

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

	{

		f(e[v][i],d-1,c);

	}

}



void work()

{

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

	{

		f(q[i].FIR,q[i].SEC,q[i].THI);

	}

}



void output()

{

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

	{

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

	}

}



int main()

{

	input();

	work();

	output();

	return 0;

}


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

using namespace std;

const int N = 1e5+20;

struct edge { int ver,next; } e[N<<1];

int n,m,q,t,Head[N],ans[N],v[N],d[N],c[N],dis[N];

inline void insert(int x,int y) { e[++t] = (edge){y,Head[x]} , Head[x] = t; }

inline void input(void)

{

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

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

    {

        int u,v;

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

        insert( u , v );

        insert( v , u );

    }

    scanf("%d",&q);

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

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

}

inline void dfs(int x,int d,int col)

{

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

    if ( !d || dis[x] >= d ) return void();

    dis[x] = d;

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

    {

        int y = e[i].ver;

        dfs( y , d-1 , col );

    }

}

int main(void)

{

    input();

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

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

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

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

    return 0;

}


// Fastest known solution for this problem: