Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 11:26:39 2019
D - 連結 / Connectivity
実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 
400 点
@author: justwah
"""

class UnionFind:
    def __init__(self, N):
        self.rank = [0]*N
        self.parent = [i for i in range(N)]
        self.n = N
        
    def root(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.root(self.parent[x])
            return self.parent[x]
        
    def merge(self, a, b):
        a = self.root(a)
        b = self.root(b)
        if a == b:
            return
        if self.rank[a] < self.rank[b]:
            self.parent[a] = b
        else:
            self.parent[b] = a
            if self.rank[a] == self.rank[b]:
                self.rank[a] +=1
                
    def same(self, a, b):
        return self.root(a) == self.roob(b)


n, k, l = map(int, input().split())
same = [0]*n

uf_road = UnionFind(n)
for _ in range(k):
    p, q = map(int, input().split())
    uf_road.merge(p-1, q-1)

uf_rail = UnionFind(n)
for _ in range(l):
    r, s = map(int, input().split())
    uf_rail.merge(r-1, s-1)
    
for i in range(n):
    for j in range(n):
        if (uf_road.parent[i] - uf_road.parent[j] == 0) \
            and (uf_rail.parent[i] - uf_rail.parent[j] == 0):
            same[i] +=1

for i in range(n):
    print(same[i], end = ' ')

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 11:26:39 2019
D - 連結 / Connectivity
実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 
400 点
@author: justwah
"""
from collections import Counter

class UnionFind:
    def __init__(self, N):
        self.rank = [0]*N
        self.parent = [i for i in range(N)]
        self.n = N
        
    def root(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.root(self.parent[x])
            return self.parent[x]
        
    def merge(self, a, b):
        a = self.root(a)
        b = self.root(b)
        if a == b:
            return
        if self.rank[a] < self.rank[b]:
            self.parent[a] = b
        else:
            self.parent[b] = a
            if self.rank[a] == self.rank[b]:
                self.rank[a] +=1
                
    def same(self, a, b):
        return self.root(a) == self.roob(b)


n, k, l = map(int, input().split())

uf_road = UnionFind(n)
for _ in range(k):
    p, q = map(int, input().split())
    uf_road.merge(p-1, q-1)

uf_rail = UnionFind(n)
for _ in range(l):
    r, s = map(int, input().split())
    uf_rail.merge(r-1, s-1)
    
ctr = Counter()
for i in range(n):  # make a list to count number of cities that both road and rail-wise are in the same group
    r1 = uf_road.root(i)
    r2 = uf_rail.root(i)
    ctr[(r1,r2)] += 1

ans = []
for i in range(n): # print the group size of that city when looped
    r1 = uf_road.root(i)
    r2 = uf_rail.root(i)
    ans.append(ctr[(r1,r2)])

print(*ans)   # * is for unpack the list

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 11:26:39 2019
D - 連結 / Connectivity
実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 
400 点
@author: justwah

stupid hard code solution
need to find good way to accommodate multiple forests for unionfind
"""

n, k, l = map(int, input().split())
parentK = [0]*n
parentL = [0]*n
rankK = [0]*n
rankL = [0]*n
same = [0]*n


for i in range(n):
    parentK[i] = i
    parentL[i] = i

def rootK(x): #recursively find root for a node
    if parentK[x] == x: # if at root, return self
        return x
    else:
        parentK[x] = rootK(parentK[x])  # while searching rejoint queried node to a common parent for hierarchical tree
        return parentK[x]
    
def mergeK(a, b):
    a = rootK(a)
    b = rootK(b)
    if rankK[a] < rankK[b]: # joint lower rank node to a higher rank one
        parentK[a] = b
    else:
        parentK[b] = a
        if rankK[a] == rankK[b]: # rank only change if two same rank node joined
            rankK[a] +=1
    return

def rootL(x): #recursively find root for a node
    if parentL[x] == x: # if at root, return self
        return x
    else:
        parentL[x] = rootL(parentL[x])  # while searching rejoint queried node to a common parent for hierarchical tree
        return parentL[x]
    
def mergeL(a, b):
    a = rootL(a)
    b = rootL(b)
    if rankL[a] < rankL[b]: # joint lower rank node to a higher rank one
        parentL[a] = b
    else:
        parentL[b] = a
        if rankL[a] == rankL[b]: # rank only change if two same rank node joined
            rankL[a] +=1
    return

for _ in range(k):
    p, q = map(int, input().split())
    mergeK(p-1, q-1)

for _ in range(l):
    r, s = map(int, input().split())
    mergeL(r-1, s-1)
    
for i in range(n):
    for j in range(n):
        if (parentK[i] - parentK[j] == 0) and (parentL[i] - parentL[j] == 0):
            same[i] +=1

for i in range(n):
    print(same[i], end = ' ')

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 11:26:39 2019
D - 連結 / Connectivity
実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 
400 点
@author: justwah
"""
from collections import Counter

class UnionFind:
    def __init__(self, N):
        self.rank = [0]*N
        self.parent = [i for i in range(N)]
        self.n = N
        
    def root(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.root(self.parent[x])
            return self.parent[x]
        
    def merge(self, a, b):
        a = self.root(a)
        b = self.root(b)
        if a == b:
            return
        if self.rank[a] < self.rank[b]:
            self.parent[a] = b
        else:
            self.parent[b] = a
            if self.rank[a] == self.rank[b]:
                self.rank[a] +=1
                
    def same(self, a, b):
        return self.root(a) == self.roob(b)


n, k, l = map(int, input().split())

uf_road = UnionFind(n)
for _ in range(k):
    p, q = map(int, input().split())
    uf_road.merge(p-1, q-1)

uf_rail = UnionFind(n)
for _ in range(l):
    r, s = map(int, input().split())
    uf_rail.merge(r-1, s-1)
    
ctr = Counter()
for i in range(n):  # make a list to count number of cities that both road and rail-wise are in the same group
    r1 = uf_road.root(i)
    r2 = uf_rail.root(i)
    ctr[(r1,r2)] += 1

ans = []
for i in range(n): # print the group size of that city when looped
    r1 = uf_road.root(i)
    r2 = uf_rail.root(i)
    ans.append(ctr[(r1,r2)])

print(*ans)   # * is for unpack the list