Problem p02536 (python)

Fixed submission:
(N,M),*AB=[list(map(int, x.split())) for x in open(0).readlines() if len(x)>1]

class UnionFind:
    def __init__(self, n):
        self.n=n
        self.parents=[-1 for i in range(n)]

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx == ry:
            return
        if self.parents[rx] > self.parents[ry]:
            rx,ry=ry,rx
        self.parents[rx] = self.parents[ry]
        self.parents[ry] = rx

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i,x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}


d=UnionFind(N)
for a,b in AB:
    d.union(a-1,b-1)
print(d.group_count() - 1)

Buggy submission (verdict: Runtime Error):
(N,M),*AB=[list(map(int, x.split())) for x in open(0).readlines() if len(x)>1]

class UnionFind:
    def __init__(self, n):
        self.par=[i for i in range(n)]

    def root(self, x):
        if self.par[x] == x:
            return x
        return self.par[x] == self.root(self.par[x])

    def union(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry:
            return
        self.par[rx] = ry

    def same(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        return rx == ry

    def count(self):
        return len([0 for i, x in enumerate(self.par) if x == i])

d=UnionFind(N)
for a,b in AB:
    d.union(a-1,b-1)
print(d.count() - 1)

Buggy submission (verdict: Runtime Error):
(N,M),*AB=[list(map(int, x.split())) for x in open(0).readlines() if len(x)>1]
d={}
g=0
for a,b in AB:
    if a in d:
        d[b] = d[a]
    elif b in d:
        b[a] = d[b]
    else:
        g += 1
        d[a] = d[b] = g
for i in range(1,N+1):
    if not i in d:
        g += 1
print(g - 1)

Buggy submission (verdict: Runtime Error):
#(N,M),*AB=[list(map(int, x.split())) for x in open(0).readlines()]
N,M=map(int,input().split())
AB=[list(map(int, input().split())) for _ in range(M)]
d={}
g=0
for a,b in AB:
    if a in d:
        d[b] = d[a]
    elif b in d:
        b[a] = d[b]
    else:
        g += 1
        d[a] = d[b] = g
for i in range(1,N+1):
    if not i in d:
        g += 1
print(g - 1)

Buggy submission (verdict: Runtime Error):
(N,M),*AB=[list(map(int, x.split())) for x in open(0).readlines()]
d={}
g=0
for a,b in AB:
    if a in d:
        d[b] = d[a]
    elif b in d:
        b[a] = d[b]
    else:
        g += 1
        d[a] = d[b] = g
for i in range(1,N+1):
    if not i in d:
        g += 1
print(g - 1)

Buggy submission (verdict: Runtime Error):
N,M=map(int, input().split())
AB=list(map(int, open(0).read().split()))
ss=[set()]
for i in range(0, len(AB), 2):
    a=AB[i]
    b=AB[i+1]
    for s in ss:
        if a in s or b in s:
            s.add(a)
            s.add(b)
            break
    else:
        ss.add(set([a,b]))
print(N - len(ss) - M)