Problem p03108 (python)

Fixed submission:
N, M = map(int, input().split())
B = [list(map(int, input().split())) for _ in range(M)]

pt = []
G = [[None, 1] for x in range(N+1)]

def find(x):
    n = G[x]
    c = []
    while not n[0] is None:
        c.append(n)
        n = n[0]
    for ci in c:
        ci[0] = n
    return n

cnt = 0
while len(B):
    x, y = B.pop()
    wx = find(x)
    wy = find(y)
    if wx is wy:
        pt.append(0)
    else:
        pt.append(wx[1] * wy[1])
        wx[1] += wy[1]
        wy[0] = wx

    #print(G)

cnt = 0
for _ in range(M):
    cnt += pt.pop()
    print(cnt)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

B = [list(map(int, input().split())) for _ in range(M)][::-1]

class Group:
    id = 0
    def __init__(self):
        self.id = Group.id
        Group.id += 1
        self.root = None
        self.children = []
        self.count = 2

    def top(self):
        w = self
        while not w.root is None:
            w = w.root
        return w

    def addMember(self):
        self.count += 1

    def union(self, g):
        self.top().count += g.top().count
        self.top().children.extend(g.top().children)
        ch = g.top().children
        for x in ch:
            x.root = self.top()
        g.top().root = self.top()

    def __repr__(self):
        w = self.top()
        return str(("Gropup", w.id, w.count))

pt = []
G = [None] * (N+1)
idx = 0
cnt = 0
for x, y in B:
    wx = G[x]
    wy = G[y]
    wcnt = (wx is None) + (wy is None)

    if wcnt >= 2:
        pt.append(1)
        w = Group()
        G[x] = w
        G[y] = w
    elif wcnt == 1:
        if wx is None:
            wx = wy
            G[x] = G[y]
        else:
            G[y] = G[x]
        pt.append(wx.top().count)
        wx.addMember()
    else:
        if wx.top() is wy.top():
            pt.append(0)
        else:
            pt.append(wx.top().count * wy.top().count)
            wx.union(wy)

    print(G)

cnt = 0
for x in pt[::-1]:
    cnt += x
    print(cnt)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

B = [list(map(int, input().split())) for _ in range(M)][::-1]

pt = []
G = [None] * (N+1)
C = [0] * (N+1)
idx = 0
cnt = 0
for x, y in B:
    wx = G[x]
    wy = G[y]
    wcnt = (wx is None) + (wy is None)

    if wcnt >= 2:
        pt.append(1)
        w = [idx]
        C[idx] += 2
        idx += 1
        G[x] = w
        G[y] = w
    elif wcnt == 1:
        if wx is None:
            wx = wy
            G[x] = G[y]
        else:
            G[y] = G[x]
        pt.append(C[wx[0]])
        C[wx[0]] += 1
    else:
        if wx[0] == wy[0]:
            pt.append(0)
        else:
            pt.append(C[wx[0]] * C[wy[0]])
            C[wx[0]] += C[wy[0]]
            G[y][0] = G[x][0]

    #print((G, C))

cnt = 0
for x in pt[::-1]:
    cnt += x
    print(cnt)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

B = [list(map(int, input().split())) for _ in range(M)][::-1]

S = []
pt = []
G = [None] * (N+1)
C = [0] * (N+1)
idx = 0
cnt = 0
for pair in B:
    d = []
    for i, x in enumerate(pair):
        if not G[x] is None:
            d.append(G[x])

    if len(d) <= 0:
        pt.append(1)
        w = [idx]
        C[idx] += 2
        idx += 1
        for x in pair:
            G[x] = w
    elif len(d) == 1:
        pt.append(C[d[0][0]])
        C[d[0][0]] += 1
        for x in pair:
            if G[x] is None:
                G[x] = d[0]
    else:
        if d[0][0] == d[1][0]:
            pt.append(0)
        else:
            pt.append(C[d[0][0]] * C[d[1][0]])
            C[d[0][0]] += C[d[1][0]]
            d[1][0] = d[0][0]

    print(G)

cnt = 0
for x in pt[::-1]:
    cnt += x
    print(cnt)