Problem p02762 (python)

Fixed submission:
class unionFind():
  def __init__(self,n):
    self.size = 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 grpsize(self, x):
    return -self.parents[self.find(x)]
  def roots(self):
    return [i for i, x in enumerate(self.parents) if x < 0]
  def members(self, x):
    root = self.find(x)
    return [i for i in range(self.size) if self.find(i) == root]
  def __str__(self):
    return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
      
import sys
sys.setrecursionlimit(4100000)

line = input().split()
nPerson = int(line[0])
nFriend = int(line[1])
nBlock  = int(line[2])

friends = [[] for i in range(nFriend)]
for i in range(nFriend):
  line = input().split()
  friends[i] =[int(line[0])-1, int(line[1])-1]

nconnected = [0]*nPerson
for i in friends:
    nconnected[i[0]] += 1
    nconnected[i[1]] += 1
  
  
blocks = [[] for i in range(nBlock)]
for i in range(nBlock):
  line = input().split()
  blocks[i] = [int(line[0])-1, int(line[1])-1]

uf = unionFind(nPerson)
for i in friends:
  uf.union(i[0],i[1])

nblocked = [0]*nPerson
for i in blocks:
  if uf.find(i[0]) == uf.find(i[1]):
    nblocked[i[0]] += 1
    nblocked[i[1]] += 1
  
res = ""
for i in range(nPerson):
  res += str(uf.grpsize(i)-nconnected[i] -nblocked[i]-1) + " "

print(res)

Buggy submission (verdict: Runtime Error):
line = input().split()
nPerson = int(line[0])
nFriend = int(line[1])
nBlock  = int(line[2])
 
friends = [[] for i in range(nFriend)]
for i in range(nFriend):
  line = input().split()
  friends[i] =[int(line[0]), int(line[1]) ]
 
blocks = [[] for i in range(nBlock)]
for i in range(nBlock):
  line = input().split()
  blocks[i] = [int(line[0]), int(line[1]) ]
  
connected = [[] for i in range(nPerson+1)]
  
for i in friends:
  connected[i[0]].append(i[1])
  connected[i[1]].append(i[0])
 
blocked = [[] for i in range(nPerson+1)]
for i in blocks:
  blocked[i[0]].append(i[1])
  blocked[i[1]].append(i[0])
  
for i in range(len(connected)):
  connected[i] = set(connected[i])
for i in range(len(blocked)):
  blocked[i] = set(blocked[i])
  
nCand = [0]*(nPerson+1)
 
def findCand(target,curPer):
  if visited[curPer] == True :
    return
  visited[curPer] = True
  if curPer not in connected[target] and curPer not in blocked[target]:
    nCand[target] += 1
  for i in connected[curPer]:
    findCand(target,i)
 
for i in range(1,nPerson+1):
  visited = [False]*(nPerson+1)
  visited[i] = True
  for j in connected[i]:
    findCand(i,j)
 
res = ""
for i in range(1,nPerson+1):
  res += str(nCand[i]) + " "
  
print(res

Buggy submission (verdict: Runtime Error):
line = input().split()
nPerson = int(line[0])
nFriend = int(line[1])
nBlock  = int(line[2])

friends = [[] for i in range(nFriend)]
for i in range(nFriend):
  line = input().split()
  friends[i] =[int(line[0]), int(line[1]) ]

blocks = [[] for i in range(nBlock)]
for i in range(nBlock):
  line = input().split()
  blocks[i] = [int(line[0]), int(line[1]) ]
  
connected = [[] for i in range(nPerson+1)]
  
for i in friends:
  connected[i[0]].append(i[1])
  connected[i[1]].append(i[0])

blocked = [[] for i in range(nPerson+1)]
for i in blocks:
  blocked[i[0]].append(i[1])
  blocked[i[1]].append(i[0])
  
for i in range(len(connected)):
  connected[i] = set(connected[i])
for i in range(len(blocked)):
  blocked[i] = set(blocked[i])
  
nCand = [0]*(nPerson+1)

def findCand(target,curPer):
  if visited[curPer] == True :
    return
  visited[curPer] = True
  if curPer not in connected[target] and curPer not in blocked[target]:
    nCand[target] += 1
  for i in connected[curPer]:
    findCand(target,i)

for i in range(1,nPerson+1):
  visited = [False]*(nPerson+1)
  visited[i] = True
  for j in connected[i]:
    findCand(i,j)

res = ""
for i in range(1,nPerson+1):
  res += str(nCand[i]) + " "
  
print(res)

Buggy submission (verdict: Runtime Error):
line = input().split()
nPerson = int(line[0])
nFriend = int(line[1])
nBlock  = int(line[2])

friends = []
for i in range(nFriend):
  line = input().split()
  friends.append(( int(line[0]), int(line[1]) ))

blocks = []
for i in range(nBlock):
  line = input().split()
  blocks.append(( int(line[0]), int(line[1]) ))
  
connected = [[] for i in range(nPerson+1)]
  
for i in friends:
  connected[i[0]].append(i[1])
  connected[i[1]].append(i[0])

blocked = [[] for i in range(nPerson+1)]
for i in blocks:
  blocked[i[0]].append(i[1])
  blocked[i[1]].append(i[0])
  
for i in range(len(connected)):
  connected[i] = set(connected[i])
for i in range(len(blocked)):
  blocked[i] = set(blocked[i])
  
nCand = [0]*(nPerson+1)

def findCand(target,curPer):
  if visited[curPer] == True :
    return
  visited[curPer] = True
  if curPer not in connected[target] and curPer not in blocked[target]:
    nCand[target] += 1
  for i in connected[curPer]:
    findCand(target,i)

for i in range(1,nPerson+1):
  visited = [False]*(nPerson+1)
  visited[i] = True
  for j in connected[i]:
    findCand(i,j)

res = ""
for i in range(1,nPerson+1):
  res += str(nCand[i]) + " "
  
print(res)