Buggy python submission (verdict: Time Limit Exceeded):
from numba import jit
N, M = map(int, input().split())
def func(N, M):
  group = []
  tf = []
  for i in range(M):
    A, B = map(int, input().split())
    if A not in tf and B not in tf:
      tf.append(A)
      tf.append(B)
      group.append([A,B])
    else:
      if A in tf and B in tf:
        a, b = 0, 0
        for j in range(len(group)):
          if A in group[j]:
            a = j
            if b!=0:
              break
          elif B in group[j]:
            b = j
            if a!=0:
              break
        group[a].extend(group[b])
        del group[b]
      elif A not in tf:
        tf.append(A)
        for j in range(len(group)):
          if B in group[j]:
            group[j].append(A)
            break
      elif B not in tf:
        tf.append(B)
        for j in range(len(group)):
          if A in group[j]:
            group[j].append(B)
            break
  num = N - len(tf) 
  return len(group)+num-1
print(func(N,M))

Fixed submission:
# Connect Cities
# Union Findデータ構造（素集合データ構造）
 
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())
 
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
 
class UnionFind():
	"""docstring for UnionFind"""
	def __init__(self, n):
		self.parents = [-1]*n
 
	def find(self,x):
		if self.parents[x] < 0: #自分が親である時
			return x
		else: #自分が子供である時
			self.parents[x]=self.find(self.parents[x])
			return self.parents[x]
 
	def union(self,x,y):
		# 各要素の親要素を返す
		x=self.find(x)
		y=self.find(y)
 
		if x==y:
			return
		if self.parents[x] > self.parents[y]:
			x,y=y,x
 

		self.parents[y]=x

n,m=input2()
AB=[input_array() for _ in range(m)]
 
uf = UnionFind(n)
for ab in AB:
	a=ab[0]
	b=ab[1]
	uf.union(a-1,b-1)

ans=0
for i in uf.parents:
	if i < 0:
		ans+=1

print(ans-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
group = []
tf = []

for i in range(M):
  A, B = map(int, input().split())
  if A not in tf and B not in tf:
    tf.append(A)
    tf.append(B)
    group.append([A,B])
  else:
    if A in tf and B in tf:
      a, b = 0, 0
      for j in range(len(group)):
        if A in group[j]:
          a = j
          if b!=0:
            break
        elif B in group[j]:
          b = j
          if a!=0:
            break
      group[a].extend(group[b])
      del group[b]
    elif A not in tf:
      tf.append(A)
      for j in range(len(group)):
        if B in group[j]:
          group[j].append(A)
          break
    elif B not in tf:
      tf.append(B)
      for j in range(len(group)):
        if A in group[j]:
          group[j].append(B)
          break
num = N - len(tf) 
print(len(group)+num-1)

Fixed submission:
# Connect Cities
# Union Findデータ構造（素集合データ構造）
 
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())
 
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
 
class UnionFind():
	"""docstring for UnionFind"""
	def __init__(self, n):
		self.parents = [-1]*n
 
	def find(self,x):
		if self.parents[x] < 0: #自分が親である時
			return x
		else: #自分が子供である時
			self.parents[x]=self.find(self.parents[x])
			return self.parents[x]
 
	def union(self,x,y):
		# 各要素の親要素を返す
		x=self.find(x)
		y=self.find(y)
 
		if x==y:
			return
		if self.parents[x] > self.parents[y]:
			x,y=y,x
 

		self.parents[y]=x

n,m=input2()
AB=[input_array() for _ in range(m)]
 
uf = UnionFind(n)
for ab in AB:
	a=ab[0]
	b=ab[1]
	uf.union(a-1,b-1)

ans=0
for i in uf.parents:
	if i < 0:
		ans+=1

print(ans-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
group = []
tf = []
for i in range(M):
  A, B = map(int, input().split())
  if A not in tf and B not in tf:
    tf.append(A)
    tf.append(B)
    group.append([A,B])
  else:
    if A in tf and B in tf:
      for j in range(len(group)):
        if A in group[j]:
          a = j
        elif B in group[j]:
          b = j
      group[a].extend(group[b])
      del group[b]
    elif A not in tf:
      tf.append(A)
      for j in range(len(group)):
        if B in group[j]:
          group[j].append(A)
          break
    elif B not in tf:
      tf.append(B)
      for j in range(len(group)):
        if A in group[j]:
          group[j].append(B)
          break

l = list(range(1,N+1))
for i in tf:
  if i in l:
    l.remove(i)
    
print(len(group)+len(l)-1)

Fixed submission:
# Connect Cities
# Union Findデータ構造（素集合データ構造）
 
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())
 
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
 
class UnionFind():
	"""docstring for UnionFind"""
	def __init__(self, n):
		self.parents = [-1]*n
 
	def find(self,x):
		if self.parents[x] < 0: #自分が親である時
			return x
		else: #自分が子供である時
			self.parents[x]=self.find(self.parents[x])
			return self.parents[x]
 
	def union(self,x,y):
		# 各要素の親要素を返す
		x=self.find(x)
		y=self.find(y)
 
		if x==y:
			return
		if self.parents[x] > self.parents[y]:
			x,y=y,x
 

		self.parents[y]=x

n,m=input2()
AB=[input_array() for _ in range(m)]
 
uf = UnionFind(n)
for ab in AB:
	a=ab[0]
	b=ab[1]
	uf.union(a-1,b-1)

ans=0
for i in uf.parents:
	if i < 0:
		ans+=1

print(ans-1)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
group = []
tf = []
for i in range(M):
  A, B = map(int, input().split())
  if A and B not in tf:
    tf.append(A)
    tf.append(B)
    group.append([A,B])
  else:
    if A not in tf:
      tf.append(A)
      for j in range(len(group)):
        if B in group[j]:
          group[i].append(A)
          break
    else:
      tf.append(B)
      for j in range(len(group)):
        if A in group[j]:
          group[i].append(B)
          break
l = list(range(1,N+1))
for i in tf:
  if i in l:
    l.remove(i)
    
print(len(group)+len(l)-1)

Fixed submission:
# Connect Cities
# Union Findデータ構造（素集合データ構造）
 
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())
 
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))
 
class UnionFind():
	"""docstring for UnionFind"""
	def __init__(self, n):
		self.parents = [-1]*n
 
	def find(self,x):
		if self.parents[x] < 0: #自分が親である時
			return x
		else: #自分が子供である時
			self.parents[x]=self.find(self.parents[x])
			return self.parents[x]
 
	def union(self,x,y):
		# 各要素の親要素を返す
		x=self.find(x)
		y=self.find(y)
 
		if x==y:
			return
		if self.parents[x] > self.parents[y]:
			x,y=y,x
 

		self.parents[y]=x

n,m=input2()
AB=[input_array() for _ in range(m)]
 
uf = UnionFind(n)
for ab in AB:
	a=ab[0]
	b=ab[1]
	uf.union(a-1,b-1)

ans=0
for i in uf.parents:
	if i < 0:
		ans+=1

print(ans-1)