Problem p02762 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()

#https://www.slideshare.net/chokudai/union-find-49066733
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.parents = [-1] * self.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 False
    if self.parents[x] > self.parents[y]:
      x, y = y, x
    self.parents[x] += self.parents[y]
    self.parents[y] = x
        
  def issame(self, x, y):
    return self.find(x) == self.find(y)
  
  def get_node_members(self):
    return [[j for j in range(self.n) if self.issame(i, j)] for i in range(self.n)]
  
  def size(self, x):
    return -self.parents[self.find(x)]

N = list(map(int, input().split()))
parents = [-1] * N[0]
def find(x):
  if parents[x] < 0:
    return x
  else:
    parents[x] = find(parents[x])
    return parents[x]

def union(x, y):
  x = find(x)
  y = find(y)

  if x == y: return False
  if parents[x] > parents[y]:
    x, y = y, x
  parents[x] += parents[y]
  parents[y] = x
        
def issame(x, y):
  return find(x) == find(y)
  
def size(x):
  return -parents[find(x)]
  

results = [0 for i in range(N[0])]
for i in range(N[1]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  results[x] += 1
  results[y] += 1
  union(x, y)
#print(uf.parents)

for i in range(N[2]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  if issame(x, y):
  	results[x] += 1
  	results[y] += 1
  
#print(node_members, results)

print(" ".join([str(size(i) - results[i] - 1) for i in range(N[0])]))

Buggy submission (verdict: Runtime Error):
#https://www.slideshare.net/chokudai/union-find-49066733
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.ranks = [0] * self.n
    self.parents = list(range(self.n))

  def find(self, x):
    if self.parents[x] == x:
      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.ranks[x] > self.ranks[y]:
      x, y = y, x
    if self.ranks[x] == self.ranks[y]:
      self.ranks[x] += 1

    self.parents[y] = x
        
  def issame(self, x, y):
    return self.find(x) == self.find(y)
  
  def get_root_members(self):
    roots = set(self.parents) 
    self.root_members = {r:[] for r in roots}
    for i, r in enumerate(self.parents):
      if r != i:
      	self.root_members[r].append(i)
    return self.root_members
  
  def get_node_members(self):
    self.get_root_members()
    return [self.root_members[r] for r in self.parents]
  
N = list(map(int, input().split()))
uf = UnionFind(N[0])
results = [[] for i in range(N[0])]
for i in range(N[1]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  results[x].append(y)
  results[y].append(x)
  uf.union(x, y)
#print(uf.parents)

for i in range(N[2]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  results[x].append(y)
  results[y].append(x)
  
node_members = uf.get_node_members()
#print(node_members, results)

# どうやら set で解くべきではなさそう
print(" ".join([str(len(set(node_members[i]) - set(results[i]) - set([i]))) for i in range(N[0])]))

Buggy submission (verdict: Runtime Error):
#https://www.slideshare.net/chokudai/union-find-49066733
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.ranks = [0] * self.n
    self.parents = list(range(self.n))

  def find(self, x):
    if self.parents[x] == x:
      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.ranks[x] > self.ranks[y]:
      x, y = y, x
    if self.ranks[x] == self.ranks[y]:
      self.ranks[x] += 1

    self.parents[y] = x
        
  def issame(self, x, y):
    return self.find(x) == self.find(y)
  
  def get_root_members(self):
    roots = set(self.parents) 
    self.root_members = {r:[] for r in roots}
    for i, r in enumerate(self.parents):
      if r != -1:
      	self.root_members[r].append(i)
    return self.root_members
  
  def get_node_members(self):
    self.get_root_members()
    return [self.root_members[r] for r in self.parents]
  
N = list(map(int, input().split()))
uf = UnionFind(N[0])
results = [[] for i in range(N[0])]
for i in range(N[1]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  results[x].append(y)
  results[y].append(x)
  uf.union(x, y)
#print(uf.parents)

for i in range(N[2]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  results[x].append(y)
  results[y].append(x)
  
node_members = uf.get_node_members()
#print(node_members, results)

print(" ".join([str(len(set(node_members[i]) - set(results[i])) - 1) for i in range(N[0])]))

Buggy submission (verdict: Runtime Error):
#https://www.slideshare.net/chokudai/union-find-49066733
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.ranks = [0] * self.n
    self.parents = list(range(self.n))

  def find(self, x):
    if self.parents[x] == x:
      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.ranks[x] > self.ranks[y]:
      x, y = y, x
    if self.ranks[x] == self.ranks[y]:
      self.ranks[x] += 1

    self.parents[y] = x
        
  def issame(self, x, y):
    return self.find(x) == self.find(y)
  
  def get_root_members(self):
    roots = set(self.parents)
    self.root_members = {r:[] for r in roots}
    for i, r in enumerate(self.parents):
      self.root_members[r].append(i)
    return self.root_members
  
  def get_node_members(self):
    self.get_root_members()
    return [self.root_members[r] for r in self.parents]
  
N = list(map(int, input().split()))
uf = UnionFind(N[0])
results = [[] for i in range(N[0])]
for i in range(N[1]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  results[x].append(y)
  results[y].append(x)
  uf.union(x, y)
#print(uf.parents)

for i in range(N[2]):
  x, y = list(map(int, input().split()))
  x, y = x - 1, y - 1
  results[x].append(y)
  results[y].append(x)
  
node_members = uf.get_node_members()
#print(node_members, results)

print(" ".join([str(len(set(node_members[i]) - set(results[i])) - 1) for i in range(N[0])]))