Problem:


Score : 700 points

Problem Statement

Squid loves painting vertices in graphs.

There is a simple undirected graph consisting of N vertices numbered 1 through N, and M edges.
Initially, all the vertices are painted in color 0. The i-th edge bidirectionally connects two vertices a_i and b_i. The length of every edge is 1.

Squid performed Q operations on this graph. In the i-th operation, he repaints all the vertices within a distance of d_i from vertex v_i, in color c_i.

Find the color of each vertex after the Q operations.

Constraints

1 ≤ N,M,Q ≤ 10^5

1 ≤ a_i,b_i,v_i ≤ N

a_i ≠ b_i

0 ≤ d_i ≤ 10

1 ≤ c_i ≤10^5

d_i and c_i are all integers.

There are no self-loops or multiple edges in the given graph.

Partial Score

200 points will be awarded for passing the testset satisfying 1 ≤ N,M,Q ≤ 2{,}000.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{M} b_{M}
Q
v_1 d_1 c_1
:
v_{Q} d_{Q} c_{Q}

Output

Print the answer in N lines.
In the i-th line, print the color of vertex i after the Q operations.

Sample Input 1

7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2

Sample Output 1

2
2
2
2
2
1
0

Initially, each vertex is painted in color 0.
In the first operation, vertices 5 and 6 are repainted in color 1.
In the second operation, vertices 1, 2, 3, 4 and 5 are repainted in color 2.

Sample Input 2

14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3

Sample Output 2

1
0
3
1
5
5
3
3
6
1
3
4
5
3

The given graph may not be connected.



Sample input:
7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2


Sample output:
2
2
2
2
2
1
0

Submission status: Accepted

Solution code:

n, m = map(int, input().split())
edges = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]

q = int(input())
vv = [None] * q
dd = [None] * q
cc = [None] * q
dp = [[-1 for j in range(11)] for i in range(n)]
for i in range(q):
    vv[i], dd[i], cc[i] = map(int, input().split())
    dp[vv[i] - 1][dd[i]] = i

for i in reversed(range(1, 11)):
    for j in range(n):
        dp[j][i - 1] = max(dp[j][i - 1], dp[j][i])
    for u, v in edges:
        dp[u][i - 1] = max(dp[u][i - 1], dp[v][i])
        dp[v][i - 1] = max(dp[v][i - 1], dp[u][i])

for i in range(n):
    if dp[i][0] == -1:
        print('0')
    else:
        print(cc[dp[i][0]])

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
node = [[] for _ in range(n + 1)]
for i in range(m):
    a, b = map(int, input().split())
    node[a].append(b)
    node[b].append(a)

q = int(input())
node_color = [0 for _ in range(n + 1)]
vdc = [list(map(int, input().split())) for _ in range(q)]

vdc = vdc[::-1]
painted = set()
visited = [[0] * 11 for _ in range(n + 1)]


def dfs(now, dist, color):
    if dist < 0:
        return
    if visited[now][dist]:
        return

    visited[now][dist] += 1
    if not node_color[now]:
        node_color[now] = color

    for x in node[now]:
        dfs(x, dist - 1, color)


for i in vdc:
    v, d, c = i
    dfs(v, d, c)

for m in range(n):
    print(node_color[m + 1])

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def main():
    n,m = LI()
    e = collections.defaultdict(set)
    for _ in range(m):
        a,b = LI()
        e[a-1].add(b-1)
        e[b-1].add(a-1)
    r = [0] * n

    q = I()
    qq = sorted([[_] + LI() for _ in range(q)], reverse=True)
    f = [-1] * n
    for _,v,d,c in qq:
        v -= 1
        if f[v] >= d:
            continue
        if r[v] == 0:
            r[v] = c
        q = [v]
        for nd in range(d-1,-1,-1):
            t = []
            for p in q:
                for vv in e[p]:
                    if f[vv] >= nd:
                        continue
                    f[vv] = nd
                    t.append(vv)
                    if r[vv] == 0:
                        r[vv] = c
            q = t

    return '\n'.join(map(str, r))



print(main())

Submission status: Accepted

Solution code:
from collections import deque

