Problem:
 Score : 400 points    Problem Statement There are N cities. There are also K roads and L railways, extending between the cities.
The i -th road bidirectionally connects the p_i -th and q_i -th cities, and the i -th railway bidirectionally connects the r_i -th and s_i -th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.  We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.  For each city, find the number of the cities connected to that city by both roads and railways.      Constraints  2 ≦ N ≦ 2*10^5  1 ≦ K, L≦ 10^5  1 ≦ p_i, q_i, r_i, s_i ≦ N  p_i < q_i  r_i < s_i  When i ≠ j , (p_i, q_i) ≠ (p_j, q_j)  When i ≠ j , (r_i, s_i) ≠ (r_j, s_j)         Input The input is given from Standard Input in the following format:  N  K  L  p_1  q_1 : p_K  q_K  r_1  s_1 : r_L  s_L       Output Print N integers. The i -th of them should represent the number of the cities connected to the i -th city by both roads and railways.        Sample Input 1 4 3 1
1 2
2 3
3 4
2 3      Sample Output 1 1 2 2 1  All the four cities are connected to each other by roads.  By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1 , respectively.       Sample Input 2 4 2 2
1 2
2 3
1 4
2 3      Sample Output 2 1 2 2 1       Sample Input 3 7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7      Sample Output 3 1 1 2 1 2 2 2

Solution (Python):
import sys
readline = sys.stdin.readline
from collections import Counter

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    N, K, L = map(int, readline().rstrip().split())
    uf_road = UnionFind(N)
    uf_train = UnionFind(N)
    
    for _ in range(K):
        p, q = map(int, readline().rstrip().split())
        uf_road.union(p-1, q-1)
    
    for _ in range(L):
        r, s = map(int, readline().rstrip().split())
        uf_train.union(r-1, s-1)
    
    pairs = []
    for i in range(N):
        pairs.append((uf_road.find(i), uf_train.find(i)))
    
    cnt = Counter(pairs)
    res = [cnt[pair] for pair in pairs]
    print(*res)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
def root(x, par):
    while par[x] >= 0:
        x = par[x]
    return x

def unite(x,y,par):
    rx = root(x,par)
    ry = root(y,par)
    if rx != ry:
        if rx > ry:
            rx, ry = ry, rx
        par[rx] += par[ry]
        par[ry] = rx

def same(x,y,par):
    rx = root(x,par)
    ry = root(y,par)
    return rx == ry

def size(x, par):
    return -par[root(x,par)]

N, K, L = map(int, input().split())
par1 = [-1]*N
par2 = [-1]*N

for i in range(K):
    A, B = map(int, input().split())
    A, B = A-1, B-1
    unite(A, B, par1)

for i in range(L):
    C, D = map(int, input().split())
    C, D = C-1, D-1
    unite(C, D, par2)

G = defaultdict(list)
for i in range(N):
    G[(root(i, par1), root(i, par2))].append(i)
Ans = [0]*N
for city_list in G.values():
    for city in city_list:
        Ans[city] = len(city_list)
for i in Ans:
    print(i, end=" ")

Solution (Python):
from collections import defaultdict


class DisjointSet:

    def __init__(self, N):
        self.parents = [i for i in range(N)]
        self.count = N

    def find(self, x):
        path = []
        while self.parents[x] != x:
            x = self.parents[x]
            path.append(x)
        for p in path:
            self.parents[p] = x
        return x

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)
        if self.find(root_x) != self.find(root_y):
            self.parents[root_x] = root_y
            self.count -= 1


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def read_oneless_ints():
    return [int(a)-1 for a in input().strip().split(' ')]


