Problem p03768 (python)

Fixed submission:
from collections import deque
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)]

color = [0]*(n+1)
used = [-1]*(n+1)

for v,d,c in Q[::-1]:
    if used[v] > d:
        continue
    
    q = deque([[v,d]])
    while q:
        now, dis = q.popleft()
        if dis < 0:
            continue
        if color[now] == 0:
            color[now] = c
        for nex in e[now]:
            if used[nex] < dis-1:
                used[nex] = dis-1
                q.append([nex,dis-1])
    
for i in color[1:]:
    print(i)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)