Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _ in range(size)]

    def find(self,x):
        while self.table[x] >= 0:
            x = self.table[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.table[s1] >= self.table[s2]:
            self.table[s1] += self.table[s2]
            self.table[s2] = s1
        else:
            self.table[s2] += self.table[s1]
            self.table[s1] = s2


def main():
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.table[px] * uf.table[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


uf = UnionFind(N)
main()

Fixed submission:
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self,size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]

    def find(self,x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]


def main():
    uf = UnionFind(N)
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.number[px] * uf.number[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _ in range(size)]

    def find(self,x):
        while self.table[x] >= 0:
            x = self.table[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.table[s1] >= self.table[s2]:
            self.table[s1] += self.table[s2]
            self.table[s2] = s1
        else:
            self.table[s2] += self.table[s1]
            self.table[s1] = s2


def func1(x, y):
    global uf
    px = uf.find(x)
    py = uf.find(y)
    if px == py:
        return 0
    r = uf.table[px] * uf.table[py]
    uf.union(px, py)
    return r


def main():
    ans = []
    p = 0
    for x, y in reversed(ab):
        p += func1(x, y)
        ans.append(p)

    nall = N * (N - 1) // 2
    for a in reversed(ans[:-1]):
        print(nall - a)
    print(nall)


uf = UnionFind(N)
main()

Fixed submission:
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self,size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]

    def find(self,x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]


def main():
    uf = UnionFind(N)
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.number[px] * uf.number[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(M)]


class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _ in range(size)]

    def find(self,x):
        while self.table[x] >= 0:
            x = self.table[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.table[s1] >= self.table[s2]:
            self.table[s1] += self.table[s2]
            self.table[s2] = s1
        else:
            self.table[s2] += self.table[s1]
            self.table[s1] = s2


def func1(x, y):
    global uf
    px = uf.find(x)
    py = uf.find(y)
    if px == py:
        return 0
    r = uf.table[px] * uf.table[py]
    uf.union(px, py)
    return r


def main():
    ans = [0] * (M - 1)
    p = 0
    for q in range(M - 1, 0, -1):
        p += func1(A[q][0] - 1, A[q][1] -1)
        ans[q - 1] = p

    nall = N * (N - 1) // 2
    for a in ans:
        print(nall - a)
    print(nall)


uf = UnionFind(N)
main()

Fixed submission:
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self,size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]

    def find(self,x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]


def main():
    uf = UnionFind(N)
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.number[px] * uf.number[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(M)]

# どのグループか
z = [-1] * N
# グループに含まれる数
gcount = [0] * min(N // 2, M)
k = 0


def func1(x, y):
    global z, gcount, k
    px = z[x]
    py = z[y]
    if px > -1 and py > -1:
        if px == py:
            return 0
        else:
            r = gcount[px] * gcount[py]
            for i in range(N):
                if z[i] == py:
                    z[i] = px
            gcount[px] += gcount[py]
            return r
    elif px > -1:
        r = gcount[px]
        z[y] = px
        gcount[px] += 1
        return r
    elif py > -1:
        r = gcount[py]
        z[x] = py
        gcount[py] += 1
        return r
    else:
        z[x] = k
        z[y] = k
        gcount[k] = 2
        k += 1
        return 1


ans = [0] * (M - 1)
p = 0
for q in range(M - 1, 0, -1):
    p += func1(A[q][0] - 1, A[q][1] -1)
    ans[q - 1] = p

nall = N * (N - 1) // 2
for a in ans:
    print(nall - a)
print(nall)

Fixed submission:
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self,size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]

    def find(self,x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]


def main():
    uf = UnionFind(N)
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.number[px] * uf.number[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(M)]

z = []


