Problem p03108 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100010)
class UnionFind:
    """
    Disjoint sets
    """

    def __init__(self, size):
        # 負の値はルート (集合の代表) で集合の個数
        # 正の値は次の要素を表す
        self.size = size
        self.parent = [-1] * size

    def find(self, x):
        """
        xを含む集合の代表を求める
        """
        if self.parent[x] < 0:
            return x
        root = self.find(self.parent[x])
        self.parent[x] = root
        return root

    def union(self, x, y):
        """
        xを含む集合とyを含む集合を併合する
        """
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x != root_y:
            if self.parent[root_x] >= self.parent[root_y]:
                self.parent[root_x] += self.parent[root_y]
                self.parent[root_y] = root_x
            else:
                self.parent[root_y] += self.parent[root_x]
                self.parent[root_x] = root_y

    def get_size(self, x):
        return -self.parent[self.find(x)]



n,m = map(int, input().split())
bs = []
for i in range(m):
    a,b = map(int, input().split())
    bs.append((a-1,b-1))

uf = UnionFind(n)

res  = []
fuben = n*(n-1)//2
for a,b in bs[::-1]:
    res.append(fuben)
    if uf.find(a) != uf.find(b):
        fuben -= uf.get_size(a) * uf.get_size(b)
    uf.union(a,b)

for v in res[::-1]:
    print(v)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)
class UnionFind:
    """
    Disjoint sets
    """

    def __init__(self, size):
        # 負の値はルート (集合の代表) で集合の個数
        # 正の値は次の要素を表す
        self.size = size
        self.parent = [-1] * size

    def find(self, x):
        """
        xを含む集合の代表を求める
        """
        if self.parent[x] < 0:
            return x
        root = self.find(self.parent[x])
        self.parent[x] = root
        return root

    def union(self, x, y):
        """
        xを含む集合とyを含む集合を併合する
        """
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x != root_y:
            if self.parent[root_x] >= self.parent[root_y]:
                self.parent[root_x] += self.parent[root_y]
                self.parent[root_y] = root_x
            else:
                self.parent[root_y] += self.parent[root_x]
                self.parent[root_x] = root_y

    def get_size(self, x):
        return -self.parent[self.find(x)]



n,m = map(int, input().split())
bs = []
for i in range(m):
    a,b = map(int, input().split())
    bs.append((a-1,b-1))

uf = UnionFind(n)

res  = []
fuben = n*(n-1)//2
for a,b in bs[::-1]:
    res.append(fuben)
    if uf.find(a) != uf.find(b):
        fuben -= uf.get_size(a) * uf.get_size(b)
    uf.union(a,b)

for v in res[::-1]:
    print(v)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)
class UnionFind:
    """
    Disjoint sets
    """

    def __init__(self, size):
        # 負の値はルート (集合の代表) で集合の個数
        # 正の値は次の要素を表す
        self.size = size
        self.parent = [-1] * size
        self.group_count = size

    def find(self, x):
        """
        xを含む集合の代表を求める
        """
        if self.parent[x] < 0:
            return x
        root = self.find(self.parent[x])
        self.parent[x] = root
        return root

    def union(self, x, y):
        """
        xを含む集合とyを含む集合を併合する
        """
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x != root_y:
            if self.parent[root_x] >= self.parent[root_y]:
                self.parent[root_x] += self.parent[root_y]
                self.parent[root_y] = root_x
            else:
                self.parent[root_y] += self.parent[root_x]
                self.parent[root_x] = root_y
            self.group_count -= 1
    def get_size(self, x):
        return -self.parent[self.find(x)]



n,m = map(int, input().split())
connected = [0]*n
bs = []
for i in range(m):
    a,b = map(int, input().split())
    bs.append((a-1,b-1))
bs.reverse()

uf = UnionFind(n)

res  = []
fuben = n*(n-1)//2
for a,b in bs:
    res.append(fuben)
    if uf.find(a) == uf.find(b):
        pass
    else:
        fuben -= uf.get_size(a) * uf.get_size(b)
    uf.union(a,b)

res.reverse()
for v in res:
    print(v)

Buggy submission (verdict: Runtime Error):
class UnionFind:
    """
    Disjoint sets
    """

    def __init__(self, size):
        # 負の値はルート (集合の代表) で集合の個数
        # 正の値は次の要素を表す
        self.size = size
        self.parent = [-1] * size
        self.group_count = size

    def find(self, x):
        """
        xを含む集合の代表を求める
        """
        if self.parent[x] < 0:
            return x
        root = self.find(self.parent[x])
        self.parent[x] = root
        return root

    def union(self, x, y):
        """
        xを含む集合とyを含む集合を併合する
        """
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x != root_y:
            if self.parent[root_x] >= self.parent[root_y]:
                self.parent[root_x] += self.parent[root_y]
                self.parent[root_y] = root_x
            else:
                self.parent[root_y] += self.parent[root_x]
                self.parent[root_x] = root_y
            self.group_count -= 1
    def get_size(self, x):
        return -self.parent[self.find(x)]



n,m = map(int, input().split())
connected = [0]*n
bs = []
for i in range(m):
    a,b = map(int, input().split())
    bs.append((a-1,b-1))
bs.reverse()

uf = UnionFind(n)

res  = []
fuben = n*(n-1)//2
for a,b in bs:
    res.append(fuben)
    if uf.find(a) == uf.find(b):
        pass
    else:
        fuben -= uf.get_size(a) * uf.get_size(b)
    uf.union(a,b)

res.reverse()
for v in res:
    print(v)