Buggy python 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)    

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

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

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

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


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 python submission (verdict: Time Limit Exceeded):
import heapq
INFTY = 10**10
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:
    K = len(G)
    d = {i:INFTY for i in G.keys()}
    k = list(G.keys())[0]
    d[k]=0
    hist = {i:0 for i in G.keys()}
    heap = [(0,k)]
    while heap:
        cur = heapq.heappop(heap)
        hist[cur[1]] = 1
        for x in G[cur[1]]:
            if hist[x]==0:
                if d[cur[1]]+1<d[x]:
                    d[x] = d[cur[1]]+1
                    heapq.heappush(heap,(d[x],x))
    A = list(G.keys())
    for i in A:
        if hist[i]==1:
            del G[i]
    cnt += 1
print(cnt)

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