stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
//

//  main.cpp

//  backToProblemSolving

//

//  Created by Khaled Abdelfattah on 6/27/17.

//  Copyright © 2017 Khaled Abdelfattah. All rights reserved.

//



#include <bits/stdc++.h>

// #include <iostream>

using namespace std;

//#include "stdc++.h"



typedef long long ll;

#define MAX 1000000000

#define BASE1 23

#define BASE2 31

#define MOD1 1000000007

#define MOD2 217645177

#define eps 1e-6



int colors[100005];

vector<int> adjList[100005];

bool visited[100005][15];

void DFS (int node, int d, int c) {

    if (d < 0 || visited[node][d])

        return;

    visited[node][d] = true;

    if (colors[node] == 0)

        colors[node] = c;

    for (int i : adjList[node])

        DFS(i, d - 1, c);

}

int main() {

    int n, m;

    cin >> n >> m;

    memset(colors, 0, n);

    memset(visited, 0, sizeof visited);

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

        int u, v;

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

        v--, u--;

        adjList[u].push_back(v);

        adjList[v].push_back(u);

    }

    int q;

    cin >> q;

    pair<pair<int, int>, int> *quers = new pair<pair<int, int>, int> [q];

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

        int s, d, c;

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

        quers[i] = {{s - 1, d}, c};

    }

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

        DFS(quers[i].first.first, quers[i].first.second, quers[i].second);

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

        cout << colors[i] << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.3304414059793475x):
#include<cstdio>

#include<fstream>

#include<queue>

#include<iostream>

#include<cstring>

#define N 100010

using namespace std;

int n,m;

struct rode{

  int y,next;

}e[N<<1]={};

int fl[N]={},len=0;

int a[N]={};

inline int read()

{

	int s = 0, w = 1;

	char c = getchar();

	while ((c<'0' || c>'9') && c != '0')c = getchar();

	if (c == '-')w = -1, c = getchar();

	while (c <= '9'&&c >= '0')s = (s << 3) + (s << 1) + c - '0', c = getchar();

	return s * w;

}

inline void inc(int x,int y)

{

  e[++len].y=y;

  e[len].next=fl[x];

  fl[x]=len;

}

struct que{

  int u,d,c;

}qu[N]={};

int c[N]={};

bool vis[N]={};

int s=0;

void dfs(int u,int v,int d,int co)

{

  if(d==0)return ;

  for(int i=fl[u];i;i=e[i].next)

   {

     int vv=e[i].y;

     if(vis[vv])continue;

     if(c[vv]==0)c[vv]=co,--s;

     if(s==0)return ;

     vis[vv]=1;

     dfs(vv,v,d-1,co);

     vis[vv]=0;

   }

}

int main()

