Problem p03045 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
N,M = list(map(int,input().split()))
XYZ = [list(map(int,input().split())) for _ in range(M)]

G = [[] for _ in range(N)]
 
for i in XYZ:
    G[i[0]-1].append(i[1])
    G[i[1]-1].append(i[0])
 
check = [False for _ in range(N)]
 
def DFS(G,s):
    check[s-1] = True
    for i in G[s-1]:
        if not check[i-1]:         
            DFS(G,i)
ans = 0
for i in range(N):
    if not check[i]:
        DFS(G,i+1)
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N,M = list(map(int,input().split()))
XYZ = [list(map(int,input().split())) for _ in range(M)]

G = [[] for _ in range(N)]
 
for i in XYZ:
    G[i[0]-1].append(i[1])
    G[i[1]-1].append(i[0])
 
check = [False for _ in range(N)]

def DFS(G,s):
    check[s-1] = True
    for i in G[s-1]:
        if not check[i-1]:         
            DFS(G,i)
ans = 0
for i in range(N):
    if not check[i]:
        DFS(G,i+1)
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = [0] * M
Y = [0] * M
Z = [0] * M 
for i in range(M):
  X[i], Y[i], Z[i] = map(int, input().split())

G = [[] for _ in range(N)]

for i in range(M) :
    G[Y[i]-1].append(X[i])
    G[X[i]-1].append(Y[i])

c = [False for _ in range(N)]
 
def DFS(G,s):
    c[s-1] = True
    for i in G[s-1]:
        if not c[i-1]:         
            DFS(G,i)
ans = 0
for i in range(N):
    if not c[i]:
        DFS(G,i+1)
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
XYZ = [list(map(int,input().split())) for _ in range(M)]

reach = [[] for _ in range(N)]
 
for i in XYZ:
    reach[i[0]-1].append(i[1])
    reach[i[1]-1].append(i[0])

approach = [False for _ in range(N)]

def change(reach, s):       
    approach[s-1] = True
    for i in reach[s-1]:
        if not approach[i-1] :
            change(reach, i)

ans = 0
for i in range(N) :
    if not approach[i]:
        change(reach, i+1)
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = [0] * M
Y = [0] * M
Z = [0] * M 
for i in range(M):
  X[i], Y[i], Z[i] = map(int, input().split())

reach = [[] for _ in range(N)]

for i in range(M) :
    reach[Y[i]-1].append(X[i])
    reach[X[i]-1].append(Y[i])

approach = [False for _ in range(N)]

def change(reach, s):       
    approach[s-1] = True
    for i in reach[s-1]:
        if not approach[i-1] :
            change(reach, i)

ans = 0
for i in range(N) :
    if not approach[i]:
        change(reach, i+1)
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = [0] * M
Y = [0] * M
Z = [0] * M 
unique = []
count = 0
for i in range(M):
  X[i], Y[i], Z[i] = map(int, input().split())

reach = [[] for _ in range(N)]
for i in range(M) :
    reach[Y[i]-1].append(X[i])
    reach[X[i]-1].append(Y[i])

approach = [False for _ in range(N)]

ans = 0

def change(reach, s):       
    approach[s-1] = True
    for i in reach[s-1]:
        if not approach[i-1] :
            change(reach, i)

for i in range(N) :
    if not approach[i]:
        change(reach, i+1)
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = [0] * M
Y = [0] * M
Z = [0] * M 
unique = []
count = 0
for i in range(M):
  X[i], Y[i], Z[i] = map(int, input().split())

reach = [[] for _ in range(N)]
for i in range(M) :
    reach[Y[i]-1].append(X[i])
    reach[X[i]-1].append(Y[i])

approach = [False for _ in range(N)]

ans = 0

def change(reach, s):       
    approach[s-1] = True
    for i in reach[s-1]:
        if not approach[i-1] :
            change(reach, i)

for i in range(N) :
    if not approach[i]:
        change(reach, i)
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = [0] * M
Y = [0] * M
Z = [0] * M 
unique = []
count = 0
for i in range(M):
  X[i], Y[i], Z[i] = map(int, input().split())

reach = [[] for _ in range(N)]
for i in range(M) :
    reach[Y[i]-1].append(X[i])
    reach[X[i]-1].append(Y[i])

approach = [False for _ in range(N)]

i = 1

ans = 0

def change(reach, s):       
    approach[s-1] = True
    for i in reach[s-1]:
        if not approach[i-1] :
            change(reach, i)

for i in range(N) :
    if not approach[i]:
        change(reach, i)
        ans += 1

print(ans)