Problem p02762 (python)

Fixed submission:
import sys

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 same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

N,M,K = map(int,input().split())
friend =[0]*N
block = {}
for i in range(N):
  block[i] = []
uf = UnionFind(N)
for i in range(M):
  a,b = map(int,input().split())  
  a -= 1;b-=1
  friend[a]+=1
  friend[b]+=1
  uf.union(a,b)
  
for i in range(K):
  c,d = map(int,input().split())
  c -= 1
  d -= 1
  block[c].append(d)
  block[d].append(c)
res = [0]*N
for i in range(N):
  ans = uf.size(i)-1-friend[i]
  for u in block[i]:
    if uf.same(i,u):
      ans -= 1
      
  res[i] = ans
  
print(*res)

Buggy submission (verdict: Runtime Error):
def DFS(graph,start,mark_dict):
    mark_dict[start] = True
    for target in graph[start]:
      if target not in mark_dict:
        mark_dict[target] = True
        DFS(graph,target,mark_dict)

N,M,K = (int(x) for x in input().split())

friend = {}
for i in range(N):
  friend[i+1] = []
for i in range(M):
  a,b = (int(x) for x in input().split())
  friend[a].append(b)
  friend[b].append(a)

neighbor = [0]*N
for i in range(N):
  dic = {}
  DFS(friend,i+1,dic)
  neighbor[i] = len(dic)

for i in range(K):
  c,d = (int(x) for x in input().split())
  friend[c].append(d)
  friend[d].append(c)
  
for i in range(N):
  neighbor[i] -= len(friend[i+1]) + 1

print(' '.join(map(str, neighbor)))

Buggy submission (verdict: Runtime Error):
def DFS(graph,start,mark_dict):
    mark_dict[start] = True
    for target in graph[start]:
      if target not in mark_dict:
        mark_dict[target] = True
        DFS(graph,target,mark_dict)

N,M,K = (int(x) for x in input().split())

friend = {}
for i in range(N):
  friend[i+1] = []
for i in range(M):
  a,b = (int(x) for x in input().split())
  friend[a].append(b)
  friend[b].append(a)

neighbor = [0]*N
for i in range(N):
  dic = {}
  DFS(friend,i+1,dic)
  neighbor[i] = len(dic)

for i in range(K):
  c,d = (int(x) for x in input().split())
  friend[c].append(d)
  friend[d].append(c)
  
for i in range(N):
  neighbor[i] -= len(friend[i+1]) + 1

print(' '.join(map(str, neighbor)))

Buggy submission (verdict: Runtime Error):
5 10 0
1 2
1 3
1 4
1 5
3 2
2 4
2 5
4 3
5 3
4 5