Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.num = [1] * (n + 1)

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        numx = self.num[x]
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        self.num[x] += self.num[y]
        self.num[y] += numx

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


n, m = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
inconv = n * (n - 1) // 2
inconv_list = []
inconv_list.append(inconv)

uf = UnionFind(n)
for i in range(m - 1, 0, -1):
    a, b = edge[i][0], edge[i][1]
    if not uf.same_check(a, b):
        inconv -= uf.num[a] * uf.num[b]
        uf.union(a, b)
    else:
        inconv = inconv
    inconv_list.insert(0, inconv)
print(*inconv_list, sep="\n")


Fixed submission:
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.num = [1] * (n + 1)

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        numx = self.num[x]
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        self.num[x] += self.num[y]
        self.num[y] += numx

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


n, m = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
inconv = n * (n - 1) // 2
inconv_list = [0] * m
inconv_list[m - 1] = inconv

uf = UnionFind(n)
for i in range(m - 1, 0, -1):
    if inconv == 0:
        break
    a, b = edge[i][0], edge[i][1]
    if not uf.same_check(a, b):
        inconv -= uf.num[a] * uf.num[b]
        uf.union(a, b)
    else:
        inconv = inconv
    inconv_list[i - 1] = inconv
print(*inconv_list, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.num = [1] * (n + 1)

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        numx = self.num[x]
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        self.num[x] += self.num[y]
        self.num[y] += numx

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


n, m = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
inconv = n * (n - 1) // 2
inconv_list = []
inconv_list.append(inconv)

uf = UnionFind(n)
for i in range(m - 1, 0, -1):
    a, b = edge[i][0], edge[i][1]
    if not uf.same_check(a, b):
        inconv -= uf.num[a] * uf.num[b]
        uf.union(a, b)
    else:
        inconv = inconv
    inconv_list.insert(0, inconv)
print(*inconv_list, sep="\n")


Fixed submission:
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.num = [1] * (n + 1)

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        numx = self.num[x]
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        self.num[x] += self.num[y]
        self.num[y] += numx

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


n, m = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
inconv = n * (n - 1) // 2
inconv_list = [0] * m
inconv_list[m - 1] = inconv

uf = UnionFind(n)
for i in range(m - 1, 0, -1):
    if inconv == 0:
        break
    a, b = edge[i][0], edge[i][1]
    if not uf.same_check(a, b):
        inconv -= uf.num[a] * uf.num[b]
        uf.union(a, b)
    else:
        inconv = inconv
    inconv_list[i - 1] = inconv
print(*inconv_list, sep="\n")