def solve():
    N, K, L = read_ints()
    disjoint_set_roads = DisjointSet(N)
    disjoint_set_railways = DisjointSet(N)

    for _ in range(K):
        u, v = read_oneless_ints()
        disjoint_set_roads.union(u, v)
    for _ in range(L):
        u, v = read_oneless_ints()
        disjoint_set_railways.union(u, v)
    G = defaultdict(list) 
    for i in range(N):
        G[(disjoint_set_roads.find(i), disjoint_set_railways.find(i))].append(i)
    answer = [None]*N
    for city_list in G.values():
        for city in city_list:
            answer[city] = len(city_list)
    for a in answer:
        print(a)


if __name__ == '__main__':
    solve()

Solution (Python):
import numpy as np
from collections import Counter
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix

int1 = lambda x: int(x) - 1
N, K, L = map(int, input().split())
roads = np.array([tuple(map(int1, input().split())) for _ in range(K)]).T
railways = np.array([tuple(map(int1, input().split())) for _ in range(L)]).T


def connected(G, M):
    matr = csr_matrix((np.ones(M, dtype=int), G), shape=(N, N))
    _, labels = connected_components(matr)
    return labels


roads_connection = connected(roads, K).tolist()
railways_connection = connected(railways, L).tolist()

ct = Counter((roads_connection[i], railways_connection[i]) for i in range(N))
ans = [ct[(roads_connection[i], railways_connection[i])] for i in range(N)]
print(*ans)

Solution (Python):
n,k,l=map(int,input().split())
load=[[] for _ in range(n)]
train=[[] for _ in range(n)]
for _ in range(k):
  a,b=map(int,input().split())
  a-=1
  b-=1
  load[a].append(b)
  load[b].append(a)
for _ in range(l):
  a,b=map(int,input().split())
  a-=1
  b-=1
  train[a].append(b)
  train[b].append(a)


load_grp_id=[-1]*n
not_grp=set(range(n))
while not_grp:
  t0=not_grp.pop()
  todo=[t0]
  load_grp_id[t0]=t0
  while todo:
    t=todo.pop()
    l=load[t]
    for li in l:
      if load_grp_id[li]==-1:
        load_grp_id[li]=t0
        not_grp.discard(li)
        todo.append(li)

train_grp_id=[-1]*n
not_grp=set(range(n))
while not_grp:
  t0=not_grp.pop()
  todo=[t0]
  train_grp_id[t0]=t0
  while todo:
    t=todo.pop()
    l=train[t]
    for li in l:
      if train_grp_id[li]==-1:
        train_grp_id[li]=t0
        not_grp.discard(li)
        todo.append(li)

d={}
for i in range(n):
  lid=load_grp_id[i]
  tid=train_grp_id[i]
  if (lid,tid) in d:
    d[(lid,tid)]+=1
  else:
    d[(lid,tid)]=1
ans=[]
for i in range(n):
  lid=load_grp_id[i]
  tid=train_grp_id[i]
  ans.append(str(d[(lid,tid)]))
