Problem p03108 (python)

Fixed submission:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

class UnionFindTree:
  def __init__(self, n):
    self.nodes = [-1]*n
    
  def find(self, x):
      parent = self.nodes[x]
      if parent < 0:
        return x
      else:
        self.nodes[x] = self.find(self.nodes[x])
        return self.nodes[x]
  
  def unite(self, x, y):
    xp = self.find(x)
    yp = self.find(y)
    if self.nodes[xp] > self.nodes[yp]:
      xp, yp = yp, xp
    self.nodes[xp] += self.nodes[yp]
    self.nodes[yp] = xp  
  
  def size(self, x):
    return -self.nodes[self.find(x)]


def main():
  n, m = map(int, input().split())
  edges = []
  
  for _ in range(m):
    a, b = map(lambda x:int(x)-1, input().split())
    edges.append((a, b))
  uftree = UnionFindTree(n)
  scores = []
  
  for a, b in edges[::-1]:
    ap = uftree.find(a)
    bp = uftree.find(b)
    if  ap != bp:
      scores.append(uftree.size(ap) * uftree.size(bp))
      uftree.unite(a, b)    
    else:
      scores.append(0)
      
  sum = 0
  for score in scores[::-1]:
    sum += score
    print(sum)

if __name__=='__main__':
  main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/python3
# -*- coding:utf-8 -*-

class UnionFindTree:
  def __init__(self, n):
    self.nodes = [-1]*n
    
  def find(self, x):
      parent = self.nodes[x]
      if parent < 0:
        return x
      else:
        self.nodes[x] = self.find(self.nodes[x])
        return self.nodes[x]
  
  def unite(self, x, y):
    xp = self.find(x)
    yp = self.find(y)
    self.nodes[xp] += self.nodes[yp]
    self.nodes[yp] = xp  
  
  def size(self, x):
    return -self.nodes[self.find(x)]


def main():
  n, m = map(int, input().split())
  edges = []
  
  for _ in range(m):
    a, b = map(lambda x:int(x)-1, input().split())
    edges.append((a, b))
  uftree = UnionFindTree(n)
  scores = []
  
  for a, b in edges[::-1]:
    ap = uftree.find(a)
    bp = uftree.find(b)
    if  ap != bp:
      scores.append(uftree.size(ap) * uftree.size(bp))
      uftree.unite(a, b)    
    else:
      scores.append(0)
      
  sum = 0
  for score in scores[::-1]:
    sum += score
    print(sum)

if __name__=='__main__':
  main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# RE, TLE

import numpy as np

def main():
  N, M = map(int, input().split(' '))
  As, Bs = [], []
  for _ in range(M):
    A, B = map(int, input().split(' '))
    As += [A-1]
    Bs += [B-1]

  links = np.zeros((N, N), dtype=bool)
  scores = [0.0]
  for A, B in zip(As[::-1], Bs[::-1]):
    if not links[A, B]:
      links[A, B] = links[B, A] = 1
      links[links[:,A]==1] |= links[A, :]
      links[links[:,B]==1] |= links[B, :]
      scores += [(np.sum(links) - np.sum(np.diag(links)))/2]
    else:
      scores += [scores[-1]]
  max_score = scores[-1]
  for i in range(0,len(scores)-1)[::-1]:
    print(int(max_score - scores[i]))

if __name__=='__main__':
  main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import numpy as np

def main():
  N, M = map(int, input().split(' '))
  As, Bs = [], []
  for _ in range(M):
    A, B = map(int, input().split(' '))
    As += [A-1]
    Bs += [B-1]

  links = np.zeros((N, N), dtype=bool)
  scores = [0.0]
  for A, B in zip(As[::-1], Bs[::-1]):
    links[A, B] = links[B, A] = 1
    links[links[:,A]==1] |= links[A, :]
    links[links[:,B]==1] |= links[B, :]
    scores += [(np.sum(links) - np.sum(np.diag(links)))/2]
  max_score = scores[-1]
  for i in range(0,len(scores)-1)[::-1]:
    print(int(max_score - scores[i]))

if __name__=='__main__':
  main()