Problem p03045 (python)

Fixed submission:
def rt(i):
    while i!=G[i][0]:
        i = G[i][0]
    return i
N,M = map(int,input().split())
G = {i:[i,1] for i in range(1,N+1)}
for _ in range(M):
    X,Y,Z = map(int,input().split())
    rx = rt(X)
    ry = rt(Y)
    if rx!=ry:
        if G[rx][1]>=G[ry][1]:
            G[ry][0] = rx
            G[rx][1] += G[ry][1]
        else:
            G[rx][0] = ry
            G[ry][1] += G[rx][1]
cnt = 0
A = []
for i in range(1,N+1):
    A.append(rt(i))
A = list(set(A))
print(len(A))

Buggy submission (verdict: Runtime Error):
def clr(x):
    for y in G[x]:
        if hist[y]==0:
            hist[y]=1
            A.append(y)
            clr(y)
N,M = map(int,input().split())
G = {}
for _ in range(M):
    x,y,_ = map(int,input().split())
    if x not in G:
        G[x] = []
    G[x].append(y)
    if y not in G:
        G[y] = []
    G[y].append(x)
cnt = N-len(G)
hist = [0 for i in range(N+1)]
LG = {i:0 for i in list(G.keys())}
while LG:
    s = LG.popitem()[0]
    hist[s]=1
    A = []
    clr(s)
    for x in A:
        del LG[x]
    cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
def clr(x):
    for y in G[x]:
        if hist[y]==0:
            hist[y]=1
            A.append(y)
            clr(y)
N,M = map(int,input().split())
G = {}
for _ in range(M):
    x,y,_ = map(int,input().split())
    if x not in G:
        G[x] = []
    G[x].append(y)
    if y not in G:
        G[y] = []
    G[y].append(x)
cnt = N-len(G)
hist = [0 for i in range(N+1)]
while G:
    s = list(G.keys())[0]
    hist[s]=1
    A = [s]
    clr(s)
    for x in A:
        del G[x]
    cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
def col(i):
    for x in G[i]:
        if hist[x]==0:
            hist[x]=1
            A.remove(x)
            col(x)
N,M = map(int,input().split())
G = {}
for _ in range(M):
    x,y,z = map(int,input().split())
    if x not in G:
        G[x]=[]
    G[x].append(y)
    if y not in G:
        G[y]=[]
    G[y].append(x)
cnt = N-len(G)
hist = {i:0 for i in G.keys()}
A = list(hist.keys())
while A:
    s = A[0]
    hist[s]=1
    A.remove(s)
    col(s)
    cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
def col(i):
    for x in G[i]:
        if x in hist and hist[x]==0:
            hist[x]=1
            col(x)
N,M = map(int,input().split())
G = {}
for _ in range(M):
    x,y,z = map(int,input().split())
    if x not in G:
        G[x]=[]
    G[x].append(y)
    if y not in G:
        G[y]=[]
    G[y].append(x)
cnt = N-len(G)
while G:
    hist = {i:0 for i in G.keys()}
    s = list(G.keys())[0]
    hist[s]=1
    col(s)
    for i in hist.keys():
        if hist[i]==1:
            del G[i]
    cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
def col(i):
    for x in G[i]:
        if hist[x]==0:
            hist[x]=1
            col(x)
N,M = map(int,input().split())
G = {}
for _ in range(M):
    x,y,z = map(int,input().split())
    if x not in G:
        G[x]=[]
    G[x].append(y)
    if y not in G:
        G[y]=[]
    G[y].append(x)
cnt = N-len(G)
while G:
    hist = {i:0 for i in G.keys()}
    s = list(G.keys())[0]
    hist[s]=1
    col(s)
    for i in hist.keys():
        if hist[i]==1:
            del G[i]
    cnt += 1
print(cnt)