print(' '.join(ans))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
class UnionFind():
    def __init__(self,n):
        self.n = n
        self.parents = [-1]*n

    def find(self,x):
        if self.parents[x]<0:
            return x
        else:
            self.parents[x]=self.find(self.parents[x])
            return self.parents[x]
    
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x==y:
            return
        if self.parents[x]>self.parents[y]:
            x,y=y,x
        self.parents[x]+=self.parents[y]
        self.parents[y]=x
    
    def size(self,x):
        return -self.parents[self.find(x)]

    def same(self,x,y):
        return self.find(x)==self.find(y)

    def members(self,x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i)==root]

    def roots(self):
        return [i for i,x in enumerate(self.parents) if x>0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
n,k,l=map(int,input().split())
load=UnionFind(n+1)
tetsu=UnionFind(n+1)
for _ in range(k):
    p,q=map(int,input().split())
    load.union(p,q)
for _ in range(l):
    r,s = map(int,input().split())
    tetsu.union(r,s)

count = [(load.find(i),tetsu.find(i)) for i in range(1,n+1)]

C = Counter(count)
ans = []
for i in range(n):
	if count[i][0]<0 or count[i][1]<0:
		ans.append(0)
	else:
		ans.append(C[count[i]])
print(*ans)

Solution (Python):
import bisect
class UnionFind:

    
    def __init__(self,n):
        self.par = list(range(n))
        self.rank = [0]*n
        self.memo = {}

    
    def find(self,x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    
    
    def unite(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return None
        
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    
    def same(self,x,y):
        return self.find(x) == self.find(y)

if __name__ == '__main__':
    n,k,l = map(int,input().split())
    UF_k = UnionFind(n)
    UF_l = UnionFind(n)
    for i in range(k):
        p,q = map(int,input().split())
        UF_k.unite(p-1,q-1)
    for i in range(l):
        r,s = map(int,input().split())
        UF_l.unite(r-1,s-1)
    l = []
    dic = {}
    for i in range(n):
        tmp = (UF_k.find(i),UF_l.find(i))
        l.append(tmp)
        if tmp in dic:
            dic[tmp] += 1
        else:
            dic[tmp] = 1
    
    for fac in l:
        print(dic[fac],end=' ')

Solution (Python):

import sys
from collections import Counter, defaultdict
input = sys.stdin.readline
N, K, L = map(int, input().split())


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * (n+1)

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n+1) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


A = UnionFind(N)
B = UnionFind(N)


ans = [0 for i in range(N+1)]
for _ in range(K):
    p, q = map(int, input().split())
    A.union(p, q)
for _ in range(L):
    p, q = map(int, input().split())
    B.union(p, q)

members = defaultdict(list)
for i in range(1, N+1):
    K = A.find(i)
    members[A.find(i)].append(i)

roots = A.roots()

for parent in roots:
    if parent == 0:
        continue

    group = members[parent]
    
    B_parent = [B.find(child) for child in group]
    C = dict(Counter(B_parent))
    for child in group:
        ans[child] = C[B.find(child)]
print(*ans[1:])

Solution (Python):
from collections import defaultdict
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, N):
        self.parent = list(range(N))
        self.rank = [0] * N
        self.size = [1] * N

    def find(self, x):
        r = x
        while self.parent[r] != r:
            r = self.parent[r]
        while self.parent[x] != r:
            x, self.parent[x] = self.parent[x], r
        return r

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.size[y] += self.size[x]
        else:
            self.parent[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]

N, K, L = map(int, input().split())
uf1 = UnionFind(N+1)
for _ in range(K):
    p, q = map(int, input().split())
    uf1.unite(p, q)
uf2 = UnionFind(N+1)
for _ in range(L):
    r, s = map(int, input().split())
    uf2.unite(r, s)
cnt = defaultdict(int)
for i in range(1, N+1):
    cnt[(uf1.find(i), uf2.find(i))] += 1
ans = [cnt[(uf1.find(i), uf2.find(i))] for i in range(1, N+1)]
print(*ans)

Solution (Python):
from collections import defaultdict
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, N):
        self.parent = list(range(N))
        self.rank = [0] * N
        self.size = [1] * N

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.size[y] += self.size[x]
        else:
            self.parent[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]

N, K, L = map(int, input().split())
uf1 = UnionFind(N+1)
for _ in range(K):
    p, q = map(int, input().split())
    uf1.unite(p, q)
uf2 = UnionFind(N+1)
for _ in range(L):
    r, s = map(int, input().split())
    uf2.unite(r, s)
cnt = defaultdict(int)
for i in range(1, N+1):
    cnt[(uf1.find(i), uf2.find(i))] += 1
ans = [cnt[(uf1.find(i), uf2.find(i))] for i in range(1, N+1)]
print(*ans)

Solution (Python):
from collections import defaultdict
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, N):
        self.parent = list(range(N))
        self.rank = [0] * N
        self.size = [1] * N

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.size[y] += self.size[x]
        else:
            self.parent[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]

N, K, L = map(int, input().split())
uf1 = UnionFind(N+1)
for _ in range(K):
    p, q = map(int, input().split())
    uf1.unite(p, q)