def func1(x, y):
    global z
    px = -1
    py = -1
    for i, s in enumerate(z):
        if x in s:
            if py > -1:
                c = len(z[py]) * len(z[i])
                z[py] |= z[i]
                del z[i]
                return c
            elif y in s:
                return 0
            else:
                px = i
        elif y in s:
            if px > -1:
                c = len(z[px]) * len(z[i])
                z[px] |= z[i]
                del z[i]
                return c
            else:
                py = i

    if px > -1:
        lx = len(z[px])
        z[px].add(y)
        return lx

    if py > -1:
        lx = len(z[py])
        z[py].add(x)
        return lx

    z.append({x, y})
    return 1


ans = [0] * (M - 1)
p = 0
for k in range(M - 1, 0, -1):
    p += func1(A[k][0], A[k][1])
    ans[k - 1] = p

nall = N * (N - 1) // 2
for a in ans:
    print(nall - a)
print(nall)

Fixed submission:
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self,size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]

    def find(self,x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]


def main():
    uf = UnionFind(N)
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.number[px] * uf.number[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(M)]

z = [set({})] * (N // 2)
k = 0


def func1(x, y):
    global z, k
    px = -1
    py = -1
    for i, s in enumerate(z[:k]):
        if x in s:
            if py > -1:
                lx = len(z[py])
                ly = len(z[i])
                z[py] |= z[i]
                z[i] = set({})
                return (lx + ly)*(lx + ly - 1) // 2 - lx * (lx - 1) // 2 - ly * (ly - 1) // 2
            elif y in s:
                return 0
            else:
                px = i
        elif y in s:
            if px > -1:
                lx = len(z[px])
                ly = len(z[i])
                z[px] |= z[i]
                z[i] = set({})
                return (lx + ly)*(lx + ly - 1) // 2 - lx * (lx - 1) // 2 - ly * (ly - 1) // 2
            else:
                py = i

    if px > -1:
        lx = len(z[px])
        z[px].add(y)
        return lx

    if py > -1:
        lx = len(z[py])
        z[py].add(x)
        return lx

    z[k] = {x, y}
    k += 1
    return 1


ans = [0] * (M - 1)
p = 0
for i in range(M - 1, 0, -1):
    p += func1(A[i][0], A[i][1])
    ans[i - 1] = p

nall = N * (N - 1) // 2
for a in ans:
    print(nall - a)
print(nall)

Fixed submission:
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self,size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]

    def find(self,x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]


def main():
    uf = UnionFind(N)
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.number[px] * uf.number[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(M)]
z = []


def func1(x, y):
    global z
    px = -1
    py = -1
    for i, s in enumerate(z):
        if x in s:
            if py > -1:
                lx = len(z[py])
                ly = len(z[i])
                z[py] |= z[i]
                del z[i]
                return (lx + ly)*(lx + ly - 1) // 2 - lx * (lx - 1) // 2 - ly * (ly - 1) // 2
            elif y in s:
                return 0
            else:
                px = i
        elif y in s:
            if px > -1:
                lx = len(z[px])
                ly = len(z[i])
                z[px] |= z[i]
                del z[i]
                return (lx + ly)*(lx + ly - 1) // 2 - lx * (lx - 1) // 2 - ly * (ly - 1) // 2
            else:
                py = i

    if px > -1:
        lx = len(z[px])
        z[px].add(y)
        return lx

    if py > -1:
        lx = len(z[py])
        z[py].add(x)
        return lx

    z.append(set({x, y}))
    return 1


ans = [0] * (M - 1)
p = 0
for i in range(M - 1, 0, -1):
    p += func1(A[i][0], A[i][1])
    ans[i - 1] = p

nall = N * (N - 1) // 2
for a in ans:
    print(nall - a)
print(nall)

Fixed submission:
N, M = map(int, input().split())
ab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self,size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]

    def find(self,x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]


def main():
    uf = UnionFind(N)
    p = N * (N - 1) // 2
    ans = [p]
    for x, y in reversed(ab):
        px = uf.find(x)
        py = uf.find(y)
        if px != py:
            p -= uf.number[px] * uf.number[py]
            uf.union(px, py)
        ans.append(p)

    for a in reversed(ans[:-1]):
        print(a)


main()