Problem p03108 (python)

Fixed submission:
N,M=list(map(int,input().split()))
L=[]
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.root[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 isSameGroup(self,x,y):
        if(self.find_root(x)==self.find_root(y)):
            return True
        else:
            False

    def Size(self,x):
        return -self.root[self.find_root(x)]

for i in range(M):
    li = list(map(int,input().split()))
    L.append(li)
L=L[::-1]

ans = [0]*(M)
ans[0] = N*(N-1)/2
T = UnionFind(N)
for i in range(M-1):
    if(T.isSameGroup(L[i][0],L[i][1])):
        ans[i+1] = ans[i]
        continue
    else:
        ans[i+1] = max(0, ans[i]-T.Size(L[i][0])*T.Size(L[i][1]))
        T.Unite(L[i][0],L[i][1])

for i in ans[::-1]:
    print(int(i))
#print(len(ans))

Buggy submission (verdict: Runtime Error):
N,M=list(map(int,input().split()))
L=[]
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.root[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 isSameGroup(self,x,y):
        if(self.find_root(x)==self.find_root(y)):
            return True
        else:
            False

    def Size(self,x):
        return -self.root[self.find_root(x)]

for i in range(M):
    li = list(map(int,input().split()))
    L.append(li)
L=L[::-1]

ans = [0]*(M)
ans[0] = N*(N-1)/2
T = UnionFind(N)
for i in range(N-1):
    if(T.isSameGroup(L[i][0],L[i][1])):
        ans[i+1] = ans[i]
        continue
    else:
        ans[i+1] = max(0, ans[i]-T.Size(L[i][0])*T.Size(L[i][1]))
        T.Unite(L[i][0],L[i][1])

for i in ans[::-1]:
    print(int(i))
#print(len(ans))

Buggy submission (verdict: Runtime Error):
N,M=list(map(int,input().split()))
L=[]
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.root[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 isSameGroup(self,x,y):
        if(self.find_root(x)==self.find_root(y)):
            return True
        else:
            False

    def Size(self,x):
        return -self.root[self.find_root(x)]

for i in range(M):
    li = list(map(int,input().split()))
    L.append(li)
L=L[::-1]

ans = [0]*(M+1)
ans[0] = N*(N-1)/2
T = UnionFind(N)
for i in range(N):
    if(T.isSameGroup(L[i][0],L[i][1])):
        ans[i+1] = ans[i]
        continue
    else:
        ans[i+1] = max(0, ans[i]-T.Size(L[i][0])*T.Size(L[i][1]))
        T.Unite(L[i][0],L[i][1])

for i in range(M-1,-1,-1):
    print(int(ans[i]))
#print(len(ans))

Buggy submission (verdict: Runtime Error):
N,M=list(map(int,input().split()))
L=[]
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.root[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 isSameGroup(self,x,y):
        if(self.find_root(x)==self.find_root(y)):
            return True
        else:
            False

    def Size(self,x):
        return -self.root[self.find_root(x)]

for i in range(M):
    li = list(map(int,input().split()))
    L.append(li)
L=L[::-1]

ans = [0]*(M+1)
ans[0] = N*(N-1)/2
T = UnionFind(N)
for i in range(N):
    if(T.isSameGroup(L[i][0],L[i][1])):
        ans[i+1] = ans[i]
        continue
    else:
        ans[i+1] = max(0, ans[i]-T.Size(L[i][0])*T.Size(L[i][1]))
        T.Unite(L[i][0],L[i][1])

for i in range(N,0,-1):
    print(int(ans[i]))
#print(len(ans))