Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N, K, L = [int(a) for a in input().split(" ")]
P, Q = zip(*[[int(v) for v in input().split(" ")] for i in range(K)])
R, S = zip(*[[int(v) for v in input().split(" ")] for i in range(L)])

def find(tab, a):
    if tab[a] == -1: return a
    tab[a] = find(tab, tab[a])
    return tab[a]

def union(tab, a, b):
    pa, pb = (find(tab, a), find(tab, b))
    if pa!=pb:
        tab[pb] = pa

loads = [-1]*N
rails = [-1]*N
for p, q in zip(P, Q):
    union(loads, p-1, q-1)

for r, s in zip(R, S):
    union(rails, r-1, s-1)

ans = []
for i in range(N):
    ans.append(0)
    for j in range(N):
        if find(loads, i)==find(loads, j) and find(rails, i)==find(rails,j):
            ans[-1]+=1

print(" ".join([str(n) for n in ans]))


Fixed submission:
from collections import defaultdict

N, K, L = [int(a) for a in input().split(" ")]
P, Q = zip(*[[int(v) for v in input().split(" ")] for i in range(K)])
R, S = zip(*[[int(v) for v in input().split(" ")] for i in range(L)])

def find(tab, a):
    if tab[a] == -1: return a
    tab[a] = find(tab, tab[a])
    return tab[a]

def union(tab, a, b):
    pa, pb = (find(tab, a), find(tab, b))
    if pa!=pb:
        tab[pb] = pa

loads = [-1]*N
rails = [-1]*N
both = [-1]*N
for p, q in zip(P, Q):
    union(loads, p-1, q-1)

for r, s in zip(R, S):
    union(rails, r-1, s-1)

dic = defaultdict(lambda: 0)
ans = []
for i in range(N):
    key = (find(loads, i), find(rails, i))
    ans.append(key)
    dic[key] += 1

ans = [dic[key] for key in ans]
print(" ".join([str(cnt) for cnt in ans]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N, K, L = [int(a) for a in input().split(" ")]
P, Q = zip(*[[int(v) for v in input().split(" ")] for i in range(K)])
R, S = zip(*[[int(v) for v in input().split(" ")] for i in range(L)])

def find(tab, a):
    if tab[a] == -1: return a
    tab[a] = find(tab, tab[a])
    return tab[a]

def union(tab, a, b):
    pa, pb = (find(tab, a), find(tab, b))
    if pa!=pb:
        tab[pb] = pa

loads = [-1]*N
rails = [-1]*N
for p, q in zip(P, Q):
    union(loads, p-1, q-1)

for r, s in zip(R, S):
    union(rails, r-1, s-1)

dl = defaultdict(set)
dr = defaultdict(set)
for i in range(N):
    dl[find(loads, i)].add(i)
    dr[find(rails, i)].add(i)

ans = [len(dl[find(loads, i)].intersection(dr[find(rails, i)])) for i in range(N)]

print(" ".join([str(n) for n in ans]))


Fixed submission:
from collections import defaultdict

N, K, L = [int(a) for a in input().split(" ")]
P, Q = zip(*[[int(v) for v in input().split(" ")] for i in range(K)])
R, S = zip(*[[int(v) for v in input().split(" ")] for i in range(L)])

def find(tab, a):
    if tab[a] == -1: return a
    tab[a] = find(tab, tab[a])
    return tab[a]

def union(tab, a, b):
    pa, pb = (find(tab, a), find(tab, b))
    if pa!=pb:
        tab[pb] = pa

loads = [-1]*N
rails = [-1]*N
both = [-1]*N
for p, q in zip(P, Q):
    union(loads, p-1, q-1)

for r, s in zip(R, S):
    union(rails, r-1, s-1)

dic = defaultdict(lambda: 0)
ans = []
for i in range(N):
    key = (find(loads, i), find(rails, i))
    ans.append(key)
    dic[key] += 1

ans = [dic[key] for key in ans]
print(" ".join([str(cnt) for cnt in ans]))