G = dict()
N, M = map(int, input().split())
color = [0]*(N+1)
for n in range(1, N+1):
    G[n] = []
for _ in range(M):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)
Q = int(input())




data = [list(map(int, input().split())) for _ in range(Q)]
data.reverse()
color_d = [-1]*(N+1)

for v, d, c in data:
    if color_d[v] >= d:
        pass
    # BFS
    deq = deque([v])
    if color[v] == 0:
        color[v] = c
    color_d[v] = d
    while len(deq) > 0:
        s = deq.popleft()
        for t in G[s]:
            if color_d[t] >= color_d[s] - 1:
                pass
            else:
                color_d[t] = color_d[s] - 1
                if color[t] == 0:
                    color[t] = c
                if color_d[t] > 0:
                    deq.append(t)
                
for n in range(1, N+1):
    print(color[n])

Submission status: Accepted

Solution code:
def dfs(v, d, c):
    if vis[v][d] or d < 0:
        return
    vis[v][d] = 1
    if not color[v]:
        color[v] = c
    for u in E[v]:
            dfs(u, d - 1, c)
n, m = map(int, raw_input().split())
E = [[] for i in xrange(n + 1)]
for i in xrange(m):
    a, b = map(int, raw_input().split())
    E[a].append(b)
    E[b].append(a)
q = input()
operations = [map(int, raw_input().split()) for i in xrange(q)]
operations.reverse()
color = [0] * (n + 1)
vis = [[0] * 11 for i in xrange(n + 1)]
for op in operations:
    dfs(op[0], op[1], op[2])
for i in xrange(1, n + 1):
    print color[i]

Submission status: Accepted

Solution code:
from sys import stdin, stderr
from collections import deque

def solve():
    N, M = map(int, input().split())

    Adj = [[] for i in range(N)]

    for i in range(M):
        a, b = map(int, stdin.readline().split())
        a, b = a - 1, b - 1
        Adj[a].append(b)
        Adj[b].append(a)

    Q = int(stdin.readline())

    v = [0] * Q
    d = [0] * Q
    c = [0] * Q

    for i in range(Q):
        v[i], d[i], c[i] = map(int, stdin.readline().split())
        v[i] -= 1

    v.reverse()
    d.reverse()
    c.reverse()

    cols = [0] * N
    nuta = [-1] * N

    for i in range(Q):
        bfs(N, Adj, v[i], d[i], c[i], cols, nuta)

    print(*cols, sep='\n')

def bfs(N, Adj, v, d, c, cols, nuta):
    if nuta[v] >= d:
        return

    nxts = deque([[v, d]])
    nuta[v] = d

    while nxts:
        u, step = nxts.popleft()

        if step < 0:
            continue

        if cols[u] == 0:
            cols[u] = c

        for w in Adj[u]:
            if nuta[w] < step - 1:
                nuta[w] = step - 1
                nxts.append([w, step - 1])

    return

if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
N, M = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(M)]
Q = int(input())
vdc = [list(map(int, input().split())) for _ in range(Q)]

L = [[] for _ in range(N)]
for a, b in ab:
  a -= 1
  b -= 1
  L[a].append(b)
  L[b].append(a)

D = [[0] * 10 for _ in range(N)]
Ans = [0] * N

def check(v, d, c):
  if D[v][d] != 0: return
  if d == 0:
    D[v][d] = c
    return
  for i in range(d, -1, -1):
    if D[v][i] != 0: break
    D[v][i] = c
  for i in L[v]:
    pass

import queue
Que = queue.Queue
get = queue.Queue.get
put = queue.Queue.put
emp = queue.Queue.empty

for v, d, c in vdc[::-1]:
  v -= 1
  if D[v][d] != 0: continue
  t = (d << 18) + v
  S = Que()
  put(S, t)
  while not emp(S):
    t = get(S)
    dd = t >> 18
    vv = t & ((1 << 18) - 1)
    if Ans[vv] == 0: Ans[vv] = c
    if dd == 0:
      D[vv][dd] = c
      continue
    for i in range(dd, -1, -1):
      if D[vv][i] != 0: break
      D[vv][i] = c
    for i in L[vv]:
      if D[i][dd - 1] != 0: continue
      t = ((dd - 1) << 18) + i
      put(S, t)

