Problem p03855 (python)

Fixed submission:
# union-find木
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    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()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())



N, K, L = map(int, input().split())

uf_road = UnionFind(N)
uf_train = UnionFind(N)

for _ in range(K):
    a, b = map(int, input().split())
    uf_road.union(a-1, b-1)
    
for _ in range(L):
    a, b = map(int, input().split())
    uf_train.union(a-1, b-1)

from collections import defaultdict

d = defaultdict(int)
for i in range(N):
    d[(uf_road.find(i), uf_train.find(i))]+=1
    
ans = []
for i in range(N):
    ans.append(d[(uf_road.find(i), uf_train.find(i))])

print(' '.join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
# union-find木
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    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()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

## 
# N個の都市
# K本の道路
# L本の鉄道
N, K, L = map(int, input().split())

# N都市を初期の要素数とする
# 道路と鉄道をそれぞれ別のunion-find木として表現する
uf_p = UnionFind(N)
uf_t = UnionFind(N)

# 道路の木
for _ in range(K):
    x,y = map(int, input().split())
    uf_p.union(x-1,y-1)

# 鉄道の木
for _ in range(L):
    x,y = map(int, input().split())
    uf_.tunion(x-1,y-1)

    
# 出力結果を整形
result = []
for i in range(N):
    # 道路と鉄道の両方繋がっている件数をand条件で抽出する
    result.append(str(len(set(uf_p.members(i)) & set(uf_t.members(i)))))

print(" ".join(result))

Buggy submission (verdict: Runtime Error):
# union-find木
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    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()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

## 
# N個の都市
# K本の道路
# L本の鉄道
N, K, L = map(int, input().split())

# N都市を初期の要素数とする
# 道路と鉄道をそれぞれ別のunion-find木として表現する
uf_p = UnionFind(N)
uf_t = UnionFind(N)

# 道路の木
for _ in range(K):
    x,y = map(int, input().split())
    uf_p.union(px-1,py-1)

# 鉄道の木
for _ in range(L):
    x,y = map(int, input().split())
    uf_.tunion(px-1,py-1)

    
# 出力結果を整形
result = []
for i in range(N):
    # 道路と鉄道の両方繋がっている件数をand条件で抽出する
    result.append(str(len(set(uf_p.members(i)) & set(uf_t.members(i)))))

print(" ".join(result))

Buggy submission (verdict: Runtime Error):
# union-find木
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    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()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

## 
# N個の都市
# K本の道路
# L本の鉄道
N, K, L = map(int, input().split())

# N都市を初期の要素数とする
# 道路と鉄道をそれぞれ別のunion-find木として表現する
uf_p = UnionFind(N)
uf_t = UnionFind(N)

# 道路の木
for _ in range(K):
    x,y = map(int, input().split())
    uf_p.union(px-1,py-1)

# 鉄道の木
for _ in range(L):
    x,y = map(int, input().split())
    uf_.tunion(px-1,py-1)


# 鉄道の木
for rx,ry in r:
    uf_t.union(rx-1,ry-1)

    
# 出力結果を整形
result = []
for i in range(N):
    # 道路と鉄道の両方繋がっている件数をand条件で抽出する
    result.append(str(len(set(uf_p.members(i)) & set(uf_t.members(i)))))

print(" ".join(result))