Problem p03045 (python)

Fixed submission:
import queue
import sys
sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
XYZ = []
for _ in range(M):
    XYZ.append(tuple(map(int, input().split())))

G=[[] for _ in range(N+1)]
for el in XYZ:
    G[el[0]].append(el[1])
    G[el[1]].append(el[0])
#print(G)

seen=[False]*(N+1)
todo=queue.Queue()
def bfs(n):
    seen[n]=True
    for nxt in G[n]:
        if seen[nxt]==False:
            bfs(nxt)


cnt=0
for check in range(1, N+1):
    if seen[check]==False:
        cnt+=1
        bfs(check)
print(cnt)

Buggy submission (verdict: Runtime Error):
import networkx as nx

G = nx.Graph()

N, M = map(int, input().split())

G.add_nodes_from(range(1, N + 1))
for _ in range(M):
    x, y, z = map(int, input().split())
    G.add_edge(y, z)

cnt = 0
for c in  nx.connected_components(G):
        cnt += 1
print(cnt)

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

G = [[] for _ in range(N + 1)]
for el in XYZ:
    x, y, z = el
    G[x].append(y)
    G[y].append(x)

seen=[False] * (N + 1)
def dfs(parent):
    seen[parent] = True
    for child in G[parent]:
        if seen[child] == False:
            dfs(child)

cnt = 0
for start in range(1, N + 1):
    if seen[start] == False:
        dfs(start)
        cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
import queue
import sys
sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
XYZ = []
for _ in range(M):
    XYZ.append(tuple(map(int, input().split())))

G=[[] for _ in range(N+1)]
for el in XYZ:
    G[el[0]].append(el[1])
    G[el[1]].append(el[0])
#print(G)

not_seen=list(range(1,N+1))
todo=queue.Queue()
def dfs(n):
    if n in not_seen:
        not_seen.remove(n)
    for nxt in G[n]:
        if nxt in not_seen:
            todo.put(nxt)
    if todo.empty():
        return None
    else:
        dfs(todo.get())


cnt=0
while not_seen:
    cnt+=1
    dfs(not_seen[0])
print(cnt)
import queue
import sys
sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
XYZ = []
for _ in range(M):
    XYZ.append(tuple(map(int, input().split())))

G=[[] for _ in range(N+1)]
for el in XYZ:
    G[el[0]].append(el[1])
    G[el[1]].append(el[0])
#print(G)

not_seen=list(range(1,N+1))
todo=queue.Queue()
def dfs(n):
    if n in not_seen:
        not_seen.remove(n)
    for nxt in G[n]:
        if nxt in not_seen:
            todo.put(nxt)
    if todo.empty():
        return None
    else:
        dfs(todo.get())


cnt=0
while not_seen:
    cnt+=1
    dfs(not_seen[0])
print(cnt)

Buggy submission (verdict: Runtime Error):
import queue

N, M = map(int, input().split())
XYZ = []
for _ in range(M):
    XYZ.append(tuple(map(int, input().split())))

G=[[] for _ in range(N+1)]
for el in XYZ:
    G[el[0]].append(el[1])
    G[el[1]].append(el[0])
#print(G)

seen=[False]*(N+1)
todo=queue.Queue()
def dfs(n):
    seen[n]=True
    for nxt in G[n]:
        if seen[nxt]==False:
            todo.put(nxt)
    if todo.empty():
        return None
    else:
        dfs(todo.get())


cnt=0
for check in range(1, N+1):
    if seen[check]==False:
        cnt+=1
        dfs(check)
print(cnt)