for i in Ans:
  print(i)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
n,m = map(int,input().split())
e = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    e[a].append(b)
    e[b].append(a)
qv = int(input())
Q = [list(map(int,input().split())) for i in range(qv)]
Q = Q[::-1]
l = [[0]*(11) for i in range(n+1)]
for v,d,c in Q:
    if l[v][d] > 0:
        continue
    l[v][d] = c
dic = {}
color = [0]*(n+1)

left = [i for i in range(1,n+1)]
num = 0
while num < qv and left:
    v,d,c = Q[num]
    if v not in dic:
        for i in range(10,-1,-1):
            if l[v][i] > 0:
                M = i
                break
        dic[v] = [set() for i in range(n+1)]
        s = set()
        s.add(v)
        dic[v][0] |= s
        dist = [float("INF")]*(n+1)
        dist[v] = 0
        q = [v]
        for i in range(1,M+1):
            nq = []
            for now in q:
                for nex in e[now]:
                    if dist[nex]<= i:
                        continue
                    nq.append(nex)
                    dist[nex] = i
                    s.add(nex)
            dic[v][i] |= s
            q = nq
    nl = []
    
    for i in left:
        if i in dic[v][d]:
            color[i] = c
        else:
            nl.append(i) 
    num += 1
    
    left = nl
    
for i in color[1:]:
    print(i)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
n,m = map(int,input().split())
e = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    e[a].append(b)
    e[b].append(a)
qv = int(input())
Q = [list(map(int,input().split())) for i in range(qv)]
Q = Q[::-1]
l = [[0]*(11) for i in range(n+1)]
for v,d,c in Q:
    if l[v][d] > 0:
        continue
    l[v][d] = c
dic = {}
color = [0]*(n+1)

left = [i for i in range(1,n+1)]
num = 0
while num < qv and left:
    v,d,c = Q[num]
    if v not in dic:
        for i in range(10,-1,-1):
            if l[v][i] > 0:
                M = i
                break
        dic[v] = [set() for i in range(n+1)]
        s = set()
        s.add(v)
        dist = [float("INF")]*(n+1)
        dist[v] = 0
        q = [v]
        for i in range(1,M+1):
            nq = []
            for now in q:
                for nex in e[now]:
                    if dist[nex]<= i:
                        continue
                    nq.append(nex)
                    dist[nex] = i
                    s.add(nex)
            dic[v][i] |= s
            q = nq
    nl = []
    
    for i in left:
        if i in dic[v][d]:
            color[i] = c
        else:
            nl.append(i) 
    num += 1
    
    left = nl
    
for i in color[1:]:
    print(i)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
n,m = map(int,input().split())
e = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    e[a].append(b)
    e[b].append(a)
qv = int(input())
Q = [list(map(int,input().split())) for i in range(qv)]
Q = Q[::-1]
l = [[0]*(11) for i in range(n+1)]
for v,d,c in Q:
    if l[v][d] > 0:
        continue
    l[v][d] = c
dic = {}
color = [0]*(n+1)

left = [i for i in range(1,n+1)]
num = 0
while num < qv and left:
    v,d,c = Q[num]
    if v not in dic:
        for i in range(10,-1,-1):
            if l[v][i] > 0:
                M = i
                break
        dic[v] = [set() for i in range(n+1)]
        s = set()
        s.add(v)
        dist = [float("INF")]*(n+1)
        dist[v] = 0
        q = [v]
        for i in range(1,m+1):
            nq = []
            for now in q:
                for nex in e[now]:
                    if dist[nex]<= i:
                        continue
                    nq.append(nex)
                    dist[nex] = i
                    s.add(nex)
            dic[v][i] |= s
            q = nq
    nl = []
    
    for i in left:
        if i in dic[v][d]:
            color[i] = c
        else:
            nl.append(i) 
    num += 1
    
    left = nl
    
for i in color[1:]:
    print(i)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
n,m = map(int,input().split())
e = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    e[a].append(b)
    e[b].append(a)