uf2 = UnionFind(N+1)
cnt = [defaultdict(int) for _ in range(N+1)]
for i in range(1, N+1):
    cnt[i][uf1.find(i)] = 1
for _ in range(L):
    r, s = map(int, input().split())
    if uf2.same(r, s):
        continue
    a = uf2.find(r)
    b = uf2.find(s)
    uf2.unite(r, s)
    c = uf2.find(r)
    if a == c:
        for k, v in cnt[b].items():
            cnt[c][k] += v
    else:
        for k, v in cnt[a].items():
            cnt[c][k] += v
ans = [0] * (N+1)
for i in range(1, N+1):
    ans[i] = cnt[uf2.find(i)][uf1.find(i)]
print(*ans[1:])

Solution (Python):

class UnionFind():
    def __init__(self, n, m = None, read = 0):
        self.n = n
        self.parents = [-1] * n
        if read:
            if m is None:
                raise ValueError("What is M")
            for i in range(m):
                a, b = map(int, input().split())
                self.union(a - 1, b - 1)

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

C = []
n, k, l = map(int, input().split())
UF_R = UnionFind(n, k, 1)
UF_T = UnionFind(n, l, 1)
for i in range(n):
    R = UF_R.find(i)
    T = UF_T.find(i)
    C += (R, T),
from collections import*
c = Counter(C)
print(*[c[g] for g in C])

Solution (Python):
N, M, L = [int(s) for s in input().split()]
parent_road = list(range(N))
parent_train = list(range(N))
rank_road = [0] * N
rank_train = [0] * N


class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * N

    def find(self, x):
        if x == self.parent[x]:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1


edge_road = [[int(s) - 1 for s in input().split()] for _ in range(M)]
edge_train = [[int(s) - 1 for s in input().split()] for _ in range(L)]

group_road = UnionFind(N)
group_train = UnionFind(N)

for x, y in edge_road:
    group_road.union(x, y)

for x, y in edge_train:
    group_train.union(x, y)

group_count = {}
group_list = []
for i in range(N):
    gr = group_road.find(i)
    gt = group_train.find(i)
    group_list.append((gr, gt))
    if (gr, gt) in group_count.keys():
        group_count[(gr, gt)] += 1
    else:
        group_count[(gr, gt)] = 1

print(' '.join([str(group_count[g]) for g in group_list]))

Solution (Python):
from collections import deque, Counter

N, K, L = [int(s) for s in input().split()]
edge_road = [[int(s) - 1 for s in input().split()] for _ in range(K)]
edge_train = [[int(s) - 1 for s in input().split()] for _ in range(L)]
graph_road = [[] for _ in range(N)]
graph_train = [[] for _ in range(N)]

for i, j in edge_road:
    graph_road[i].append(j)
    graph_road[j].append(i)

for i, j in edge_train:
    graph_train[i].append(j)
    graph_train[j].append(i)

group_road = [0] * N
group_num = 0

visited = [False]*N

for i in range(N):
    if visited[i]:
        continue
    dq = deque([i])
    group_road[i] = group_num
    visited[i] = True
    while(dq):
        search_obj = dq.pop()
        for candidate_obj in graph_road[search_obj]:
            if not visited[candidate_obj]:
                group_road[candidate_obj] = group_num
                dq.appendleft(candidate_obj)
                visited[candidate_obj] = True
    group_num += 1


group_train = [0] * N
group_num = 0

visited = [False]*N

for i in range(N):
    if visited[i]:
        continue
    dq = deque([i])
    group_train[i] = group_num
    visited[i] = True
    while(dq):
        search_obj = dq.pop()
        for candidate_obj in graph_train[search_obj]:
            if not visited[candidate_obj]:
                group_train[candidate_obj] = group_num
                dq.appendleft(candidate_obj)
                visited[candidate_obj] = True
    group_num += 1

group_count = Counter(zip(group_road, group_train))

