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


class UnionFind():
    def __init__(self, n):
        self.rank = [1 for _ in range(n)]
        self.parent = [i for i in range(n)]

    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        if i == j:
            return
        if self.rank[i] < self.rank[j]:
            self.parent[i] = j
        else:
            if self.rank[i] == self.rank[j]:
                self.rank[j] += 1
            self.parent[j] = i

    def same(self, i, j):
        return self.root(i) == self.root(j)

    def root(self, i):
        if self.parent[i] != i:
            self.parent[i] = self.root(self.parent[i])
        return self.parent[i]


uf = UnionFind(N)

for _ in range(M):
    X, Y, _ = list(map(int, input().split()))
    uf.union(X-1, Y-1)

ans = 0

for i in range(N):
    if uf.root(i) == i:
        ans += 1

print(ans)


Fixed submission:
N, M = list(map(int, input().split()))


class UnionFind():
    def __init__(self, n):
        self.rank = [1 for _ in range(n)]
        self.parent = [i for i in range(n)]

    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        if i == j:
            return
        if self.rank[i] < self.rank[j]:
            self.parent[i] = j
        else:
            if self.rank[i] == self.rank[j]:
                self.rank[j] += 1
            self.parent[j] = i

    def same(self, i, j):
        return self.root(i) == self.root(j)

    def root(self, i):
        if self.parent[i] != i:
            self.parent[i] = self.root(self.parent[i])
        return self.parent[i]


uf = UnionFind(N)

for _ in range(M):
    X, Y, _ = list(map(int, input().split()))
    uf.union(X-1, Y-1)

ans = 0

for i in range(N):
    if uf.parent[i] == i:
        ans += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
XYZ = []
for _ in range(M):
    XYZ.append(list(map(int, input().split())))


class UnionFind():
    def __init__(self, n):
        self.rank = [1 for _ in range(n)]
        self.parent = [i for i in range(n)]

    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        if i == j:
            return
        if self.rank[i] < self.rank[j]:
            self.parent[i] = j
        else:
            if self.rank[i] == self.rank[j]:
                self.rank[j] += 1
            self.parent[j] = i

    def same(self, i, j):
        return self.root(i) == self.root(j)

    def root(self, i):
        if self.parent[i] != i:
            self.parent[i] = self.root(self.parent[i])
        return self.parent[i]


uf = UnionFind(N)

for xyz in XYZ:
    X, Y, Z = xyz
    uf.union(X-1, Y-1)

ans = 0

for i in range(N):
    if uf.root(i) == i:
        ans += 1

print(ans)


Fixed submission:
N, M = list(map(int, input().split()))


class UnionFind():
    def __init__(self, n):
        self.rank = [1 for _ in range(n)]
        self.parent = [i for i in range(n)]

    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        if i == j:
            return
        if self.rank[i] < self.rank[j]:
            self.parent[i] = j
        else:
            if self.rank[i] == self.rank[j]:
                self.rank[j] += 1
            self.parent[j] = i

    def same(self, i, j):
        return self.root(i) == self.root(j)

    def root(self, i):
        if self.parent[i] != i:
            self.parent[i] = self.root(self.parent[i])
        return self.parent[i]


uf = UnionFind(N)

for _ in range(M):
    X, Y, _ = list(map(int, input().split()))
    uf.union(X-1, Y-1)

ans = 0

for i in range(N):
    if uf.parent[i] == i:
        ans += 1

print(ans)