Buggy python submission (verdict: Time Limit Exceeded):
N, K, L = map(int, input().split())
G = [[[], []] for i in range(N)]
R = [[i, i] for i in range(N)]
def root(i, j):
  if R[i][j] != i:
    R[i][j] = root(R[i][j], j)
  return R[i][j]
for i in range(K):
  p, q = map(int, input().split())
  rp = root(p - 1, 0)
  rq = root(q - 1, 0)
  if rp != rq:
    R[rp][0] = min(rp, rq)
    R[rq][0] = min(rp, rq)
for i in range(L):
  r, s = map(int, input().split())
  rr = root(r - 1, 1)
  rs = root(s - 1, 1)
  if rr != rs:
    R[rr][1] = min(rr, rs)
    R[rs][1] = min(rr, rs)
U = [[[], []] for i in range(N)]
for i in range(N):
  R[i][0] = root(i, 0)
  R[i][1] = root(i, 1)
  U[R[i][0]][0].append(i)
  U[R[i][1]][1].append(i)
A = [1 for i in range(N)]
for i in range(N):
  if A[i] == 1:
    S = set(U[R[i][0]][0]) & set(U[R[i][1]][1])
    for s in S:
      A[s] = len(S)
ans = ''
for a in A:
  ans += ' ' + str(a)
print(ans)

Fixed submission:
from collections import defaultdict
N, K, L = map(int, input().split())
G = [[[], []] for i in range(N)]
R = [[i, i] for i in range(N)]
def root(i, j):
  if R[i][j] != i:
    R[i][j] = root(R[i][j], j)
  return R[i][j]
for i in range(K):
  p, q = map(int, input().split())
  rp = root(p - 1, 0)
  rq = root(q - 1, 0)
  if rp != rq:
    R[rp][0] = min(rp, rq)
    R[rq][0] = min(rp, rq)
for i in range(L):
  r, s = map(int, input().split())
  rr = root(r - 1, 1)
  rs = root(s - 1, 1)
  if rr != rs:
    R[rr][1] = min(rr, rs)
    R[rs][1] = min(rr, rs)
A = defaultdict(int)
for i in range(N):
  R[i][0] = root(i, 0)
  R[i][1] = root(i, 1)
  A[(R[i][0], R[i][1])] += 1
for i in range(N):
  print(A[(R[i][0], R[i][1])])

Buggy python submission (verdict: Time Limit Exceeded):
N, K, L = map(int, input().split())
G = [[[], []] for i in range(N)]
R = [[i, i] for i in range(N)]
def root(i, j):
  if R[i][j] != i:
    R[i][j] = root(R[i][j], j)
  return R[i][j]
for i in range(K):
  p, q = map(int, input().split())
  rp = root(p - 1, 0)
  rq = root(q - 1, 0)
  if rp != rq:
    R[rp][0] = min(rp, rq)
    R[rq][0] = min(rp, rq)
for i in range(L):
  r, s = map(int, input().split())
  rr = root(r - 1, 1)
  rs = root(s - 1, 1)
  if rr != rs:
    R[rr][1] = min(rr, rs)
    R[rs][1] = min(rr, rs)
U = [[[], []] for i in range(N)]
for i in range(N):
  R[i][0] = root(i, 0)
  R[i][1] = root(i, 1)
  U[R[i][0]][0].append(i)
  U[R[i][1]][1].append(i)
A = [1 for i in range(N)]
for i in range(N):
  if A[i] == 1:
    S = set(U[R[i][0]][0]) & set(U[R[i][1]][1])
    for s in S:
      A[s] = len(S)
ans = ''
for a in A:
  ans += ' ' + str(a)
print(ans[1:])

Fixed submission:
from collections import defaultdict
N, K, L = map(int, input().split())
G = [[[], []] for i in range(N)]
R = [[i, i] for i in range(N)]
def root(i, j):
  if R[i][j] != i:
    R[i][j] = root(R[i][j], j)
  return R[i][j]
for i in range(K):
  p, q = map(int, input().split())
  rp = root(p - 1, 0)
  rq = root(q - 1, 0)
  if rp != rq:
    R[rp][0] = min(rp, rq)
    R[rq][0] = min(rp, rq)
for i in range(L):
  r, s = map(int, input().split())
  rr = root(r - 1, 1)
  rs = root(s - 1, 1)
  if rr != rs:
    R[rr][1] = min(rr, rs)
    R[rs][1] = min(rr, rs)
A = defaultdict(int)
for i in range(N):
  R[i][0] = root(i, 0)
  R[i][1] = root(i, 1)
  A[(R[i][0], R[i][1])] += 1
for i in range(N):
  print(A[(R[i][0], R[i][1])])