Buggy python submission (verdict: Time Limit Exceeded):
n,k,l=map(int,input().split())
road=[list(map(int,input().split())) for i in range(k)]
train=[list(map(int,input().split())) for i in range(l)]

city1=[[i for i in range(n)]]
city2=[[i for i in range(n)]]
rank1=[0 for i in range(n)]
rank2=[0 for i in range(n)]
s1=[{i} for i in range(n)]
s2=[{i} for i in range(n)]
city1.append(rank1)
city1.append(s1)
city2.append(rank2)
city2.append(s2)
def find(c,x,y):
  return root(c,x)==root(c,y)
def root(c,x):
  if x==c[0][x]:
    return x
  else:
    return root(c,c[0][x])
def union(c,x,y):
  if find(c,x,y):
    return
  else:
    rx=root(c,x)
    ry=root(c,y)
    if c[1][x]>c[1][y]:
      c[0][y]=rx
      c[2][rx]=c[2][rx]|c[2][ry]
    elif c[1][y]>c[1][x]:
      c[0][x]=ry
      c[2][ry]=c[2][rx]|c[2][ry]
    else:
      c[0][x]=ry
      c[1][x]+=1
      c[2][ry]=c[2][rx]|c[2][ry]
for i in range(k):
  union(city1,road[i][0]-1,road[i][1]-1)
for i in range(l):
  union(city2,train[i][0]-1,train[i][1]-1)
root1=[root(city1,i) for i in range(n)]
root2=[root(city2,i) for i in range(n)]
for i in range(n):
  ans=len(city1[2][root1[i]] & city2[2][root2[i]])
  if i==n:
    print(ans)
  else:
    print(ans,end=' ')

Fixed submission:
from collections import Counter
n,k,l=map(int,input().split())
city1=[i for i in range(n)]
city2=[i for i in range(n)]
def root(c,x):
  if x==c[x]:
    return x
  else:
    return root(c,c[x])
def union(c,x,y):
  rx=root(c,x)
  ry=root(c,y)
  if rx>ry:
    c[rx]=ry
  else:
    c[ry]=rx
for i in range(k):
  a,b=map(int,input().split())
  union(city1,a-1,b-1)
for i in range(l):
  a,b=map(int,input().split())
  union(city2,a-1,b-1)
r=[]
for i in range(n):
  r.append((root(city1,i),root(city2,i)))
c=Counter(r)
for i in range(n):
  ans=c[r[i]]
  if i==n-1:
    print(ans)
  else:
    print(ans,end=' ')

Buggy python submission (verdict: Time Limit Exceeded):
n,k,l=map(int,input().split())
road=[list(map(int,input().split())) for i in range(k)]
train=[list(map(int,input().split())) for i in range(l)]

city1=[[i for i in range(n)]]
city2=[[i for i in range(n)]]
rank1=[0 for i in range(n)]
rank2=[0 for i in range(n)]
s1=[{i} for i in range(n)]
s2=[{i} for i in range(n)]
city1.append(rank1)
city1.append(s1)
city2.append(rank2)
city2.append(s2)
def find(c,x,y):
  return root(c,x)==root(c,y)
def root(c,x):
  if x==c[0][x]:
    return x
  else:
    return root(c,c[0][x])
def union(c,x,y):
  if find(c,x,y):
    return
  else:
    rx=root(c,x)
    ry=root(c,y)
    if c[1][x]>c[1][y]:
      c[0][y]=rx
      c[2][rx]=c[2][rx]|c[2][ry]
    elif c[1][y]>c[1][x]:
      c[0][x]=ry
      c[2][ry]=c[2][rx]|c[2][ry]
    else:
      c[0][x]=ry
      c[1][x]+=1
      c[2][ry]=c[2][rx]|c[2][ry]

for i in range(k):
  union(city1,road[i][0]-1,road[i][1]-1)
for i in range(l):
  union(city2,train[i][0]-1,train[i][1]-1)
ans=[0 for i in range(n)]
for i in range(n):
  ans[i]=len(city1[2][root(city1,i)] & city2[2][root(city2,i)])
  if i==n:
    print(ans[i])
  else:
    print(ans[i],end=' ')

Fixed submission:
from collections import Counter
n,k,l=map(int,input().split())
city1=[i for i in range(n)]
city2=[i for i in range(n)]
def root(c,x):
  if x==c[x]:
    return x
  else:
    return root(c,c[x])
def union(c,x,y):
  rx=root(c,x)
  ry=root(c,y)
  if rx>ry:
    c[rx]=ry
  else:
    c[ry]=rx
for i in range(k):
  a,b=map(int,input().split())
  union(city1,a-1,b-1)
for i in range(l):
  a,b=map(int,input().split())
  union(city2,a-1,b-1)
r=[]
for i in range(n):
  r.append((root(city1,i),root(city2,i)))
c=Counter(r)
for i in range(n):
  ans=c[r[i]]
  if i==n-1:
    print(ans)
  else:
    print(ans,end=' ')