Buggy python submission (verdict: Runtime Error):

import sys
import collections

n, m = map(int, sys.stdin.readline().split())
p = list(range(n))
r = [set([i]) for i in range(n)]


def li_(): return [int(x)-1 for x in sys.stdin.readline().split()]


def find(x):
    if p[x] != x:
        p[i] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    tmp = r[y]
    for i in tmp:
        if p[i] != p[x]:
            p[i] = x
    r[x] = r[x] | r[y]
    r[y] = r[x]


flist = [0] * m
elist = [0] * m
for i in range(m):
    flist[i], elist[i] = li_()

flist.reverse()
elist.reverse()

ans = [0] * m
ans[0] = int(n*(n-1)/2)

for i in range(m-1):
    a, b = flist[i], elist[i]
    if find(a) != find(b):
        union(a, b)
    tmp = ans[0]
    c = sorted(collections.Counter(p).values(), reverse=True)
    for j in c:
        if j == 1:
            break
        tmp -= int(j * (j - 1) / 2)
    ans[i + 1] = tmp
    if tmp == 0:
        break

for i in range(m):
    print(ans[m-i-1])


Fixed submission:

import sys
import collections

n, m = map(int, sys.stdin.readline().split())
p = list(range(n))
r = [1] * n


def li_(): return [int(x)-1 for x in sys.stdin.readline().split()]


def find(x):
    global p
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    if y > x:
        x, y = y, x
    p[y] = x
    r[x] += r[y]
    r[y] = r[x]


flist = [0] * m
elist = [0] * m
for i in range(m):
    flist[i], elist[i] = li_()

flist.reverse()
elist.reverse()

ans = [0] * m
ans[0] = int(n*(n-1)/2)

for i in range(m-1):
    a, b = flist[i], elist[i]
    if find(a) != find(b):
        ans[i + 1] = ans[i] - r[find(a)]*r[find(b)]
        union(a, b)
    else:
        ans[i+1] = ans[i]
    if ans[i+1] == 0:
        break

for i in range(m):
    print(ans[m-i-1])

Buggy python submission (verdict: Runtime Error):

import sys
import collections

n, m = map(int, sys.stdin.readline().split())
p = list(range(n))
r = [set([i]) for i in range(n)]


def li_(): return [int(x)-1 for x in sys.stdin.readline().split()]


def find(x):
    if p[x] != x:
        p[i] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    tmp = r[y]
    for i in tmp:
        p[i] = x
    r[x] = r[x] | r[y]
    r[y] = r[x]


flist = [0] * m
elist = [0] * m
for i in range(m):
    flist[i], elist[i] = li_()

flist.reverse()
elist.reverse()

ans = [0] * m
ans[0] = int(n*(n-1)/2)

for i in range(m-1):
    a, b = flist[i], elist[i]
    if find(a) != find(b):
        union(a, b)
    tmp = ans[0]
    c = sorted(collections.Counter(p).values(), reverse=True)
    for j in c:
        if j == 1:
            break
        tmp -= int(j * (j - 1) / 2)
    ans[i + 1] = tmp
    if tmp == 0:
        break

for i in range(m):
    print(ans[m-i-1])


Fixed submission:

import sys
import collections

n, m = map(int, sys.stdin.readline().split())
p = list(range(n))
r = [1] * n


def li_(): return [int(x)-1 for x in sys.stdin.readline().split()]


def find(x):
    global p
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    if y > x:
        x, y = y, x
    p[y] = x
    r[x] += r[y]
    r[y] = r[x]


flist = [0] * m
elist = [0] * m
for i in range(m):
    flist[i], elist[i] = li_()

flist.reverse()
elist.reverse()

ans = [0] * m
ans[0] = int(n*(n-1)/2)

for i in range(m-1):
    a, b = flist[i], elist[i]
    if find(a) != find(b):
        ans[i + 1] = ans[i] - r[find(a)]*r[find(b)]
        union(a, b)
    else:
        ans[i+1] = ans[i]
    if ans[i+1] == 0:
        break

for i in range(m):
    print(ans[m-i-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import collections

n, m = map(int, sys.stdin.readline().split())
p = list(range(n))
r = [set([i]) for i in range(n)]


def li_(): return [int(x)-1 for x in sys.stdin.readline().split()]


def find(x):
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    p[y] = x
    r[x] = r[x] | r[y]
    r[y] = r[x]


flist = [0] * m
elist = [0] * m
for i in range(m):
    flist[i], elist[i] = li_()

flist.reverse()
elist.reverse()

ans = [0] * m
ans[0] = int(n*(n-1)/2)

for i in range(m-1):
    a, b = flist[i], elist[i]
    if find(a) != find(b):
        union(a, b)
    tmp = ans[0]
    for j in range(n):
        find(j)
    c = sorted(collections.Counter(p).values(), reverse=True)
    for j in c:
        if j == 1:
            break
        tmp -= int(j * (j - 1) / 2)
    ans[i + 1] = tmp
    if tmp == 0:
        break

for i in range(m):
    print(ans[m-i-1])


Fixed submission:

import sys
import collections

n, m = map(int, sys.stdin.readline().split())
p = list(range(n))
r = [1] * n


def li_(): return [int(x)-1 for x in sys.stdin.readline().split()]


def find(x):
    global p
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    if y > x:
        x, y = y, x
    p[y] = x
    r[x] += r[y]
    r[y] = r[x]


flist = [0] * m
elist = [0] * m
for i in range(m):
    flist[i], elist[i] = li_()

flist.reverse()
elist.reverse()

ans = [0] * m
ans[0] = int(n*(n-1)/2)

for i in range(m-1):
    a, b = flist[i], elist[i]
    if find(a) != find(b):
        ans[i + 1] = ans[i] - r[find(a)]*r[find(b)]
        union(a, b)
    else:
        ans[i+1] = ans[i]
    if ans[i+1] == 0:
        break

for i in range(m):
    print(ans[m-i-1])