qv = int(input())
Q = [list(map(int,input().split())) for i in range(qv)]
Q = Q[::-1]
l = [[0]*(11) for i in range(n+1)]
for v,d,c in Q:
    if l[v][d] > 0:
        continue
    l[v][d] = c
dic = {}
color = [0]*(n+1)

left = [i for i in range(1,n+1)]
num = 0
while num < qv and left:
    v,d,c = Q[num]
    if v not in dic:
        for i in range(10,0,-1):
            if l[v][i] > 0:
                M = i
                break
        dic[v] = [set() for i in range(n+1)]
        s = set()
        s.add(v)
        dist = [float("INF")]*(n+1)
        dist[v] = 0
        q = [v]
        for i in range(1,m+1):
            nq = []
            for now in q:
                for nex in e[now]:
                    if dist[nex]<= i:
                        continue
                    nq.append(nex)
                    dist[nex] = i
                    s.add(nex)
            dic[v][i] |= s
            q = nq
    nl = []
    
    for i in left:
        if i in dic[v][d]:
            color[i] = c
        else:
            nl.append(i) 
    num += 1
    
    left = nl
    
for i in color[1:]:
    print(i)

Submission status: Runtime Error

Solution code:
from collections import deque

n,m = map(int,input().split())
e = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    e[a].append(b)
    e[b].append(a)
qv = int(input())
Q = [list(map(int,input().split())) for i in range(qv)]
Q = Q[::-1]
l = [[0]*(11) for i in range(n+1)]
for v,d,c in Q:
    if l[v][d] > 0:
        continue
    l[v][d] = c
dic = {}
color = [0]*(n+1)

left = [i for i in range(1,n+1)]
num = 0
while num < qv and left:
    v,d,c = Q[num]
    if v not in dic:
        for i in range(10,0,-1):
            if l[v][i] > 0:
                M = i
                break
        dic[v] = [set() for i in range(n+1)]
        s = set()
        s.add(v)
        dist = [float("INF")]*(n+1)
        dist[v] = 0
        q = [v]
        for i in range(1,m+1):
            nq = []
            for now in q:
                for nex in e[now]:
                    if dist[nex]<= i:
                        continue
                    nq.append(nex)
                    dist[nex] = i
                    s.add(nex)
            dic[v][i] |= s
            q = nq
    nl = []
    
    for i in left:
        if i in dic[v][d]:
            color[i] = c
        else:
            nl.append(i) 
    num += 1
    
    left = nl
    
for i in color[1:]:
    print(i)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
n,m=map(int, input().split())
e=[[] for _ in range(n)]
for _ in range(m):
    ai,bi=map(int, input().split())
    ai-=1;bi-=1
    e[ai].append(bi);e[bi].append(ai)
Q=int(input())
q=[tuple(map(int, input().split())) for _ in range(Q)]

q.reverse()
color=[0]*n
from collections import deque
que=deque()
used=set()
def bfs(v,d,c):
    if d<0:
        return
    used.add(v)
    if not color[v]:
        color[v]=c
    #que.append((v,d))
    for u in e[v]:
        que.append((u,d-1))
    while que:
        u,dd=que.popleft()
        if u not in used:
            bfs(u,dd,c)
for v,d,c in q:
    v-=1
    used=set()
    bfs(v,d,c)
    #print(v,d,c,"   ",*color)
for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
n,m=map(int, input().split())
e=[[] for _ in range(n)]
for _ in range(m):
    ai,bi=map(int, input().split())
    ai-=1;bi-=1
    e[ai].append(bi);e[bi].append(ai)
Q=int(input())
q=[tuple(map(int, input().split())) for _ in range(Q)]

q.reverse()
color=[0]*n
from collections import deque
que=deque()
used=set()
def bfs(v,d,c):
    if d<0:
        return
    used.add(v)
    if not color[v]:
        color[v]=c
    #que.append((v,d))
    for u in e[v]:
        que.append((u,d-1))
    while que:
        u,dd=que.popleft()
        if u not in used:
            bfs(u,dd,c)
for v,d,c in q:
    v-=1
    used=set()
    bfs(v,d,c)
    #print(v,d,c,"   ",*color)