{

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

  s=n;

  // for(int i=1;i<=n;++i)a[i]=read(),c[i]=0;

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

  {

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

    inc(x,y);inc(y,x);

  }

  int q=read();

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

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

  {

    int u=qu[i].u,d=qu[i].d,co=qu[i].c;

    if(c[u]==0)c[u]=co,--s;

    if(s==0)break;

    vis[u]=1;

    dfs(u,u,d,co);

    vis[u]=0;

  }

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

  return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

int q;

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

vector<int> g[100005];

int colour[100005];

int mindis[100005];

inline void dfs(int x,int dleft,int c)

{

	if(dleft<=mindis[x])

	{

		return;

	}

	mindis[x]=dleft;

	if(colour[x]==0)

	{

		colour[x]=c;

	}

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

	{

		dfs(g[x][i],dleft-1,c);

	}

}

int main()

{

	cin>>n>>m;

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

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

	{

		int a,b;

		cin>>a>>b;

		a--;

		b--;

		g[a].push_back(b);

		g[b].push_back(a);

	}

	cin>>q;

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

	{

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

	}

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

	{

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

	}

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

	{

		cout<<colour[i]<<endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.9955378651829054x):
#include<cstdio>

#include<fstream>

#include<queue>

#include<iostream>

#include<cstring>

#define N 100010

using namespace std;

int n,m;

struct rode{

  int y,next;

}e[N<<1]={};

int fl[N]={},len=0;

int a[N]={};

inline int read()

{

	int s = 0, w = 1;

	char c = getchar();

	while ((c<'0' || c>'9') && c != '0')c = getchar();

	if (c == '-')w = -1, c = getchar();

	while (c <= '9'&&c >= '0')s = (s << 3) + (s << 1) + c - '0', c = getchar();

	return s * w;

}

inline void inc(int x,int y)

{

  e[++len].y=y;

  e[len].next=fl[x];

  fl[x]=len;

}

struct que{

  int u,d,c;

}qu[N]={};

int c[N]={};

bool vis[N]={};

int s=0;

void dfs(int u,int v,int d,int co)

{

  if(d==0)return ;

  for(int i=fl[u];i;i=e[i].next)

   {

     int vv=e[i].y;

     if(vis[vv])continue;

     if(c[vv]==0)c[vv]=co,--s;

     if(s==0)return ;

     vis[vv]=1;

     dfs(vv,v,d-1,co);

     vis[vv]=0;

   }

}

int main()

{

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

  s=n;

  // for(int i=1;i<=n;++i)a[i]=read(),c[i]=0;

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

  {

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

    inc(x,y);inc(y,x);

  }

  int q=read();

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

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

  {

    int u=qu[i].u,d=qu[i].d,co=qu[i].c;

    if(c[u]==0)c[u]=co,--s;

    if(s==0)break;

    vis[u]=1;

    dfs(u,u,d,co);

    vis[u]=0;

  }

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

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int N = 1e5 + 10;



vector <int> G[N];

int rec[N], color[N], ver[N], d[N], c[N];



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

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

	if (~rec[x] && dist <= rec[x]) return;

	rec[x] = dist;

	if (dist == 0) return;

	for (int i = 0; i < G[x].size(); i++) dfs(G[x][i], dist - 1, col);

}



int main(){

	int n, m, q, u, v;

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

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

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

		G[u].push_back(v);

		G[v].push_back(u);

	}

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

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

	scanf("%d",&q);

	for (int i = 1; i <= q; i++) scanf("%d %d %d",&ver[i],&d[i],&c[i]);

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

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.4243504066223025x):
#include<cstdio>

#include<fstream>

#include<queue>

#include<iostream>

#include<cstring>

#define N 100010

using namespace std;

int n,m;

struct rode{

  int y,next;

}e[N<<1]={};

int fl[N]={},len=0;

int a[N]={};

inline int read()

{

	int s = 0, w = 1;

	char c = getchar();

	while ((c<'0' || c>'9') && c != '0')c = getchar();

	if (c == '-')w = -1, c = getchar();

	while (c <= '9'&&c >= '0')s = (s << 3) + (s << 1) + c - '0', c = getchar();

	return s * w;

}

inline void inc(int x,int y)

{

  e[++len].y=y;

  e[len].next=fl[x];

  fl[x]=len;

}

struct que{

  int u,d,c;

}qu[N]={};

int c[N]={};

bool vis[N]={};

int s=0;

void dfs(int u,int v,int d,int co)

{

  if(d==0)return ;

  for(int i=fl[u];i;i=e[i].next)

   {

     int vv=e[i].y;

     if(vis[vv])continue;

     if(c[vv]==0)c[vv]=co,--s;

     if(s==0)return ;

     vis[vv]=1;

     dfs(vv,v,d-1,co);

     vis[vv]=0;

   }

}

int main()

{

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

  s=n;

  // for(int i=1;i<=n;++i)a[i]=read(),c[i]=0;

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

  {

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

    inc(x,y);inc(y,x);

  }

  int q=read();

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

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

  {

    int u=qu[i].u,d=qu[i].d,co=qu[i].c;

    if(c[u]==0)c[u]=co,--s;

    if(s==0)break;

    vis[u]=1;

    dfs(u,u,d,co);

    vis[u]=0;

  }

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

  return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.4180628354872156x):
#include<cstdio>

#include<fstream>

#include<queue>

#include<iostream>

#include<cstring>

#define N 100010

using namespace std;

int n,m;

struct rode{

  int y,next;

}e[N<<1]={};

int fl[N]={},len=0;

int a[N]={};

inline int read()

{

	int s = 0, w = 1;

	char c = getchar();

	while ((c<'0' || c>'9') && c != '0')c = getchar();

	if (c == '-')w = -1, c = getchar();

	while (c <= '9'&&c >= '0')s = (s << 3) + (s << 1) + c - '0', c = getchar();

	return s * w;

}

inline void inc(int x,int y)

{

  e[++len].y=y;

  e[len].next=fl[x];

  fl[x]=len;

}

struct que{

  int u,d,c;

}qu[N]={};

int c[N]={};

bool vis[N]={};

int s=0;

void dfs(int u,int v,int d,int co)

{

  if(d==0)return ;

  for(int i=fl[u];i;i=e[i].next)

   {

     int vv=e[i].y;

     if(vis[vv])continue;

     if(c[vv]==0)c[vv]=co,--s;

     if(s==0)return ;

     vis[vv]=1;

     dfs(vv,v,d-1,co);

     vis[vv]=0;

   }

}

int main()

{

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

  s=n;

  // for(int i=1;i<=n;++i)a[i]=read(),c[i]=0;

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

  {

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

    inc(x,y);inc(y,x);

  }

  int q=read();

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

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

  {

    int u=qu[i].u,d=qu[i].d,co=qu[i].c;

    if(c[u]==0)c[u]=co,--s;

    if(s==0)break;

    vis[u]=1;

    dfs(u,u,d,co);

    vis[u]=0;

  }

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

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <vector>



const int MAXN = 1e5 + 100;



int n, m, Q;

int x[MAXN], d[MAXN], c[MAXN];



std::vector<int> edge[MAXN << 1];



int col[MAXN][20];



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

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

		return;

	col[x][d] = c;

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

		int y = edge[x][i];

		Dfs(y, d - 1, c);

	}

}



