Problem p02573 (python)

Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = 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[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]    
      
def program():
  N, M = map(int, input().split())
  uf = UnionFind(N)
  for i in range(M):
    a, b = map(int, input().split())
    uf.union(a-1,b-1)
  
  ans = max([uf.size(i) for i in range(N)])
  return ans

print(program())

Buggy submission (verdict: Runtime Error):
N, M = [int(k) for k in input().split()]
nodes = {k:set() for k in range(1,N+1)}
visited = [False for i in range(N+2)]

for i in range(M):
  a, b = [int(k) for k in input().split()]
  nodes[a].add(b)
  nodes[b].add(a)
  
def dfs(i):
  visited[i] = True
  cnt = 1
  for j in nodes[i]:
    if not visited[j]:
      cnt += dfs(j)
      
  return cnt
  
   
ans = 1
if M > 0:
  for z in nodes:
    if not visited[z]:
      m = dfs(z)
      ans = max(m,ans)

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(k) for k in input().split()]
nodes = {k:set() for k in range(1,N+1)}
visited = [False for i in range(N+1)]

for i in range(M):
  a, b = [int(k) for k in input().split()]
  nodes[a].add(b)
  nodes[b].add(a)
  
def dfs(i):
  visited[i] = True
  cnt = 1
  for j in nodes[i]:
    if not visited[j]:
      cnt += dfs(j)
      
  return cnt
  
   
ans = 1
if M > 0:
  for i in nodes:
    if not visited[i]:
      ans = max(dfs(i),ans)

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(k) for k in input().split()]
nodes = {k:[] for k in range(1,N+1)}
visited = [False for i in range(N+1)]

for i in range(M):
  a, b = [int(k) for k in input().split()]
  nodes[a].append(b)
  nodes[b].append(a)
  
def dfs(i):
  visited[i] = True
  cnt = 1
  for j in nodes[i]:
    if not visited[j]:
      cnt += dfs(j)
      
  return cnt
   
ans = 0
for z in nodes:
  if not visited[z]:
    ans = max(dfs(z),ans)

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(k) for k in input().split()]
nodes = {k:set() for k in range(1,N+1)}
visited = [False for i in range(N+1)]

for i in range(M):
  a, b = [int(k) for k in input().split()]
  nodes[a].add(b)
  nodes[b].add(a)
  
def dfs(i):
  visited[i] = True
  cnt = 1
  for j in nodes[i]:
    if not visited[j]:
      cnt += dfs(j)
      
  return cnt
   
ans = 0
for z in nodes:
  if not visited[z]:
    ans = max(dfs(i),ans)

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(k) for k in input().split()]
nodes = {k:set() for k in range(1,N+1)}
visited = [False for i in range(N+1)]

for i in range(M):
  a, b = [int(k) for k in input().split()]
  nodes[a].add(b)
  nodes[b].add(a)
  
def dfs(i):
  visited[i] = True
  cnt = 1
  for j in nodes[i]:
    if not visited[j]:
      cnt += dfs(j)
      
  return cnt
  

   
ans = -1
if M == 0:
  ans = N
else:
  for i in nodes:
    if not visited[i]:
      ans = max(dfs(i),ans)

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(a) for a in input().split()]
lol = []
for i in range(M):
  a, b = [int(a) for a in input().split()]
  found_a = -10
  found_b = -10
  for j in range(len(lol)):
    if a in lol[j]:
      found_a = j
    if b in lol[j]:
      found_b = j
    if found_a >= 0 and found_b >= 0:
      break
  
  if found_a < 0 and found_b < 0:
    lol.append(set([a,b]))
  elif found_a >= 0 and found_b < 0:
    lol[found_a].add(b)
  elif found_b >= 0 and found_a < 0:
    lol[found_b].add(a)
  elif found_a >= 0 and found_b >=0 and found_a != found_b:
    lol[found_a] += lol[found_b]
    lol.pop(found_b)
    
ans = -1
for z in lol:
  if len(z) > ans:
    ans = len(z)

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(a) for a in input().split()]
lol = []
for i in range(M):
  a, b = [int(a) for a in input().split()]
  found_a = -10
  found_b = -10
  for j in range(len(lol)):
    if a in lol[j]:
      found_a = j
    if b in lol[j]:
      found_b = j
    if found_a >= 0 and found_b >= 0:
      break
  
  if found_a < 0 and found_b < 0:
    lol.append([a,b])
  elif found_a >= 0 and found_b < 0:
    lol[found_a].append(b)
  elif found_b >= 0 and found_a < 0:
    lol[found_b].append(a)
  elif found_a >= 0 and found_b >=0 and found_a != found_b:
    lol[found_a] += lol[found_b]
    lol.pop(found_b)
    
ans = max([len(z) for z in lol])

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(a) for a in input().split()]
lol = []
for i in range(M):
  a, b = [int(a) for a in input().split()]
  found_a = -10
  found_b = -10
  for j in range(len(lol)):
    if a in lol[j]:
      found_a = j
    if b in lol[j]:
      found_b = j
    if found_a >= 0 and found_b >= 0:
      break
  
  if found_a < 0 and found_b < 0:
    lol.append([a,b])
  elif found_a >= 0 and found_b < 0:
    lol[found_a].append(b)
  elif found_b >= 0 and found_a < 0:
    lol[found_b].append(a)
    
ans = max([len(z) for z in lol])

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(a) for a in input().split()]
lol = []
for i in range(M):
  a, b = [int(a) for a in input().split()]
  found_a = -10
  found_b = -10
  for j in range(len(lol)):
    if a in lol[j]:
      found_a = j
      break
  for j in range(len(lol)):
    if b in lol[j]:
      found_b = j
      break
  
  if found_a < 0 and found_b < 0:
    lol.append([a,b])
  elif found_a >= 0 and found_b < 0:
    lol[found_a].append(b)
  elif found_b >= 0 and found_a < 0:
    lol[found_b].append(a)
    
ans = max([len(z) for z in lol])

print(ans)