for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
n,m=map(int, input().split())
e=[[] for _ in range(n)]
for _ in range(m):
    ai,bi=map(int, input().split())
    ai-=1;bi-=1
    e[ai].append(bi);e[bi].append(ai)
Q=int(input())
q=[tuple(map(int, input().split())) for _ in range(Q)]

q.reverse()
color=[0]*n
from collections import deque
que=deque()
def bfs(v,d,c):
    if color[v] or d<0:
        return
    que.append((v,d))
    color[v]=c
    for u in e[v]:
        que.append((u,d-1))
    while que:
        u,dd=que.popleft()
        bfs(u,dd,c)
for v,d,c in q:
    v-=1
    bfs(v,d,c)
    #print(v,d,c,"   ",*color)
for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
N = int(input())
moti_list = []
for i in range(N):
    moti_list.append(int(input()))

moti_list.sort(reverse=True)
tower = 1
for j in range(N - 1):
    if moti_list[j] > moti_list[j + 1]:
        tower += 1

print(tower)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def graph_input(N, M):
    G = [[] for _ in range(N + 1)]
    for _ in range(M):
        a, b = map(int, readline().split())
        G[a].append(b)
        G[b].append(a)
    ind = np.cumsum(list(map(len, G)))
    G = np.concatenate(G).astype(np.int32)
    return G, ind

def main(N, G, ind, V, D, C):
    # 強さ n で塗った時刻
    dp = np.full((N + 1, 11), -1, np.int32)
    for t in range(len(V)):
        v, d = V[t], D[t]
        dp[v, d] = t
    for n in range(10, 0, -1):
        for v in range(1, N + 1):
            dp[v, n - 1] = max(dp[v, n - 1], dp[v, n])
            for i in range(ind[v - 1], ind[v]):
                w = G[i]
                dp[w, n - 1] = max(dp[w, n - 1], dp[v, n])
    dp = dp[:, 0]
    for v in range(1, N + 1):
        t = dp[v]
        c = C[t] if t != -1 else 0
        print(c)

signature = '(i8,i4[:],i4[:],i4[:],i4[:],i4[:])'
if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('main', signature)(main)
    cc.compile()

from my_module import main

N, M = map(int, readline().split())
G, ind = graph_input(N, M)
Q = int(readline())
nums = np.array(read().split(), np.int32)
V = nums[::3]
D = nums[1::3]
C = nums[2::3]

main(N, G, ind, V, D, C)

Submission status: Runtime Error

Solution code:
from collections import deque

n, m = map(int, input().split())
G = [list() for _ in range(n)]
for _ in range(m):
  a, b = map(int, input().split())
  a -= 1; b -= 1
  G[a].append(b)
  G[b].append(a)
Q = int(input())
queries = [tuple(map(int, input().split())) for _ in range(Q)][::-1]
dp = [[Q]*n for _ in range(11)]
for i, (v, d, _) in enumerate(queries):
  dp[v-1][d] = i
for d in range(10, 0, -1):
  for v in range(n):
    if dp[v][d] < Q:
      dp[v][d-1] = min(dp[v][d-1], dp[v][d])
      for x in G[v]:
        dp[x][d-1] = min(dp[x][d-1], dp[v][d])
queries.append((-1, -1, 0))
color = [queries[dp[v][0]][2] for v in range(n)]
print(*color, sep='\n')

Submission status: Runtime Error

Solution code:
from collections import deque

n, m = map(int, input().split())
G = [list() for _ in range(n)]
for _ in range(m):
  a, b = map(int, input().split())
  a -= 1; b -= 1
  G[a].append(b)
  G[b].append(a)
Q = int(input())
queries = [tuple(map(int, input().split())) for _ in range(Q)][::-1]
dp = [[Q]*n for _ in range(n)]
for i, (v, d, _) in enumerate(queries):
  dp[v-1][d] = i
for d in range(10, 0, -1):
  for v in range(n):
    if dp[v][d] < Q:
      dp[v][d-1] = min(dp[v][d-1], dp[v][d])
      for x in G[v]:
        dp[x][d-1] = min(dp[x][d-1], dp[v][d])
