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

parent=[i for i in range(N)]
depth=[0 for i in range(N)]

def root(x):
  if parent[x]==x:
    return x
  else:
    return root(parent[x])

def same(x,y):
  return root(x)==root(y)

def unite(x,y):
  rx=root(x)
  ry=root(y)
  if rx==ry:
    return
  if depth[x]<depth[y]:
    parent[rx]=ry
  else:
    parent[ry]=rx
    if depth[x]==depth[y]:
      depth[x]+=1
      
for i in range(M):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  unite(a,b)
  
roots=set()
for i in range(N):
  roots.add(root(i))

print(len(roots)-1)

Fixed submission:
n,m=map(int,input().split())

parent=[i for i in range(n)]
rank=[0 for i in range(n)]

def find(a):
  if parent[a]==a:
    return a
  else:
    return find(parent[a])

def union(a,b):
  ar=find(a)
  br=find(b)
  if ar==br:
    return
  if rank[a]>rank[b]:
    parent[br]=ar
  else:
    parent[ar]=br
    if rank[a]==rank[b]:
      rank[a]+=1
      
def same(a,b):
  ar=find(a)
  br=find(b)
  return ar==br

q=[None]*m

for i in range(m):
  x,y,z=map(int,input().split())
  q[i]=[x-1,y-1]

for i in range(len(q)):
  union(q[i][0],q[i][1])
  
roots=[0]*len(parent)
for i in range(len(parent)):
  roots[i]=find(i)

print(len(set(roots)))

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
  n,m=map(int,input().split())
  res=[0]*n

  datas=[None]*m
  for i in range(m):
    x,y,z=map(int,input().split())
    datas[i]=[min(x-1,y-1),max(x-1,y-1)]

  datas=sorted(datas,key=lambda x:x[0])

  maxi=0
  for i in range(len(datas)):
    d=datas[i]
    if res[d[0]]==0 and res[d[1]]==0:
      maxi+=1
      res[d[0]]=maxi
      res[d[1]]=maxi
    elif res[d[0]]==0 and res[d[1]]!=0:
      res[d[0]]=res[d[1]]
    elif res[d[0]]!=0 and res[d[1]]==0:
      res[d[1]]=res[d[0]]
    elif res[d[0]]!=res[d[1]]:
      cor=min(res[d[0]],res[d[1]])
      dum=max(res[d[0]],res[d[1]])
      res[d[0]],res[d[1]]=cor,cor
      for i in range(len(res)):
        if res[i]==dum:
          res[i]=cor

  count0=res.count(0)
  ress=set(res)

  print(count0 + len(ress) - (1 if 0 in ress else 0))
  
solve()


Fixed submission:
n,m=map(int,input().split())

parent=[i for i in range(n)]
rank=[0 for i in range(n)]

def find(a):
  if parent[a]==a:
    return a
  else:
    return find(parent[a])

def union(a,b):
  ar=find(a)
  br=find(b)
  if ar==br:
    return
  if rank[a]>rank[b]:
    parent[br]=ar
  else:
    parent[ar]=br
    if rank[a]==rank[b]:
      rank[a]+=1
      
def same(a,b):
  ar=find(a)
  br=find(b)
  return ar==br

q=[None]*m

for i in range(m):
  x,y,z=map(int,input().split())
  q[i]=[x-1,y-1]

for i in range(len(q)):
  union(q[i][0],q[i][1])
  
roots=[0]*len(parent)
for i in range(len(parent)):
  roots[i]=find(i)

print(len(set(roots)))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
res=[0]*n

datas=[None]*m
for i in range(m):
  x,y,z=map(int,input().split())
  datas[i]=[min(x-1,y-1),max(x-1,y-1)]

datas=sorted(datas,key=lambda x:x[0])

maxi=0
for i in range(len(datas)):
  d=datas[i]
  if res[d[0]]==0 and res[d[1]]==0:
    maxi+=1
    res[d[0]]=maxi
    res[d[1]]=maxi
  elif res[d[0]]==0 and res[d[1]]!=0:
    res[d[0]]=res[d[1]]
  elif res[d[0]]!=0 and res[d[1]]==0:
    res[d[1]]=res[d[0]]
  elif res[d[0]]!=res[d[1]]:
    cor=min(res[d[0]],res[d[1]])
    dum=max(res[d[0]],res[d[1]])
    res[d[0]],res[d[1]]=cor,cor
    for i in range(len(res)):
      if res[i]==dum:
        res[i]=cor

count0=res.count(0)
ress=set(res)

print(count0 + len(ress) - (1 if 0 in ress else 0))


Fixed submission:
n,m=map(int,input().split())