for gr, gt in zip(group_road, group_train):
    print(group_count[(gr, gt)], end=' ')

Solution (Python):
class UnionFind():
    
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * n
    
    
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    
    def sama_check(self, x, y):
        return self.find(x) == self.find(y)


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

un_load = UnionFind(n)
un_train = UnionFind(n)

for i in range(k):
    p, q = map(lambda x: int(x) - 1, input().split())
    
    if not un_load.sama_check(p, q):
        un_load.union(p, q)

for i in range(l):
    r, s = map(lambda x: int(x) - 1, input().split())
    
    if not un_train.sama_check(r, s):
        un_train.union(r, s)

load_train = {}
for i in range(n):
    comb = (un_load.find(i), un_train.find(i))
    
    if comb in load_train:
        load_train[comb] += 1
    else:
        load_train[comb] = 1

for i in range(n):
    print(load_train[(un_load.par[i], un_train.par[i])])

Solution (Python):
class UF():
  def __init__(self, N):
    self.par = [-1] * N

  def find(self, n):
    if self.par[n] < 0:
      return n
    else:
      self.par[n] = self.find(self.par[n])
      return self.par[n]

  def union(self, x,y):
    p1,p2 = self.find(x), self.find(y)
    if p1 == p2: return
    if p1 > p2:
      p1,p2 = p2,p1
    self.par[p1] += self.par[p2]
    self.par[p2] = p1

  def same(self, x,y):
    return self.find(x) == self.find(y)

N,K,L = map(int,input().split())
uf_road = UF(N)
uf_railway = UF(N)

for _ in range(K):
  u,v = map(int,input().split())
  u -= 1
  v -= 1
  uf_road.union(u,v)

for _ in range(L):
  u,v = map(int,input().split())
  u -= 1
  v -= 1
  uf_railway.union(u,v)

dic = {}
counter = []
for i in range(N):
  p1 = uf_road.find(i)
  p2 = uf_railway.find(i)
  counter.append((p1,p2))
  if (p1,p2) not in dic:
    dic[(p1,p2)] = 1
  else:
    dic[(p1,p2)] += 1

for i in range(N):
  print(dic[counter[i]], end=" ")

Solution (Python):
from collections import deque

def bfs():
    q = deque()
    color1 = [-1] * (n + 1)
    color2 = [-1] * (n + 1)
    for i in range(1, n + 1):
        if color1[i] == -1:
            color1[i] = i
            q.append(i)
            while q:
                p = q.popleft()
                for j in G[p]:
                    if color1[j] == -1:
                        color1[j] = color1[p]
                        q.append(j)
        if color2[i] == -1:
            color2[i] = i
            q.append(i)
            while q:
                p = q.popleft()
                for j in H[p]:
                    if color2[j] == -1:
                        color2[j] = color2[p]
                        q.append(j)
    group = [color1[i] + 200000 * color2[i] for i in range(n + 1)]
    return group

n, k, l = map(int, input().split())
G = [[] for _ in range(n + 1)]
H = [[] for _ in range(n + 1)]
for _ in range(k):
    p, q = map(int, input().split())
    G[p].append(q)
    G[q].append(p)
for _ in range(l):
    r, s = map(int, input().split())
    H[r].append(s)
    H[s].append(r)
group = bfs()
d = {}
for i in range(1, n + 1):
    if group[i] in d:
        d[group[i]] += 1
    else:
        d[group[i]] = 1
ans = [0] * n
for i in range(1, n + 1):
    ans[i - 1] = d[group[i]]
print(" ".join(map(str, ans)))

Solution (Python):



class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def find(self, x):          
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def same(self, x, y):       
        return self.find(x) == self.find(y)

    def size(self, x):          
        return -self.parents[self.find(x)]

    def members(self, x):       
        root = self.find(x)
        return {i for i in range(self.n) if self.find(i) == root}

    def roots(self):            
        return {i for i, x in enumerate(self.parents) if x < 0}

    def group_count(self):      
        return len(self.roots())

    def all_group_members(self):      
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())