queries.append((-1, -1, 0))
color = [queries[dp[v][0]][2] for v in range(n)]
print(*color, sep='\n')

Submission status: Runtime Error

Solution code:
#b
#by using the condition d<=10, we should memorize V*d whether the pair is already searched.
#if searched, we can stop.

n,m = [int(i) for i in input().split()]
edges = [[] for _ in range(n)]
colors = [0]*n

checker = [[0 for _ in range(10)] for _ in range(n)]

def dfs(d,v,c):
    if checker[v][d] != 0 or d<0:
        return
    #d>=0 and (v,d) is not checked
    colors[v] = c
    checker[v][d] = 1 #checked
    
    dfs(d-1, v, c) #dが低いやつについて全て埋めておく必要がある。もう先着ありで無理ですよ、って宣言。
    
    nodes=edges[v]
    for u in nodes:
        dfs(d-1, u, c)

        

for _ in range(m):
    a,b = [int(i) for i in input().split()]
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
    
q = int(input())
info = []
for _ in range(q):
    v,d,c = [int(i) for i in input().split()]
    info.append((v,d,c))
    
info.reverse()
for tupple in info:
    v,d,c = tupple
    
    #not bfs but dfs
    dfs(d,v-1,c)
    
for i in range(n):
    print(colors[i])

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N, M = map(int, input().split())
e = dd(list)

for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)

base = 10 ** 6
def clean():
  global res
  for i in range(1, N + 1):
    if len(res[i]) == 0: continue
    p = max(res[i])
    z = p % base
    d = p // base
    newrs = set()
    newrs.add(p)
    for x in res[i]:
      if qd[x % base] > d: newrs.add(x)
    res[i] = newrs

res = [set() for _ in range(N + 1)]
Q = int(input())
qd = [0] * Q
qc = [0] * Q
for i in range(Q):
  v, d, c = map(int, input().split())
  qd[i] = d
  qc[i] = c
  res[v].add(d * base + i)
#print(qc)
clean()
for t in range(10):
  tres = [set() | s for s in res]
  for x in range(1, N + 1):
    for y in e[x]:
      for z in res[x]:
        if qd[z % base] > t: tres[y].add(z)
  res = tres
  clean()
#print(res)

for r in res[1: ]:
  if len(r): print(qc[max(r)])
  else: print(0)

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N, M = map(int, input().split())
e = dd(list)

for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)

res = [set() for _ in range(N + 1)]
Q = int(input())
qd = [0] * Q
qc = [0] * Q
for i in range(Q):
  v, d, c = map(int, input().split())
  qd[i] = d
  qc[i] = c
  res[v].add(i)
#print(qd)

def clean():
  global res
  for i in range(1, N + 1):
    a = sorted(res[i], reverse = True)
    d = -1
    for j in range(len(a)):
      x = a[j]
      if qd[x] <= d: res[i].discard(y)
      d = max(d, qd[x])
clean()
for t in range(10):
  tres = [set() | s for s in res]
  for x in range(1, N + 1):
    for y in e[x]:
      for z in res[x]:
        if qd[z] > t: tres[y].add(z)
  res = tres
  clean()
#print(res)

for r in res[1: ]:
  if len(r): print(qc[max(r)])
  else: print(0)

Submission status: Runtime Error

Solution code:
from collections import deque
import numpy as np
import sys

n, m  = map(int, input().split())
g     = {}
maxD  = np.array([-1 for i in range(n)])
color = np.array([0  for _ in range(n)])

for u, v in (map(int, sys.stdin.readline().split()) for _ in [0]*n):
    u-=1
    v-=1
    if u not in g:
        g[u] = []
    if v not in g:
        g[v] = []
    g[u].append(v)
    g[v].append(u)
    
q = int(input())
Q = [list(map(int, l.split())) for l in sys.stdin][::-1]

def bfs(curQ):
    while len(curQ) > 0:
        node, dis, col = curQ.pop()
    
        if maxD[node] > dis:
            continue
            
        if dis < 0:
            continue
            
        maxD[node] = dis
    
        if color[node] == 0:
            color[node] = col

        if node in g:
            for v in g[node]:
                if dis-1 > maxD[v]:
                    curQ.append([v, dis-1, col])
                    
