Buggy python submission (verdict: Time Limit Exceeded):
class Group:
    
    def __init__(self, a, b):
        self.member = {a, b}
        self.n = 2
        
        
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

d = {}
ans = [None] * M
ans[-1] = (N - 1) * N // 2
pre = ans[-1]
count = -2
for a, b in AB[:0:-1]:
    now = pre
    if a in d:
        if b in d:
            group1, group2 = d[a], d[b]
            if group1 is group2:
                pass
            else:
                now -= group1.n * group2.n
                group1.member |= group2.member
                group1.n += group2.n
                for x in group2.member:
                    d[x] = group1
        else:
            group = d[a]
            now -= group.n
            group.member.add(b)
            group.n += 1
            d[b] = group
    elif b in d:
        group = d[b]
        now -= group.n
        group.member.add(a)
        group.n += 1
        d[a] = group
    else:
        group = Group(a, b)
        d[a] = group
        d[b] = group
        now -= 1
    ans[count] = now
    pre = now
    count -= 1
print("\n".join(map(str, ans)))

Fixed submission:
class Group:
    
    def __init__(self, a, b):
        self.member = {a, b}
        self.n = 2
        
        
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

d = {}
ans = [None] * M
ans[-1] = (N - 1) * N // 2
pre = ans[-1]
count = -2
for a, b in AB[:0:-1]:
    now = pre
    if a in d:
        if b in d:
            group1, group2 = d[a], d[b]
            if group1 is group2:
                pass
            else:
                if group1.n < group2.n:
                    group1, group2 = group2, group1
                now -= group1.n * group2.n
                group1.member |= group2.member
                group1.n += group2.n
                for x in group2.member:
                    d[x] = group1
        else:
            group = d[a]
            now -= group.n
            group.member.add(b)
            group.n += 1
            d[b] = group
    elif b in d:
        group = d[b]
        now -= group.n
        group.member.add(a)
        group.n += 1
        d[a] = group
    else:
        group = Group(a, b)
        d[a] = group
        d[b] = group
        now -= 1
    ans[count] = now
    pre = now
    count -= 1
print("\n".join(map(str, ans)))

Buggy python submission (verdict: Time Limit Exceeded):
class Group:
    
    def __init__(self, a, b):
        self.member = {a, b}
        self.n = 2
        
        
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

d = {}
ans = [None] * M
ans[-1] = sum([i for i in range(1, N)])
pre = ans[-1]
count = -2
for a, b in AB[:0:-1]:
    now = pre
    if a in d:
        if b in d:
            group1, group2 = d[a], d[b]
            if group1 is group2:
                pass
            else:
                now -= group1.n * group2.n
                group1.member |= group2.member
                group1.n += group2.n
                for x in group2.member:
                    d[x] = group1
        else:
            group = d[a]
            now -= group.n
            group.member.add(b)
            group.n += 1
            d[b] = group
    elif b in d:
        group = d[b]
        now -= group.n
        group.member.add(a)
        group.n += 1
        d[a] = group
    else:
        group = Group(a, b)
        d[a] = group
        d[b] = group
        now -= 1
    ans[count] = now
    pre = now
    count -= 1
for ans_ in ans:
    print(ans_)

Fixed submission:
class Group:
    
    def __init__(self, a, b):
        self.member = {a, b}
        self.n = 2
        
        
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

d = {}
ans = [None] * M
ans[-1] = (N - 1) * N // 2
pre = ans[-1]
count = -2
for a, b in AB[:0:-1]:
    now = pre
    if a in d:
        if b in d:
            group1, group2 = d[a], d[b]
            if group1 is group2:
                pass
            else:
                if group1.n < group2.n:
                    group1, group2 = group2, group1
                now -= group1.n * group2.n
                group1.member |= group2.member
                group1.n += group2.n
                for x in group2.member:
                    d[x] = group1
        else:
            group = d[a]
            now -= group.n
            group.member.add(b)
            group.n += 1
            d[b] = group
    elif b in d:
        group = d[b]
        now -= group.n
        group.member.add(a)
        group.n += 1
        d[a] = group
    else:
        group = Group(a, b)
        d[a] = group
        d[b] = group
        now -= 1
    ans[count] = now
    pre = now
    count -= 1
print("\n".join(map(str, ans)))