Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
def input():
    return stdin.readline()[:-1]
#おまじない。

n,m=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(m)]
from itertools import combinations
#union-find召喚
par=[-1]*n
def find(x):
    if par[x]<0:
        return x
    else:
        par[x]=find(par[x])
        return par[x] #ここ覚えていなかった
def unite(x,y):
    x=find(x)
    y=find(y)
    if x==y:
        False
    else:
        if par[x]>par[y]:#sizeの大きいほうがxにしたい
            x,y=y,x
        par[x]+=par[y]
        par[y]=x
        return True
def same(x,y):
    return find(x)==find(y)
def size(x):
    return -par[find(x)]
comb=list(combinations(range(n),2))
ans=[]

for i in range(m):
    count=0
    num=m-i-1
    unite(AB[num][0]-1,AB[num][1]-1)
    for kumiawase in comb:
        if same(kumiawase[0],kumiawase[1]):
            count+=1
    ans.append(count)
ans=list(reversed(ans))
for i in range(len(ans)-1):
    print(ans[0]-ans[i+1])
print(ans[0])

Fixed submission:
n, m = map(int, input().split())

par = [-1] * (n + 1)


def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]


def size(x):
    X = find(x)
    return -par[X]


def unite(x, y):
    if find(x) == find(y):
        return False
    X = find(x)
    Y = find(y)
    if size(X) < size(Y):
        x, y = y, x
        X = find(x)
        Y = find(y)
    par[X] += par[Y]
    par[Y] = X
    return True


def is_same(x, y):
    return find(x) == find(y)


def calc(a):
    return a * (a - 1) // 2


goukei = calc(n)
ans = [-1] * (m + 1)
ans[-1] = calc(n)
cnt = 0
AB = [list(map(int, input().split())) for _ in range(m)]
for i in range(m - 1, -1, -1):
    a, b = AB[i]
    if is_same(a, b):
        ans[i] = goukei - cnt
    else:
        A = size(a)
        B = size(b)
        cnt += calc(A + B) - calc(A) - calc(B)
        unite(a, b)
        ans[i] = goukei - cnt
print(*ans[1::], sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(m)]
from itertools import combinations
#union-find召喚
par=[-1]*n
def find(x):
    if par[x]<0:
        return x
    else:
        par[x]=find(par[x])
        return par[x] #ここ覚えていなかった
def unite(x,y):
    x=find(x)
    y=find(y)
    if x==y:
        False
    else:
        if par[x]>par[y]:#sizeの大きいほうがxにしたい
            x,y=y,x
        par[x]+=par[y]
        par[y]=x
        return True
def same(x,y):
    return find(x)==find(y)
def size(x):
    return -par[find(x)]
comb=list(combinations(range(n),2))
ans=[]

for i in range(m):
    count=0
    num=m-i-1
    unite(AB[num][0]-1,AB[num][1]-1)
    for kumiawase in comb:
        if same(kumiawase[0],kumiawase[1]):
            count+=1
    ans.append(count)
ans=list(reversed(ans))
for i in range(len(ans)-1):
    print(ans[0]-ans[i+1])
print(ans[0])

Fixed submission:
n, m = map(int, input().split())

par = [-1] * (n + 1)


def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]


def size(x):
    X = find(x)
    return -par[X]


def unite(x, y):
    if find(x) == find(y):
        return False
    X = find(x)
    Y = find(y)
    if size(X) < size(Y):
        x, y = y, x
        X = find(x)
        Y = find(y)
    par[X] += par[Y]
    par[Y] = X
    return True


def is_same(x, y):
    return find(x) == find(y)


def calc(a):
    return a * (a - 1) // 2


goukei = calc(n)
ans = [-1] * (m + 1)
ans[-1] = calc(n)
cnt = 0
AB = [list(map(int, input().split())) for _ in range(m)]
for i in range(m - 1, -1, -1):
    a, b = AB[i]
    if is_same(a, b):
        ans[i] = goukei - cnt
    else:
        A = size(a)
        B = size(b)
        cnt += calc(A + B) - calc(A) - calc(B)
        unite(a, b)
        ans[i] = goukei - cnt
print(*ans[1::], sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(m)]
from itertools import combinations
#union-find召喚
par=[-1]*n
def find(x):
    if par[x]<0:
        return x
    else:
        par[x]=find(par[x])
        return par[x] #ここ覚えていなかった
def unite(x,y):
    x=find(x)
    y=find(y)
    if x==y:
        False
    else:
        if par[x]>par[y]:#sizeの大きいほうがxにしたい
            x,y=y,x
        par[x]+=par[y]
        par[y]=x
        return True
def same(x,y):
    return find(x)==find(y)
def size(x):
    return -par[find(x)]
comb=list(combinations(range(n),2))
ans=[]

for i in range(m):
    count=0
    num=m-i-1
    unite(AB[num][0]-1,AB[num][1]-1)
    for kumiawase in comb:
        if same(kumiawase[0],kumiawase[1]):
            count+=1
    ans.append(count)
ans=list(reversed(ans))
for i in range(len(ans)-1):
    print(ans[0]-ans[i+1])
print(ans[0])    

Fixed submission:
n, m = map(int, input().split())

par = [-1] * (n + 1)


def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]


def size(x):
    X = find(x)
    return -par[X]


def unite(x, y):
    if find(x) == find(y):
        return False
    X = find(x)
    Y = find(y)
    if size(X) < size(Y):
        x, y = y, x
        X = find(x)
        Y = find(y)
    par[X] += par[Y]
    par[Y] = X
    return True


def is_same(x, y):
    return find(x) == find(y)


def calc(a):
    return a * (a - 1) // 2


goukei = calc(n)
ans = [-1] * (m + 1)
ans[-1] = calc(n)
cnt = 0
AB = [list(map(int, input().split())) for _ in range(m)]
for i in range(m - 1, -1, -1):
    a, b = AB[i]
    if is_same(a, b):
        ans[i] = goukei - cnt
    else:
        A = size(a)
        B = size(b)
        cnt += calc(A + B) - calc(A) - calc(B)
        unite(a, b)
        ans[i] = goukei - cnt
print(*ans[1::], sep="\n")