for node, dis, col in Q:
    curQ = deque()
    curQ.append([node-1, dis, col])
    bfs(curQ)

ans = ''
for x in color:
    ans += str(x)+'\n'
print(ans)

Submission status: Runtime Error

Solution code:
n, m  = map(int, input().split())
g     = {}
used  = {i:[False for _ in range(11)] for i in range(n)}
color     = [0  for _ in range(n)]
 
for _ in range(m):
    u, v = map(int, input().split())
    u-=1
    v-=1
    if u not in g:
        g[u] = []
    if v not in g:
        g[v] = []
    g[u].append(v)
    g[v].append(u)
    
q = int(input())
Q = []
 
for _ in range(q):
    node, dis, col = map(int, input().split())
    Q.append([node-1, dis, col])
Q = Q[::-1]
 
def bfs(now, dist, col):
    if dist < 0:
        return
    
    if used[now][dist]:
        return
 
    used[now][dist] = True
    
    if not color[now]:
        color[now] = col
 
	if now in g:
    	for x in g[now]:
        	bfs(x, dist - 1, col)
            
for node, dis, col in Q:
    bfs(node, dis, col)

for x in color:
    print(x)

Submission status: Runtime Error

Solution code:
n, m  = map(int, input().split())
g     = {}
used  = {i:[False for _ in range(11)] for i in range(n)}
color     = [0  for _ in range(n)]

for _ in range(m):
    u, v = map(int, input().split())
    u-=1
    v-=1
    if u not in g:
        g[u] = []
    if v not in g:
        g[v] = []
    g[u].append(v)
    g[v].append(u)
    
q = int(input())
Q = []

for _ in range(q):
    node, dis, col = map(int, input().split())
    Q.append([node-1, dis, col])
Q = Q[::-1]

def bfs(now, dist, col):
    if dist < 0:
        return
    
    if used[now][dist]:
        return
 
    used[now][dist] = True
    
    if not color[now]:
        color[now] = col
 
    for x in g[now]:
        bfs(x, dist - 1, col)
            
for node, dis, col in Q:
    bfs(node, dis, col)

for x in color:
    print(x)

Submission status: Runtime Error

Solution code:
import numpy as np 
N, M = map(int, input().split())

#　グラフの初期化
A = [[] for i in range(N)]

# グラフの作成
for k in range(M):
  a, b = map(int, input().split())
  a, b = sorted([a, b])
  A[a-1].append(b-1)

# 距離
distance = np.full((N, N), np.inf)
for i in range(N):
    cnt = 0
    distance[i, i] = cnt
    places = A[i]
    while places:
        cnt += 1
        for k in places:
            distance[i, k] = min(distance[i, k], cnt)
            distance[k, i] = distance[i, k]
        places = list({k for place in places for k in A[place]})
    for k in range(i):
        if distance[i, k] != np.inf:
            distance[i] = np.minimum(distance[i, k] + distance[k], distance[i])
            distance[:, i] = distance[i]


#　色塗り
Q = int(input())
colors = np.zeros(N)

for k in range(Q):
  v, d, c = map(int, input().split())
  v -= 1
  colors[distance[v] <= d] = c
  

#　結果
for k in range(N):
  print(int(colors[k]))

Submission status: Runtime Error

Solution code:
import numpy as np 
N, M = map(int, input().split())

#　グラフの初期化
a = []
for k in range(N):
  a.append(set())
  A = np.array(a)

# グラフの作成
for k in range(M):
  a, b = map(int, input().split())
  A[a-1].update([b-1])
  A[b-1].update([a-1])

#　色塗り
Q = int(input())
colors = np.zeros(N)

for k in range(Q):
  v, d, c = map(int, input().split())
  v -= 1
  colors[v] = c
  v = [v]
  for length in range(d):
    s = set()
    for m in A[v]:
      s.update(m)
    v = list(s)
    colors[v] = c

#　結果
for k in range(M):
  print(int(colors[k]))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