int main() {

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

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

		int u, v;

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

		edge[u].push_back(v), edge[v].push_back(u);

	}

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

		edge[i].push_back(i);



	scanf("%d", &Q);

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

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



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

		Dfs(x[i], d[i], c[i]);

	

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

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 6.466826053987899x):
#include<cstdio>

#include<fstream>

#include<queue>

#include<iostream>

#include<cstring>

#define N 100010

using namespace std;

int n,m;

struct rode{

  int y,next;

}e[N<<1]={};

int fl[N]={},len=0;

int a[N]={};

inline int read()

{

	int s = 0, w = 1;

	char c = getchar();

	while ((c<'0' || c>'9') && c != '0')c = getchar();

	if (c == '-')w = -1, c = getchar();

	while (c <= '9'&&c >= '0')s = (s << 3) + (s << 1) + c - '0', c = getchar();

	return s * w;

}

inline void inc(int x,int y)

{

  e[++len].y=y;

  e[len].next=fl[x];

  fl[x]=len;

}

struct que{

  int u,d,c;

}qu[N]={};

int c[N]={};

bool vis[N]={};

int s=0;

void dfs(int u,int v,int d,int co)

{

  if(d==0)return ;

  for(int i=fl[u];i;i=e[i].next)

   {

     int vv=e[i].y;

     if(vis[vv])continue;

     if(c[vv]==0)c[vv]=co,--s;

     if(s==0)return ;

     vis[vv]=1;

     dfs(vv,v,d-1,co);

     vis[vv]=0;

   }

}

int main()

{

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

  s=n;

  // for(int i=1;i<=n;++i)a[i]=read(),c[i]=0;

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

  {

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

    inc(x,y);inc(y,x);

  }

  int q=read();

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

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

  {

    int u=qu[i].u,d=qu[i].d,co=qu[i].c;

    if(c[u]==0)c[u]=co,--s;

    if(s==0)break;

    vis[u]=1;

    dfs(u,u,d,co);

    vis[u]=0;

  }

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

  return 0;

}
// Slower C++ solution:
//

//  main.cpp

//  backToProblemSolving

//

//  Created by Khaled Abdelfattah on 6/27/17.

//  Copyright © 2017 Khaled Abdelfattah. All rights reserved.

//



#include <bits/stdc++.h>

// #include <iostream>

using namespace std;

//#include "stdc++.h"



typedef long long ll;

#define MAX 1000000000

#define BASE1 23

#define BASE2 31

#define MOD1 1000000007

#define MOD2 217645177

#define eps 1e-6



int colors[100005];

vector<int> adjList[100005];

bool visited[100005][15];

void DFS (int node, int d, int c) {

    if (d < 0 || visited[node][d])

        return;

    visited[node][d] = true;

    if (colors[node] == 0)

        colors[node] = c;

    for (int i : adjList[node])

        DFS(i, d - 1, c);

}

int main() {

    int n, m;

    cin >> n >> m;

    memset(colors, 0, n);

    memset(visited, 0, sizeof visited);

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

        int u, v;

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

        v--, u--;

        adjList[u].push_back(v);

        adjList[v].push_back(u);

    }

    int q;

    cin >> q;

    pair<pair<int, int>, int> *quers = new pair<pair<int, int>, int> [q];

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

        int s, d, c;

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

        quers[i] = {{s - 1, d}, c};

    }

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

        DFS(quers[i].first.first, quers[i].first.second, quers[i].second);

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

        cout << colors[i] << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.282615401295047x):
#include<bits/stdc++.h>

#define ll long long

#define rint register int

using namespace std;

const int N=1e5+7;

int tot,n,m,q,v[N],d[N],c[N],first[N];

int f[N][11];

struct fuk

{

	int x,next;

}a[N<<2];

inline void add(int x,int to)

{

	tot++;

	a[tot].next=first[x]; a[tot].x=to; first[x]=tot;

}

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

{

	if(d==-1) return;

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

	f[x][d]=col; 

	for(int i=first[x];i;i=a[i].next)

	{

		int y=a[i].x;

		dfs(y,d-1,col);

	}

}

int main()

