Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
# union find
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]

AB = AB[::-1]
ret = []
tmp = 0
uf = UnionFind(N)
for a,b in AB:
    if uf.is_same(a-1, b-1):
        ret.append(0)
    else:
        c = Counter(uf.par)
        ret.append(c[uf.find(a-1)] * c[uf.find(b-1)])
        uf.union(a-1,b-1)
        for i in range(N):
            uf.find(i)

ans = 0
for i in range(M-1, -1, -1):
    ans += ret[i]
    print(ans)

Fixed submission:
from collections import Counter
# union find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)

    def find_root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def count(self, x):
        return -self.root[self.find_root(x)]

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]

AB = AB[::-1]
ret = []
tmp = 0
uf = UnionFind(N)
for a,b in AB:
    if uf.is_same_group(a-1, b-1):
        ret.append(0)
    else:

        ret.append(uf.count(a-1) * uf.count(b-1))
        uf.unite(a-1,b-1)

ans = 0
for i in range(M-1, -1, -1):
    ans += ret[i]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(M)]
 
AB.reverse()

reader = np.array([i + 1 for i in range(N)])
size = np.array([1 for i in range(N)])

def root(a):
  if reader[a-1] == a:
    return a - 1
  else:
    return reader[a-1] - 1

def merge(a, b):
  # calc root index
  a = root(a)
  b = root(b)
  if a == b:
    return 0
  res = size[a] * size[b]
  if size[a] >= size[b]:
    # add size is root
    size[a] += size[b]
    # replace zero is all child node
    size[b] = 0
    reader[reader == reader[b]] = reader[a]
  else:
    size[b] += size[a]
    size[a] = 0
    reader[reader == reader[a]] = reader[b]
  return res

result = []
ans = N * (N-1)/2
for i in AB:
  result.append(ans)
  ans -= merge(i[0], i[1])
for i in result[::-1]:
  print(int(i))

Fixed submission:
from collections import Counter
# union find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)

    def find_root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def count(self, x):
        return -self.root[self.find_root(x)]

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]

AB = AB[::-1]
ret = []
tmp = 0
uf = UnionFind(N)
for a,b in AB:
    if uf.is_same_group(a-1, b-1):
        ret.append(0)
    else:

        ret.append(uf.count(a-1) * uf.count(b-1))
        uf.unite(a-1,b-1)

ans = 0
for i in range(M-1, -1, -1):
    ans += ret[i]
    print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(M)]
 
AB.reverse()

reader = np.array([i + 1 for i in range(N)])
size = np.array([1 for i in range(N)])

def root(a):
  if reader[a-1] == a:
    return a - 1
  else:
    return reader[a-1] - 1

def merge(a, b):
  # calc root index
  a = root(a)
  b = root(b)
  if a == b:
    return
  res = size[a] * size[b]
  if size[a] >= size[b]:
    # add size is root
    size[a] += size[b]
    # replace zero is all child node
    size[b] = 0
    reader[reader == reader[b]] = reader[a]
  else:
    size[b] += size[a]
    size[a] = 0
    reader[reader == reader[a]] = reader[b]
  return res

result = []
ans = N * (N-1)/2
for i in AB:
  result.append(ans)
  ans -= merge(i[0], i[1])
for i in result[::-1]:
  print(i)  

Fixed submission:
from collections import Counter
# union find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)

    def find_root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def count(self, x):
        return -self.root[self.find_root(x)]

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]

AB = AB[::-1]
ret = []
tmp = 0
uf = UnionFind(N)
for a,b in AB:
    if uf.is_same_group(a-1, b-1):
        ret.append(0)
    else:

        ret.append(uf.count(a-1) * uf.count(b-1))
        uf.unite(a-1,b-1)

ans = 0
for i in range(M-1, -1, -1):
    ans += ret[i]
    print(ans)