N,M=map(int,input().split())
table=[[] for i in range(N)]
for i in range(M):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)
Q=int(input())
X=[[int(i) for i in input().split()] for i in range(Q)]
color=[0]*Q
dp=[[-1]*N for i in range(10)]
for i in range(Q-1,-1,-1):
    v, d, c=X[i]
    v=v-1
    color[i]=c
    dp[d][v]=i

for d in range(9,0,-1):
    for x in range(N):
        for y in table[x]:
            dp[d-1][y]=max(dp[d][x],dp[d-1][y])
for i in range(N):
    s=-1
    for d in range(10):
        s=max(s,dp[d][i])
    if s==-1:
        print(0)
    else:
        print(color[s])

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

dp = [[-1]*10 for i in range(N)]
mark = {}
Q = int(input())
col = [0]*(Q+1)
col[-1] = 0
C = []
for i in range(Q):
    v, d, c = map(int, input().split())
    col[i] = c
    C.append((v-1, d))

def dfs(v, d, idx):
    if idx < dp[v][d]:
        return
    dp[v][d] = idx
    if d:
        for w in G[v]:
            dfs(w, d-1, idx)

for i in range(Q-1, -1, -1):
    v, d = C[i]
    dfs(v, d, i)
print(*(col[max(e for e in dp[i])] for i in range(N)), sep='\n')

Submission status: Runtime Error

Solution code:
for t in range(1):
    graph = {}
    colors = {}
    n, m = list(map(int, input().split()))
    for i in range(m):
        a, b = list(map(int, input().split()))
        if a not in graph:
            graph[a] = [b]
        else:
            graph[a].append(b)
        if b not in graph:
            graph[b] = [a]
        else:
            graph[b].append(a)              #Generating the graph as a dictionary.
        colors[a] = 0
        colors[b] = 0                               #Generating a colors dictionary to record color of each vertex.
    for q in range(int(input())):
        v, d, c = list(map(int, input().split()))
        first, sec = [v], []
        colors[v] = c
        for level in range(d):               
            for i in first:
                for j in graph[i]:
                    sec.append(j)
                    colors[j] = c
            first, sec = sec, []                       #Loop to color all vertices within d distance of given vertex v with color c.
    for i in range(1, n+1):
        print(colors[i])# your code goes here

Submission status: Runtime Error

Solution code:
import scipy.sparse as sp
import numpy as np

def query(mat, v, d, c, color):
    acc1 = acc2 = sp.identity(n, format='csr')

    for i in range(d):
        acc1 *= mat
    #     acc2 += acc1

#    vec = acc2.getrow(v-1)
#    mask = vec.sign()
#    color -= color.multiply(mask)
#    color += c*mask
    return color

n, m = [int(x) for x in input().split()]
mat = sp.lil_matrix((n, n))

for i in range(m):
    a, b = [int(x) for x in input().split()]
    mat[a-1, b-1] = 1
    mat[b-1, a-1] = 1
mat = mat.tocsr()

q = int(input())
color = sp.csr_matrix([[0 for i in range(n)]])

for i in range(q):
    v,d,c = [int(x) for x in input().split()]
    color = query(mat, v, d, c, color)

for i in range(n):
    print(int(color[0,i]))

Submission status: Runtime Error

Solution code:
import scipy.sparse as sp
import numpy as np

def query(mat, v, d, c, color):
    acc1 = acc2 = sp.identity(n, format='csr')

    for i in range(1, d+1):
        acc1 *= mat
        acc2 += acc1

#    vec = acc2.getrow(v-1)
#    mask = vec.sign()
#    color -= color.multiply(mask)
#    color += c*mask
    return color

n, m = [int(x) for x in input().split()]
mat = sp.lil_matrix((n, n))

for i in range(m):
    a, b = [int(x) for x in input().split()]
    mat[a-1, b-1] = 1
    mat[b-1, a-1] = 1
mat = mat.tocsr()

q = int(input())
color = sp.csr_matrix([[0 for i in range(n)]])

for i in range(q):
    v,d,c = [int(x) for x in input().split()]
    color = query(mat, v, d, c, color)

for i in range(n):
    print(int(color[0,i]))