from collections import Counter

def main():
    N, K, L = map(int, input().split())
    ufK = UnionFind(N)
    ufL = UnionFind(N)
    for i in range(K):
        a,b = map(int,input().split())
        ufK.union(a-1, b-1)
    for i in range(L):
        a,b = map(int,input().split())
        ufL.union(a-1, b-1)

    ps=[(ufK.find(i),ufL.find(i)) for i in range(N)]
    cps = Counter(ps)
    ret = [0]*N
    for i in range(N):
        ret[i] =cps[ps[i]]
    print(*ret)

if __name__ == '__main__':
    main()

Solution (Python):
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.root = [-1] * (n + 1)
		self.rank = [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 0
		elif self.rank[x] > self.rank[y]:
			self.root[x] += self.root[y]
			self.root[y] = x
		else:
			self.root[y] += self.root[x]
			self.root[x] = y
			if self.rank[x] == self.rank[y]:
				self.rank[y] += 1

	def isSame(self, x, y):
		return self.find(x) == self.find(y)

	def size(self, x):
		return - self.root[self.find(x)]

import sys
input = sys.stdin.buffer.readline
from collections import Counter
n,k,l=map(int,input().split())
uf1=UnionFind(n)
uf2=UnionFind(n)
for _ in range(k):
	uf1.unite(*map(int,input().split()))
for _ in range(l):
	uf2.unite(*map(int,input().split()))
ps=[(uf1.find(i),uf2.find(i)) for i in range(1,n+1)]
C=Counter(ps)
ans=[]
for i in range(n):
	if ps[i][0]<0 or ps[i][1]<0:
		ans.append(0)
	else:
		ans.append(C[ps[i]])
print(*ans)

Solution (Python):
from collections import Counter
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

N,K,L = map(int,input().split())
l = []
ufp = UnionFind(N)
ufr = UnionFind(N)
for i in range(K):
    p,q = map(int, input().split())
    ufp.union(p-1, q-1)
for i in range(L):
    r,s = map(int,input().split())
    ufr.union(r-1, s-1)

for i in range(N):
    tup = (ufp.find(i), ufr.find(i))
    l.append(tup)
c = Counter(l)
for i in range(N):
    
    print(c[l[i]], end=" ")

Solution (Python):
from collections import Counter
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

N,K,L = map(int,input().split())
l = []
ufp = UnionFind(N)
ufr = UnionFind(N)
for i in range(K):
    p,q = map(int, input().split())
    ufp.union(p-1, q-1)
for i in range(L):
    r,s = map(int,input().split())
    ufr.union(r-1, s-1)

for i in range(N):
    tup = (ufp.find(i), ufr.find(i))
    l.append(tup)
c = Counter(l)
for i in range(N):
    
    print(c[l[i]], end=" ")

Solution (Python):
from collections import defaultdict, Counter
class UF():
  def __init__(self, N):
    self.parents = [-1] * (N+1)

  def union(self, f, t):
    p1 = self.find(f)
    p2 = self.find(t)
    if p1 == p2:
      return

    if self.parents[p1] > self.parents[p2]:
      p1,p2 = p2,p1
    self.parents[p1] += self.parents[p2]
    self.parents[p2] = p1

  def find(self, node):
    if self.parents[node] < 0:
      return node
    else:
      self.parents[node] = self.find(self.parents[node])
      return self.parents[node]

  
  
  
  
  
  

N,K,L = map(int, input().split())
uf_road = UF(N)
uf_railway = UF(N)

for _ in range(K):
  f,t = map(int, input().split())
  uf_road.union(f,t)

for _ in range(L):
  f,t = map(int, input().split())
  uf_railway.union(f,t)

l = []
for i in range(1, N+1):
  l.append((uf_road.find(i), uf_railway.find(i)))

c = Counter(l)
for i in range(N):
  print(c[l[i]], end=" ")