Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict as dd
road = dd(list)
train = dd(list)
rd = dict()
td = dict()
ans = []
def gr(tree,start):
    island = set()
    rest = [start,]
    while rest:
        node = rest.pop()
        island.add(node)
        for x in tree[node]:
            if x not in island:
                rest.append(x)
    return(island)
n,k,l = map(int, input().split())
for _ in range(k):
    a,b = map(int, input().split())
    road[a].append(b)
    road[b].append(a)
for _ in range(l):
    a,b = map(int, input().split())
    train[a].append(b)
    train[b].append(a)
for i in range(1,n+1):
    if i not in rd:
      tempr = gr(road,i)
      for x in tempr:
        rd[x] = tempr
    if i not in td:
      tempt = gr(train,i)
      for x in tempt:
        td[x] = tempt
    ans.append(str(len(rd[i]&td[i])))
print(" ".join(ans))

Fixed submission:
from collections import defaultdict as dd
def find(u,x):
    if u[x]<0:
        return x
    u[x] = find(u,u[x])
    return u[x]

def unite(u,a,b):
    a = find(u,a)
    b = find(u,b)
    if a == b:
        return False
    if u[b] < u[a]:
        a,b = b,a
    u[a] += u[b]
    u[b] = a
    return True
n,k,l = map(int, input().split())
ufr = [-1]*(n+1)
uft = [-1]*(n+1)
for _ in range(k):
    a,b = map(int, input().split())
    unite(ufr,a,b)
for _ in range(l):
    a,b = map(int, input().split())
    unite(uft,a,b)
tot = dd(int)
for i in range(1,n+1):
  tot[(find(ufr,i),find(uft,i))] += 1
print(" ".join([str(tot[(find(ufr,i),find(uft,i))]) for i in range(1,n+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict as dd
road = dd(list)
train = dd(list)
ans = []
def gr(tree,start):
    island = set()
    rest = [start,]
    while rest:
        node = rest.pop()
        island.add(node)
        for x in tree[node]:
            if x not in island:
                rest.append(x)
    return(island)
n,k,l = map(int, input().split())
for _ in range(k):
    a,b = map(int, input().split())
    road[a].append(b)
    road[b].append(a)
for _ in range(l):
    a,b = map(int, input().split())
    train[a].append(b)
    train[b].append(a)
for i in range(1,n+1):
    ans.append(str(len(gr(road,i)&gr(train,i))))
print(" ".join(ans))

Fixed submission:
from collections import defaultdict as dd
def find(u,x):
    if u[x]<0:
        return x
    u[x] = find(u,u[x])
    return u[x]

def unite(u,a,b):
    a = find(u,a)
    b = find(u,b)
    if a == b:
        return False
    if u[b] < u[a]:
        a,b = b,a
    u[a] += u[b]
    u[b] = a
    return True
n,k,l = map(int, input().split())
ufr = [-1]*(n+1)
uft = [-1]*(n+1)
for _ in range(k):
    a,b = map(int, input().split())
    unite(ufr,a,b)
for _ in range(l):
    a,b = map(int, input().split())
    unite(uft,a,b)
tot = dd(int)
for i in range(1,n+1):
  tot[(find(ufr,i),find(uft,i))] += 1
print(" ".join([str(tot[(find(ufr,i),find(uft,i))]) for i in range(1,n+1)]))

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict as dd
road = dd(list)
train = dd(list)
def gr(tree,start):
    island = set()
    rest = [start,]
    while rest:
        node = rest.pop()
        island.add(node)
        for x in tree[node]:
            if x not in island:
                rest.append(x)
    return(island)
n,k,l = map(int, input().split())
for _ in range(k):
    a,b = map(int,input().split())
    road[a].append(b)
    road[b].append(a)
for _ in range(k):
    a,b = map(int,input().split())
    train[a].append(b)
    train[b].append(a)
for i in range(1,n+1):
    print(len(gr(road,i)&gr(train,i)))

Fixed submission:
from collections import defaultdict as dd
def find(u,x):
    if u[x]<0:
        return x
    u[x] = find(u,u[x])
    return u[x]

def unite(u,a,b):
    a = find(u,a)
    b = find(u,b)
    if a == b:
        return False
    if u[b] < u[a]:
        a,b = b,a
    u[a] += u[b]
    u[b] = a
    return True
n,k,l = map(int, input().split())
ufr = [-1]*(n+1)
uft = [-1]*(n+1)
for _ in range(k):
    a,b = map(int, input().split())
    unite(ufr,a,b)
for _ in range(l):
    a,b = map(int, input().split())
    unite(uft,a,b)
tot = dd(int)
for i in range(1,n+1):
  tot[(find(ufr,i),find(uft,i))] += 1
print(" ".join([str(tot[(find(ufr,i),find(uft,i))]) for i in range(1,n+1)]))