parent=[i for i in range(n)]
rank=[0 for i in range(n)]

def find(a):
  if parent[a]==a:
    return a
  else:
    return find(parent[a])

def union(a,b):
  ar=find(a)
  br=find(b)
  if ar==br:
    return
  if rank[a]>rank[b]:
    parent[br]=ar
  else:
    parent[ar]=br
    if rank[a]==rank[b]:
      rank[a]+=1
      
def same(a,b):
  ar=find(a)
  br=find(b)
  return ar==br

q=[None]*m

for i in range(m):
  x,y,z=map(int,input().split())
  q[i]=[x-1,y-1]

for i in range(len(q)):
  union(q[i][0],q[i][1])
  
roots=[0]*len(parent)
for i in range(len(parent)):
  roots[i]=find(i)

print(len(set(roots)))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
res=[0]*n

datas=[None]*m
for i in range(m):
  x,y,z=map(int,input().split())
  datas[i]=[min(x-1,y-1),max(x-1,y-1)]

datas=sorted(datas,key=lambda x:x[0])

maxi=0
for i in range(len(datas)):
  d=datas[i]
  if res[d[0]]==0 and res[d[1]]==0:
    maxi+=1
    res[d[0]]=maxi
    res[d[1]]=maxi
  elif res[d[0]]==0 and res[d[1]]!=0:
    res[d[0]]=res[d[1]]
  elif res[d[0]]!=0 and res[d[1]]==0:
    res[d[1]]=res[d[0]]
  elif res[d[0]]!=res[d[1]]:
    cor=min(res[d[0]],res[d[1]])
    dum=max(res[d[0]],res[d[1]])
    res[d[0]],res[d[1]]=cor,cor
    for i in range(len(res)):
      if res[i]==dum:
        res[i]=cor

count0=res.count(0)
ress=set(res)

print(count0 + len(ress) - (1 if 0 in ress else 0))


Fixed submission:
n,m=map(int,input().split())

parent=[i for i in range(n)]
rank=[0 for i in range(n)]

def find(a):
  if parent[a]==a:
    return a
  else:
    return find(parent[a])

def union(a,b):
  ar=find(a)
  br=find(b)
  if ar==br:
    return
  if rank[a]>rank[b]:
    parent[br]=ar
  else:
    parent[ar]=br
    if rank[a]==rank[b]:
      rank[a]+=1
      
def same(a,b):
  ar=find(a)
  br=find(b)
  return ar==br

q=[None]*m

for i in range(m):
  x,y,z=map(int,input().split())
  q[i]=[x-1,y-1]

for i in range(len(q)):
  union(q[i][0],q[i][1])
  
roots=[0]*len(parent)
for i in range(len(parent)):
  roots[i]=find(i)

print(len(set(roots)))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
res=[0]*n

datas=[None]*m
for i in range(m):
  x,y,z=map(int,input().split())
  datas[i]=[min(x-1,y-1),max(x-1,y-1)]

maxi=0
for i in range(len(datas)):
  d=datas[i]
  if res[d[0]]==0 and res[d[1]]==0:
    maxi+=1
    res[d[0]]=maxi
    res[d[1]]=maxi
  elif res[d[0]]==0 and res[d[1]]!=0:
    res[d[0]]=res[d[1]]
  elif res[d[0]]!=0 and res[d[1]]==0:
    res[d[1]]=res[d[0]]
  elif res[d[0]]!=res[d[1]]:
    cor=min(res[d[0]],res[d[1]])
    dum=max(res[d[0]],res[d[1]])
    res[d[0]],res[d[1]]=cor,cor
    for i in range(len(res)):
      if res[i]==dum:
        res[i]=cor

count0=res.count(0)
ress=set(res)

print(count0 + len(ress) - (1 if 0 in ress else 0))


Fixed submission:
n,m=map(int,input().split())

parent=[i for i in range(n)]
rank=[0 for i in range(n)]

def find(a):
  if parent[a]==a:
    return a
  else:
    return find(parent[a])

def union(a,b):
  ar=find(a)
  br=find(b)
  if ar==br:
    return
  if rank[a]>rank[b]:
    parent[br]=ar
  else:
    parent[ar]=br
    if rank[a]==rank[b]:
      rank[a]+=1
      
def same(a,b):
  ar=find(a)
  br=find(b)
  return ar==br

q=[None]*m

for i in range(m):
  x,y,z=map(int,input().split())
  q[i]=[x-1,y-1]

for i in range(len(q)):
  union(q[i][0],q[i][1])
  
roots=[0]*len(parent)
for i in range(len(parent)):
  roots[i]=find(i)

print(len(set(roots)))