Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())

adj={i: set() for i in range(1,N+1)}
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x].add(y)
  adj[y].add(x)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(1,N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Fixed submission:
import sys
mod = 10**9+7
sys.setrecursionlimit(mod)
N,M=map(int, input().split())

adj={i: set() for i in range(1,N+1)}
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x].add(y)
  adj[y].add(x)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(1,N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())

adj=[set() for i in range(N)]
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x-1].add(y-1)
  adj[y-1].add(x-1)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(N):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Fixed submission:
import sys
mod = 10**9+7
sys.setrecursionlimit(mod)
N,M=map(int, input().split())

adj={i: set() for i in range(1,N+1)}
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x].add(y)
  adj[y].add(x)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(1,N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())

adj=dict()
for i in range(M):
  x,y,z=map(int, input().split())
  if x in adj: adj[x].add(y)
  else: adj[x]={y}
  if y in adj: adj[y].add(x)
  else: adj[y]={x}
#print(outgoing)
#print(incoming)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    if v in adj:
      for u in adj[v]: dfs(u)
cnt=0
for i in range(1, N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Fixed submission:
import sys
mod = 10**9+7
sys.setrecursionlimit(mod)
N,M=map(int, input().split())

adj={i: set() for i in range(1,N+1)}
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x].add(y)
  adj[y].add(x)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(1,N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())

outgoing=dict()
incoming=dict()
for i in range(M):
  x,y,z=map(int, input().split())
  if x in outgoing: outgoing[x].add(y)
  else: outgoing[x]={y}
  if y in incoming: incoming[y].add(x)
  else: incoming[y]={x}
#print(outgoing)
#print(incoming)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    if v in outgoing:
      for u in outgoing[v]: dfs(u)
    if v in incoming:
      for u in incoming[v]: dfs(u)
cnt=0
for i in range(1, N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Fixed submission:
import sys
mod = 10**9+7
sys.setrecursionlimit(mod)
N,M=map(int, input().split())

adj={i: set() for i in range(1,N+1)}
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x].add(y)
  adj[y].add(x)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(1,N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())

outgoing=dict()
incoming=dict()
for i in range(M):
  x,y,z=map(int, input().split())
  if x in outgoing: outgoing[x].add(y)
  else: outgoing[x]={y}
  if y in incoming: incoming[y].add(x)
  else: incoming[y]={x}
#print(outgoing)
#print(incoming)

V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    og=set() if v not in outgoing else outgoing[v]
    ic=set() if v not in incoming else incoming[v]
    for u in og.union(ic):
      dfs(u)

cnt=0
for v in range(1, N+1):
  if v not in V:
    cnt += 1
    dfs(v)
print(cnt)

Fixed submission:
import sys
mod = 10**9+7
sys.setrecursionlimit(mod)
N,M=map(int, input().split())

adj={i: set() for i in range(1,N+1)}
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x].add(y)
  adj[y].add(x)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(1,N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())

L=dict()

for i in range(M):
  (x,y,z)=map(int, input().split())
  if x not in L:
    L[x] = [y]
  else:
    L[x].append(y)
  if y not in L:
    L[y] = [x]
  else:
    L[y].append(x)

#print(L)
S=set()
def dfs(a):
  if a in S: return
  S.add(a)
  if a in L: 
    for b in L[a]:
      dfs(b)

cnt = 0
for i in range(1, N+1):
  if i not in S:
    cnt += 1
    dfs(i)
print(cnt)

Fixed submission:
import sys
mod = 10**9+7
sys.setrecursionlimit(mod)
N,M=map(int, input().split())

adj={i: set() for i in range(1,N+1)}
for i in range(M):
  x,y,z=map(int, input().split())
  adj[x].add(y)
  adj[y].add(x)
V=set()
def dfs(v):
  if v not in V:
    V.add(v)
    for u in adj[v]: dfs(u)
cnt=0
for i in range(1,N+1):
  if i not in V:
    cnt += 1
    dfs(i)
print(cnt)