Buggy python submission (verdict: Time Limit Exceeded):
# https://atcoder.jp/contests/abc120/tasks/abc120_d
from collections import Counter
n, m = map(int, input().split())

brides = []
for _ in range(m):
    a, b = map(int, input().split())
    brides.append((a, b))


class Union:
    def __init__(self, val):
        self.val = val
        self.parent = self
        self.rank = 0

    def get_root(self):
        n = self
        while n.parent != n:
            n = n.parent
        return n

    def find_set(self):
        return self.get_root().val

unions = []
for i in range(n):
    unions.append(Union(i))

def check_no_access(unions, a, b):
    roots = [u.find_set() for u in unions]
    cd = Counter(roots)
    return cd.get(roots[a]) * cd.get(roots[b])

ans = []
ans.append(n * (n - 1) // 2)

for a, b in brides[::-1]:
    x, y = unions[a - 1], unions[b - 1]
    x_root = x.get_root()
    y_root = y.get_root()
    if x_root.find_set() == y_root.find_set():
        ans.append(ans[-1])
        continue
    ans.append(ans[-1] - check_no_access(unions, a - 1, b - 1))
    if x_root.rank == y_root.rank:
        y_root.parent = x_root
        x_root.rank += 1
    elif x_root.rank > y_root.rank:
        y_root.parent = x_root
    else:
        x_root.parent = y_root
ans.pop()
for a in ans[::-1]:
    print(a)

Fixed submission:
# https://atcoder.jp/contests/abc120/tasks/abc120_d
from collections import Counter
n, m = map(int, input().split())

brides = []
for _ in range(m):
    a, b = map(int, input().split())
    brides.append((a, b))


class Union:
    def __init__(self, val):
        self.val = val
        self.parent = self
        self.rank = 0
        self.size = 1

    def get_root(self):
        n = self
        while n.parent != n:
            n = n.parent
        return n

    def find_set(self):
        return self.get_root().val

unions = []
for i in range(n):
    unions.append(Union(i))

ans = []
ans.append(n * (n - 1) // 2)

for a, b in brides[::-1]:
    x, y = unions[a - 1], unions[b - 1]
    x_root = x.get_root()
    y_root = y.get_root()
    if x_root.find_set() == y_root.find_set():
        ans.append(ans[-1])
        continue
    ans.append(ans[-1] - x_root.size * y_root.size)
    if x_root.rank == y_root.rank:
        y_root.parent = x_root
        x_root.rank += 1
        size = x_root.size + y_root.size
    elif x_root.rank > y_root.rank:
        y_root.parent = x_root
    else:
        x_root.parent = y_root
    size = x_root.size + y_root.size
    x_root.size = size
    y_root.size = size
ans.pop()
for a in ans[::-1]:
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
# https://atcoder.jp/contests/abc120/tasks/abc120_d
n, m = map(int, input().split())

brides = []
for _ in range(m):
    a, b = map(int, input().split())
    brides.append((a, b))


class Union:
    def __init__(self, val):
        self.val = val
        self.parent = self
        self.rank = 0

    def get_root(self):
        n = self
        while n.parent != n:
            n = n.parent
        return n

    def find_set(self):
        return self.get_root().val

unions = []
for i in range(n):
    unions.append(Union(i))

def check_no_access(unions):
    roots = [u.find_set() for u in unions]
    counter = 0
    for i in range(len(roots)):
        for j in range(i + 1, len(roots)):
            if roots[i] != roots[j]:
                counter += 1
    return counter

ans = []
ans.append(check_no_access(unions))

for a, b in brides[::-1]:
    x, y = unions[a - 1], unions[b - 1]
    x_root = x.get_root()
    y_root = y.get_root()
    if x_root.rank == y_root.rank:
        y_root.parent = x_root
        x_root.rank += 1
    elif x_root.rank > y_root.rank:
        y_root.parent = x_root
    else:
        x_root.parent = y_root
    ans.append(check_no_access(unions))
ans.pop()
for a in ans[::-1]:
    print(a)

Fixed submission:
# https://atcoder.jp/contests/abc120/tasks/abc120_d
from collections import Counter
n, m = map(int, input().split())

brides = []
for _ in range(m):
    a, b = map(int, input().split())
    brides.append((a, b))


class Union:
    def __init__(self, val):
        self.val = val
        self.parent = self
        self.rank = 0
        self.size = 1

    def get_root(self):
        n = self
        while n.parent != n:
            n = n.parent
        return n

    def find_set(self):
        return self.get_root().val

unions = []
for i in range(n):
    unions.append(Union(i))

ans = []
ans.append(n * (n - 1) // 2)

for a, b in brides[::-1]:
    x, y = unions[a - 1], unions[b - 1]
    x_root = x.get_root()
    y_root = y.get_root()
    if x_root.find_set() == y_root.find_set():
        ans.append(ans[-1])
        continue
    ans.append(ans[-1] - x_root.size * y_root.size)
    if x_root.rank == y_root.rank:
        y_root.parent = x_root
        x_root.rank += 1
        size = x_root.size + y_root.size
    elif x_root.rank > y_root.rank:
        y_root.parent = x_root
    else:
        x_root.parent = y_root
    size = x_root.size + y_root.size
    x_root.size = size
    y_root.size = size
ans.pop()
for a in ans[::-1]:
    print(a)