Buggy python submission (verdict: Time Limit Exceeded):
def dfs(g, start):
    global visit
    visiting = [start]
    while visiting:
        v = visiting.pop()
        visit[v] = True
        for v2 in g.get(v,()):
            if v2 in visit or v2 in visiting:
                continue
            visiting.append(v2)

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

visit = {}
g = {}
for v in range(1,N+1):
    g[v] = []

for _ in range(M):
    x,y,z = map(int, input().split())
    g[x].append(y)
    g[y].append(x)

count = 0
for v in range(1,N+1):
    if v in visit:
        continue
    dfs(g,v)
    count += 1

print(count)


Fixed submission:
N, M = map(int, input().split())
graph = [[] for i in range(N)]
for i in range(M):
    x, y, z = map(int, input().split())
    x, y = x-1, y-1
    graph[x].append(y)
    graph[y].append(x)
visited = [False for i in range(N)]
def dfs(v):
    Q = []
    visited[v] = True
    Q.append(v)
    while len(Q) > 0:
        v = Q.pop()
        for u in graph[v]:
            if not visited[u]:
                visited[u] = True
                Q.append(u)

count = 0
for i in range(N):
    if not visited[i]:
        dfs(i)
        count += 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
def dfs(start):
    global visit
    visiting = [start]
    while visiting:
        v = visiting.pop()
        visit[v] = True
        for v2 in g.get(v,()):
            if v2 in visit or v2 in visiting:
                continue
            visiting.append(v2)

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

visit = {}
g = {}
for v in range(1,N+1):
    g[v] = []

for _ in range(M):
    x,y,z = map(int, input().split())
    g[x].append(y)
    g[y].append(x)

count = 0
for v in range(1,N+1):
    if v in visit:
        continue
    dfs(v)
    count += 1

print(count)


Fixed submission:
N, M = map(int, input().split())
graph = [[] for i in range(N)]
for i in range(M):
    x, y, z = map(int, input().split())
    x, y = x-1, y-1
    graph[x].append(y)
    graph[y].append(x)
visited = [False for i in range(N)]
def dfs(v):
    Q = []
    visited[v] = True
    Q.append(v)
    while len(Q) > 0:
        v = Q.pop()
        for u in graph[v]:
            if not visited[u]:
                visited[u] = True
                Q.append(u)

count = 0
for i in range(N):
    if not visited[i]:
        dfs(i)
        count += 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
def dfs(g, start):
    global visit
    visiting = [start]
    while visiting:
        v = visiting.pop()
        visit[v] = True
        for v2 in g.get(v,()):
            if v2 in visit or v2 in visiting:
                continue
            visiting.append(v2)

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

visit = {}
g = {}
for v in range(1,N+1):
    g[v] = []

for _ in range(M):
    x,y,z = map(int, input().split())
    g[x].append(y)
    g[y].append(x)

count = 0
for v in range(1,N+1):
    if v in visit:
        continue
    dfs(g,v)
    count += 1

print(count)


Fixed submission:
N, M = map(int, input().split())
graph = [[] for i in range(N)]
for i in range(M):
    x, y, z = map(int, input().split())
    x, y = x-1, y-1
    graph[x].append(y)
    graph[y].append(x)
visited = [False for i in range(N)]
def dfs(v):
    Q = []
    visited[v] = True
    Q.append(v)
    while len(Q) > 0:
        v = Q.pop()
        for u in graph[v]:
            if not visited[u]:
                visited[u] = True
                Q.append(u)

count = 0
for i in range(N):
    if not visited[i]:
        dfs(i)
        count += 1
print(count)