Problem p03108 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5)
def find(parent,i):
    t = parent[i]
    if t < 0:
        return i
    t = find(parent,t)
    parent[i] = t
    return t
def unite(parent,i,j):
    i = find(parent,i)
    j = find(parent,j)
    if i == j:
        return
    parent[j] += parent[i]
    parent[i] = j
n,m = map(int,input().split())
parent = [-1] * n
ans = n*(n-1)//2
ansl = []
l0 = []
for i in range(m):
    ab = list(map(int,input().split()))
    ab[0] -= 1
    ab[1] -= 1
    l0.append(ab)
l0.reverse()
for i in range(m):
    ansl.append(ans)
    a,b = l0[i]
    if find(parent,a) != find(parent,b):
        ans -= parent[find(parent,a)] * parent[find(parent,b)]
    unite(parent,a,b)
ansl.reverse()
for i in range(m):
    print(ansl[i])

Buggy submission (verdict: Runtime Error):
def find(parent,i):
    t = parent[i]
    if t < 0:
        return i
    t = find(parent,t)
    parent[i] = t
    return t
def unite(parent,i,j):
    i = find(parent,i)
    j = find(parent,j)
    if i == j:
        return
    parent[j] += parent[i]
    parent[i] = j
n,m = map(int,input().split())
parent = [-1] * n
ans = n*(n-1)//2
ansl = []
l0 = []
for i in range(m):
    ab = list(map(int,input().split()))
    ab[0] -= 1
    ab[1] -= 1
    l0.append(ab)
l0.reverse()
for i in range(m):
    ansl.append(ans)
    a,b = l0[i]
    if find(parent,a) != find(parent,b):
        ans -= parent[find(parent,a)] * parent[find(parent,b)]
    unite(parent,a,b)
ansl.reverse()
for i in range(m):
    print(ansl[i])

Buggy submission (verdict: Runtime Error):
def find(parent,i):
    t = parent[i]
    if t < 0:
        return i
    else:
        t = find(parent,i)
        parent[i] = t
        return t
def unite(parent,i,j):
    i = find(parent,i)
    j = find(parent,j)
    if i == j:
        return
    else:
        parent[j] += parent[i]
        parent[i] = j
n,m = map(int,input().split())
parent = [-1] * n
ans = n*(n-1)//2
ansl = []
l0 = []
for i in range(m):
    ab = list(map(int,input().split()))
    l0.append(ab)
l0.reverse()
for i in range(m):
    ansl.append(ans)
    if find(parent,l0[i][0]) != find(parent,l0[i][1]):
        ans -= parent[find(parent,l0[i][0])] * parent[find(parent,l0[i][1])]
    unite(parent,l0[i][0],l0[i][1])
ansl.reverse()
for i in range(m):
    print(ansl[i])

Buggy submission (verdict: Runtime Error):
class unionfind:
    def __init__(self,n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
    def find_root(self,x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]
    def unite(self,x,y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.rnk[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1
    def same(self,x,y):
        return self.find_root(x) == self.find_root(y)
    def count(self,x):
        return -self.root[self.find_root(x)]
n,m = map(int,input().split())
l = unionfind(n+1)
ans = []
a = n*(n-1)//2
l0 = []
for i in range(m):
    ab = list(map(int,input().split()))
    l0.append(ab)
l0.reverse()
for i in range(m):
    ans.append(a)
    if not l.same(l0[i][0],l0[i][1]):
        a -= l.count(l0[i][0])*l.count(l0[i][1])
    l.unite(l0[i][0],l0[i][1])
ans.reverse()
for i in range(m):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
class unionfind():
    def __init__(self,n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
    def find_root(self,x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]
    def unite(self,x,y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.rnk[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1
    def same(self,x,y):
        return self.find_root(x) == self.find_root(y)
    def count(self,x):
        return -self.root[self.find_root(x)]
n,m = map(int,input().split())
l = unionfind(n+1)
ans = []
a = n*(n-1)//2
l0 = []
for i in range(m):
    ab = list(map(int,input().split()))
    l0.append(ab)
l0.reverse()
for i in range(m):
    ans.append(a)
    if not l.same(l0[i][0],l0[i][1]):
        a -= l.count(l0[i][0])*l.count(l0[i][1])
    l.unite(l0[i][0],l0[i][1])
ans.reverse()
for i in range(m):
    print(ans[i])