Problem p03855 (python)

Fixed submission:
import sys
 
sys.setrecursionlimit(10000)

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

#print(N,K,L,D,T)

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

def execOne(num, uf):
    for _ in range(num):
        d0, d1 = map(int, input().split())
        d0 = d0 - 1
        d1 = d1 - 1
        uf.union(d0, d1)

uf_D = UnionFind(N)
uf_T = UnionFind(N)

execOne(K, uf_D)
execOne(L, uf_T)


pairs = []
for i in range(N):
    pairs.append((uf_D.find(i), uf_T.find(i)))

pairs_count = {}
for p in pairs:
    if not p in pairs_count:
        pairs_count[p] = 1
    else:
        pairs_count[p] += 1

res = [0] * N
for i in range(N):
    res[i] = pairs_count[pairs[i]]
#print(*(res[i] for i in range(N)))
print(" ".join([str(i) for i in res]))

Buggy submission (verdict: Runtime Error):
import numpy as np

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

#print(N,K,L,D,T)

array_D = np.eye(N,N,dtype=int)
array_T = np.eye(N,N,dtype=int)
root_D = [i for i in range(N)]
root_T = [i for i in range(N)]

for _ in range(K):
    d0, d1 = map(int, input().split())
    d0 = d0 - 1
    d1 = d1 - 1
    r0 = root_D[d0]
    r1 = root_D[d1]
    array_D[r0] = array_D[r0] | array_D[r1]
    root_D[r1] = r0
    root_D[d1] = r0
    #print(list_D, root_D)

for _ in range(L):
    t0, t1 = map(int, input().split())
    t0 = t0 - 1
    t1 = t1 - 1
    r0 = root_T[t0]
    r1 = root_T[t1]
    array_T[r0] = array_T[r0] | array_T[r1]
    root_T[r1] = r0
    root_T[t1] = r0
    #print(list_T, root_T)

#和集合
res = [0] * N
for i in range(N):
    rd = root_D[i]
    rt = root_T[i]
    d = array_D[rd]
    t = array_T[rt]
    res[i] = sum(d & t)
#print(*(res[i] for i in range(N)))
print(" ".join([str(i) for i in res]))

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

#print(N,K,L,D,T)

list_D = [set([i]) for i in range(N)]
list_T = [set([i]) for i in range(N)]
root_D = [i for i in range(N)]
root_T = [i for i in range(N)]

#print(list_D)

for _ in range(K):
    d = map(int, input().split())
    d0 = d[0] - 1
    d1 = d[1] - 1
    r0 = root_D[d0]
    r1 = root_D[d1]
    list_D[r0] = list_D[r0] | list_D[r1]
    root_D[r1] = r0
    root_D[d1] = r0
    #print(list_D, root_D)

for _ in range(L):
    t = map(int, input().split())
    t0 = t[0] - 1
    t1 = t[1] - 1
    r0 = root_T[t0]
    r1 = root_T[t1]
    list_T[r0] = list_T[r0] | list_T[r1]
    root_T[r1] = r0
    root_T[t1] = r0
    #print(list_T, root_T)

#和集合
res = [0] * N
for i in range(N):
    rd = root_D[i]
    rt = root_T[i]
    d = list_D[rd]
    t = list_T[rt]
    res[i] = len(d & t)
#print(*(res[i] for i in range(N)))
print(" ".join([str(i) for i in res]))

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

#print(N,K,L,D,T)

list_D = [set([i]) for i in range(N)]
list_T = [set([i]) for i in range(N)]
root_D = [i for i in range(N)]
root_T = [i for i in range(N)]

#print(list_D)

for _ in range(K):
    d = map(int, input().split())
    d0 = d[0] - 1
    d1 = d[1] - 1
    r0 = root_D[d0]
    r1 = root_D[d1]
    list_D[r0] = list_D[r0] | list_D[r1]
    root_D[r1] = r0
    root_D[d1] = r0
    #print(list_D, root_D)

for for _ in range(L):
    t = map(int, input().split())
    t0 = t[0] - 1
    t1 = t[1] - 1
    r0 = root_T[t0]
    r1 = root_T[t1]
    list_T[r0] = list_T[r0] | list_T[r1]
    root_T[r1] = r0
    root_T[t1] = r0
    #print(list_T, root_T)

#和集合
res = [0] * N
for i in range(N):
    rd = root_D[i]
    rt = root_T[i]
    d = list_D[rd]
    t = list_T[rt]
    res[i] = len(d & t)
#print(*(res[i] for i in range(N)))
print(" ".join([str(i) for i in res]))

Buggy submission (verdict: Runtime Error):
N,K,L = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(K)]
T = [list(map(int, input().split())) for _ in range(L)]

#print(N,K,L,D,T)

list_D = [set([i]) for i in range(N)]
list_T = [set([i]) for i in range(N)]
root_D = [i for i in range(N)]
root_T = [i for i in range(N)]

#print(list_D)

for d in D:
    d0 = d[0] - 1
    d1 = d[1] - 1
    r0 = root_D[d0]
    r1 = root_D[d1]
    list_D[r0] = list_D[r0] | list_D[r1]
    root_D[r1] = r0
    root_D[d1] = r0
    #print(list_D, root_D)

for t in T:
    t0 = t[0] - 1
    t1 = t[1] - 1
    r0 = root_T[t0]
    r1 = root_T[t1]
    list_T[r0] = list_T[r0] | list_T[r1]
    root_T[r1] = r0
    root_T[t1] = r0
    #print(list_T, root_T)

#和集合
#res = [0] * N
for i in range(N):
    rd = root_D[i]
    rt = root_T[i]
    d = list_D[rd]
    t = list_T[rt]
    res[i] = len(d & t)
#print(*(res[i] for i in range(N)))
print(" ".join([str(i) for i in res]))