Buggy python submission (verdict: Runtime Error):
class UnionFind(object):
    def __init__(self, size):
        self.size = size
        self.parents = list(range(size))
        self.counts = [1] * size

    def find(self, x):
        parent = self.parents[x]
        if x == parent:
            return x
        else:
            self.parents[x] = self.find(parent)
            return self.parents[x]
    
    def union(self, x1, x2):
        y1 = self.find(x1)
        y2 = self.find(x2)
        if y1 != y2:
            self.parents[y2] = y1
            self.counts[y1] += self.counts[y2]

    def count(self, x):
        return self.counts[self.find(x)]
    
if __name__ == '__main__':
    n, m = map(int, input().split())
    alist = []
    blist = []
    for _ in range(m):
        a, b = map(int, input().split())
        alist.append(a - 1)
        blist.append(b - 1)

    answers = [n * (n - 1) // 2]
    uf = UnionFind(n)

    for a, b in zip(reversed(alist), reversed(blist)):
        answers.append(answers[-1])
        
        c = uf.find(a)
        d = uf.find(b)
        if c == d: 
            continue
  
        answers[-1] -= uf.count(a) * uf.count(b)
        uf.union(a, b)

    for i in reversed(range(m)):
        print(answers[i])

Fixed submission:
class UnionFind(object):
    def __init__(self, size):
        self.size = size
        self.parents = list(range(size))
        self.counts = [1] * size

    def find(self, x):
        history = []
        while self.parents[x] != x:
            history.append(x)
            x = self.parents[x]
        
        for i in history: 
            self.parents[i] = x
        return x
    
    def union(self, x1, x2):
        y1 = self.find(x1)
        y2 = self.find(x2)
        if y1 != y2:
            self.parents[y2] = y1
            self.counts[y1] += self.counts[y2]

    def count(self, x):
        return self.counts[self.find(x)]
    
if __name__ == '__main__':
    n, m = map(int, input().split())
    alist = []
    blist = []
    for _ in range(m):
        a, b = map(int, input().split())
        alist.append(a - 1)
        blist.append(b - 1)

    answers = [n * (n - 1) // 2]
    uf = UnionFind(n)

    for a, b in zip(reversed(alist), reversed(blist)):
        answers.append(answers[-1])
        c = uf.find(a)
        d = uf.find(b)
        if c == d: 
            continue
  
        answers[-1] -= uf.count(a) * uf.count(b)
        uf.union(a, b)

    for i in reversed(range(m)):
        print(answers[i])

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
alist = []
blist = []
for _ in range(m):
    a, b = map(int, input().split())
    alist.append(a - 1)
    blist.append(b - 1)

c = {i : set((i, )) for i in range(n)}
answers = [n * (n - 1) // 2]
for a, b in zip(reversed(alist), reversed(blist)):
    answers.append(answers[-1])

    c1 = c[a]
    c2 = c[b]
    if b in c1: 
        continue
    answers[-1] -= len(c1) * len(c2)
    c3 = (c1 | c2)
    for i in c3:
        c[i] = c3

for i in reversed(range(m)):
    print(answers[i])


Fixed submission:
class UnionFind(object):
    def __init__(self, size):
        self.size = size
        self.parents = list(range(size))
        self.counts = [1] * size

    def find(self, x):
        history = []
        while self.parents[x] != x:
            history.append(x)
            x = self.parents[x]
        
        for i in history: 
            self.parents[i] = x
        return x
    
    def union(self, x1, x2):
        y1 = self.find(x1)
        y2 = self.find(x2)
        if y1 != y2:
            self.parents[y2] = y1
            self.counts[y1] += self.counts[y2]

    def count(self, x):
        return self.counts[self.find(x)]
    
if __name__ == '__main__':
    n, m = map(int, input().split())
    alist = []
    blist = []
    for _ in range(m):
        a, b = map(int, input().split())
        alist.append(a - 1)
        blist.append(b - 1)

    answers = [n * (n - 1) // 2]
    uf = UnionFind(n)

    for a, b in zip(reversed(alist), reversed(blist)):
        answers.append(answers[-1])
        c = uf.find(a)
        d = uf.find(b)
        if c == d: 
            continue
  
        answers[-1] -= uf.count(a) * uf.count(b)
        uf.union(a, b)

    for i in reversed(range(m)):
        print(answers[i])

Buggy python submission (verdict: Time Limit Exceeded):
def find(parents, x):
    if parents[x] == x:
        return x
    else:
        parents[x] = find(parents, parents[x])
        return parents[x]

def union(parents, x1, x2):
    y1 = find(parents, x1)
    y2 = find(parents, x2)
    if y1 != y2:
        parents[y2] = y1 


n, m = map(int, input().split())
alist = []
blist = []
for _ in range(m):
    a, b = map(int, input().split())
    alist.append(a - 1)
    blist.append(b - 1)

parents = list(range(n))
answers = [n * (n - 1) // 2]

for a, b in zip(reversed(alist), reversed(blist)):
    answers.append(answers[-1])
    c = find(parents, a)
    d = find(parents, b)
    if c == d: continue
    
    e = f = 0
    for i in range(n):
        j = find(parents, i)
        if c == j: e += 1
        if d == j: f += 1
    
    answers[-1] -= e * f
    union(parents, a, b)

for i in reversed(range(m)):
    print(answers[i])

Fixed submission:
class UnionFind(object):
    def __init__(self, size):
        self.size = size
        self.parents = list(range(size))
        self.counts = [1] * size

    def find(self, x):
        history = []
        while self.parents[x] != x:
            history.append(x)
            x = self.parents[x]
        
        for i in history: 
            self.parents[i] = x
        return x
    
    def union(self, x1, x2):
        y1 = self.find(x1)
        y2 = self.find(x2)
        if y1 != y2:
            self.parents[y2] = y1
            self.counts[y1] += self.counts[y2]

    def count(self, x):
        return self.counts[self.find(x)]
    
if __name__ == '__main__':
    n, m = map(int, input().split())
    alist = []
    blist = []
    for _ in range(m):
        a, b = map(int, input().split())
        alist.append(a - 1)
        blist.append(b - 1)

    answers = [n * (n - 1) // 2]
    uf = UnionFind(n)

    for a, b in zip(reversed(alist), reversed(blist)):
        answers.append(answers[-1])
        c = uf.find(a)
        d = uf.find(b)
        if c == d: 
            continue
  
        answers[-1] -= uf.count(a) * uf.count(b)
        uf.union(a, b)

    for i in reversed(range(m)):
        print(answers[i])

Buggy python submission (verdict: Time Limit Exceeded):
def find(parents, x):
    if parents[x] == x:
        return x
    else:
        parents[x] = find(parents, parents[x])
        return parents[x]

def union(parents, x1, x2):
    y1 = find(parents, x1)
    y2 = find(parents, x2)
    if y1 != y2:
        parents[y2] = y1 


n, m = map(int, input().split())
alist = []
blist = []
for _ in range(m):
    a, b = map(int, input().split())
    alist.append(a - 1)
    blist.append(b - 1)

parents = list(range(n))
answers = [n * (n - 1) // 2]
queries = set()
for i in range(n):
    for j in range(i + 1, n):
        queries.add((i, j))

for a, b in zip(reversed(alist), reversed(blist)):
    union(parents, a, b)
    answers.append(answers[-1])
    ng = set()
    for c, d in queries:
        if find(parents, c) == find(parents, d):
            answers[-1] -= 1
        else:
            ng.add((c, d))
    queries = ng

for i in reversed(range(m)):
    print(answers[i])

Fixed submission:
class UnionFind(object):
    def __init__(self, size):
        self.size = size
        self.parents = list(range(size))
        self.counts = [1] * size

    def find(self, x):
        history = []
        while self.parents[x] != x:
            history.append(x)
            x = self.parents[x]
        
        for i in history: 
            self.parents[i] = x
        return x
    
    def union(self, x1, x2):
        y1 = self.find(x1)
        y2 = self.find(x2)
        if y1 != y2:
            self.parents[y2] = y1
            self.counts[y1] += self.counts[y2]

    def count(self, x):
        return self.counts[self.find(x)]
    
if __name__ == '__main__':
    n, m = map(int, input().split())
    alist = []
    blist = []
    for _ in range(m):
        a, b = map(int, input().split())
        alist.append(a - 1)
        blist.append(b - 1)

    answers = [n * (n - 1) // 2]
    uf = UnionFind(n)

    for a, b in zip(reversed(alist), reversed(blist)):
        answers.append(answers[-1])
        c = uf.find(a)
        d = uf.find(b)
        if c == d: 
            continue
  
        answers[-1] -= uf.count(a) * uf.count(b)
        uf.union(a, b)

    for i in reversed(range(m)):
        print(answers[i])