{

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

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

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

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

  {

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

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

  } 

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

  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",f[i][0]);

  return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

int q;

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

vector<int> g[100005];

int colour[100005];

int mindis[100005];

inline void dfs(int x,int dleft,int c)

{

	if(dleft<=mindis[x])

	{

		return;

	}

	mindis[x]=dleft;

	if(colour[x]==0)

	{

		colour[x]=c;

	}

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

	{

		dfs(g[x][i],dleft-1,c);

	}

}

int main()

{

	cin>>n>>m;

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

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

	{

		int a,b;

		cin>>a>>b;

		a--;

		b--;

		g[a].push_back(b);

		g[b].push_back(a);

	}

	cin>>q;

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

	{

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

	}

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

	{

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

	}

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

	{

		cout<<colour[i]<<endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.959688936716647x):
#include<bits/stdc++.h>

#define ll long long

#define rint register int

using namespace std;

const int N=1e5+7;

int tot,n,m,q,v[N],d[N],c[N],first[N];

int f[N][11];

struct fuk

{

	int x,next;

}a[N<<2];

inline void add(int x,int to)

{

	tot++;

	a[tot].next=first[x]; a[tot].x=to; first[x]=tot;

}

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

{

	if(d==-1) return;

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

	f[x][d]=col; 

	for(int i=first[x];i;i=a[i].next)

	{

		int y=a[i].x;

		dfs(y,d-1,col);

	}

}

int main()

{

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

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

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

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

  {

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

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

  } 

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

  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",f[i][0]);

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int N = 1e5 + 10;



vector <int> G[N];

int rec[N], color[N], ver[N], d[N], c[N];



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

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

	if (~rec[x] && dist <= rec[x]) return;

	rec[x] = dist;

	if (dist == 0) return;

	for (int i = 0; i < G[x].size(); i++) dfs(G[x][i], dist - 1, col);

}



int main(){

	int n, m, q, u, v;

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

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

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

		G[u].push_back(v);

		G[v].push_back(u);

	}

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

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

	scanf("%d",&q);

	for (int i = 1; i <= q; i++) scanf("%d %d %d",&ver[i],&d[i],&c[i]);

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

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.384654068710259x):
#include<bits/stdc++.h>

#define ll long long

#define rint register int

using namespace std;

const int N=1e5+7;

int tot,n,m,q,v[N],d[N],c[N],first[N];

int f[N][11];

struct fuk

{

	int x,next;

}a[N<<2];

inline void add(int x,int to)

{

	tot++;

	a[tot].next=first[x]; a[tot].x=to; first[x]=tot;

}

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

{

	if(d==-1) return;

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

	f[x][d]=col; 

	for(int i=first[x];i;i=a[i].next)

	{

		int y=a[i].x;

		dfs(y,d-1,col);

	}

}

int main()

{

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

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

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

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

  {

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

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

  } 

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

  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",f[i][0]);

  return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.4053396340396642x):
#include<bits/stdc++.h>

#define ll long long

#define rint register int

using namespace std;

const int N=1e5+7;

int tot,n,m,q,v[N],d[N],c[N],first[N];

int f[N][11];

struct fuk

{

	int x,next;

}a[N<<2];

inline void add(int x,int to)

{

	tot++;

	a[tot].next=first[x]; a[tot].x=to; first[x]=tot;

}

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

{

	if(d==-1) return;

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

	f[x][d]=col; 

	for(int i=first[x];i;i=a[i].next)

	{

		int y=a[i].x;

		dfs(y,d-1,col);

	}

}

int main()

{

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

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

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

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

  {

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

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

  } 

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

  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",f[i][0]);

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <vector>



const int MAXN = 1e5 + 100;



int n, m, Q;

int x[MAXN], d[MAXN], c[MAXN];



std::vector<int> edge[MAXN << 1];



int col[MAXN][20];



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

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

		return;

	col[x][d] = c;

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

		int y = edge[x][i];

		Dfs(y, d - 1, c);

	}

}



int main() {

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

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

		int u, v;

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

		edge[u].push_back(v), edge[v].push_back(u);

	}

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

		edge[i].push_back(i);



	scanf("%d", &Q);

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

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



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

		Dfs(x[i], d[i], c[i]);

	

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

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 6.408804132425522x):
#include<bits/stdc++.h>

#define ll long long

#define rint register int

using namespace std;

const int N=1e5+7;

int tot,n,m,q,v[N],d[N],c[N],first[N];

int f[N][11];

struct fuk

{

	int x,next;

}a[N<<2];

inline void add(int x,int to)

{

	tot++;

	a[tot].next=first[x]; a[tot].x=to; first[x]=tot;

}

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

{

	if(d==-1) return;

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

	f[x][d]=col; 

	for(int i=first[x];i;i=a[i].next)

	{

		int y=a[i].x;

		dfs(y,d-1,col);

	}

}

int main()

{

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

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

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

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

  {

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

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

  } 

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

  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",f[i][0]);

  return 0;

}