Buggy python submission (verdict: Time Limit Exceeded):
n,k,l=map(int,input().split())
el=[set() for _ in range(n+1)]
et=[set() for _ in range(n+1)]
ela=[set() for _ in range(n+1)]
eta=[set() for _ in range(n+1)]
eml=[0]*(n+1)
emt=[0]*(n+1)
ans=[0]*(n+1)
for _ in range(k):
  p,q=map(int,input().split())
  el[p].add(q)
  el[q].add(p)
j=0
for i in range(n+1):
  if eml[i]:continue
  eml[i]=j
  ela[j].add(i)
  q=[i]
  while q:
    now=q.pop()
    eml[now]=j
    for to in el[now]:
      if eml[to]:continue
      ela[j].add(to)
      q+=[to]
  j+=1
for _ in range(l):
  p,q=map(int,input().split())
  et[p].add(q)
  et[q].add(p)
j=0
for i in range(n+1):
  if emt[i]:continue
  emt[i]=j
  eta[j].add(i)
  q=[i]
  while q:
    now=q.pop()
    emt[now]=j
    for to in et[now]:
      if emt[to]:continue
      eta[j].add(to)
      q+=[to]
  j+=1
for i in range(n+1):
  ans[i]=len(eta[emt[i]]&ela[eml[i]])
print(*ans[1:])

Fixed submission:
from collections import Counter
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
  def find(self, x):
    if(self.root[x] < 0):
      return x
    else:
      self.root[x] = self.find(self.root[x])
      return self.root[x]
  def unite(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if(x == y):
      return 
    elif(self.rnk[x] > self.rnk[y]):
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if(self.rnk[x] == self.rnk[y]):
        self.rnk[y] += 1
  def same(self, x, y):
    return self.find(x) == self.find(y)
n,k,l=map(int,input().split())
e=[[i]for i in range(n+1)]
ur=UnionFind(n)
ut=UnionFind(n)
for i in range(k):
  a,b=map(int,input().split())
  ur.unite(a,b)
for _ in range(l):
  a,b=map(int,input().split())
  ut.unite(a,b)
pair=[(ur.find(i),ut.find(i)) for i in range(1,n+1)]
c=Counter(pair)
ans=[c[i] for i in pair]
print(*ans)

Buggy python submission (verdict: Runtime Error):
def dfs(now,vis,e,j,el):
  vis+=[now]
  e[now]=j
  res=set([now])
  for to in el[now]:
    if to in vis:continue
    res=res|dfs(to,vis[:],e,j,el)
    e[to]=j
  return res
n,k,l=map(int,input().split())
el=[set() for _ in range(n+1)]
et=[set() for _ in range(n+1)]
ela=[set() for _ in range(n+1)]
eta=[set() for _ in range(n+1)]
eml=[0]*(n+1)
emt=[0]*(n+1)
ans=[0]*(n+1)
for _ in range(k):
  p,q=map(int,input().split())
  el[p].add(q)
  el[q].add(p)
j=0
for i in range(n+1):
  if eml[i]:continue
  ela[j]=dfs(i,[],eml,j,el)
  j+=1
for _ in range(l):
  p,q=map(int,input().split())
  et[p].add(q)
  et[q].add(p)
j=0
for i in range(n+1):
  if emt[i]:continue
  eta[j]=dfs(i,[],emt,j,et)
  j+=1
for i in range(n+1):
  ans[i]=len(eta[emt[i]]&ela[eml[i]])
print(*ans[1:])

Fixed submission:
from collections import Counter
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
  def find(self, x):
    if(self.root[x] < 0):
      return x
    else:
      self.root[x] = self.find(self.root[x])
      return self.root[x]
  def unite(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if(x == y):
      return 
    elif(self.rnk[x] > self.rnk[y]):
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if(self.rnk[x] == self.rnk[y]):
        self.rnk[y] += 1
  def same(self, x, y):
    return self.find(x) == self.find(y)
n,k,l=map(int,input().split())
e=[[i]for i in range(n+1)]
ur=UnionFind(n)
ut=UnionFind(n)
for i in range(k):
  a,b=map(int,input().split())
  ur.unite(a,b)
for _ in range(l):
  a,b=map(int,input().split())
  ut.unite(a,b)
pair=[(ur.find(i),ut.find(i)) for i in range(1,n+1)]
c=Counter(pair)
ans=[c[i] for i in pair]
print(*ans)