Problem:
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.

Incorrect submission (verdict: Wrong Answer):
from operator import itemgetter
import sys
input = sys.stdin.readline

N, K, L = map(int, input().split())
PQ = [list(map(int, input().split())) for _ in range(K)]
RS = [list(map(int, input().split())) for _ in range(L)]


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

    
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    
    def Unite(self, x, y):
        
        x = self.Find_Root(x)
        y = self.Find_Root(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 isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    
    def Count(self, x):
        return -self.root[self.Find_Root(x)]


A = UnionFind(N)
for p, q in PQ:
    A.Unite(p, q)

B = UnionFind(N)
for r,s in RS:
    B.Unite(r, s)

C = []
for i in range(1, N+1):
    a = A.root[i] if A.root[i] > 0 else i
    b = B.root[i] if B.root[i] > 0 else i
    C.append([i, a, b])

C.sort(key=itemgetter(2))
C.sort(key=itemgetter(1))
D = [1 for _ in range(N)]
pa, pb = 0, 0
c = 0
P = []
for p, a, b in C:
    P.append(p)
    if pa == a and pb == b:
        c += 1
        D[p-1] += c
    else:
        c = 0
    pa, pb = a, b
#print(D)
#print(P)
Q = P[::-1]
#print(Q)
for i, p in enumerate(Q):
    a = D[p-1]
    #print(i, p, a, pa)
    if i == 0:
        l = a
        pa = a
        continue
    if pa != 1 or a != 1:
        l = max(l, a)
        D[p-1] = l
    else:
        l = 1
    pa = a

for a in D:
    print(a, end=' ')
print()

Fixed, accepted submission:
class UnionFind():
    
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)

    
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    
    
    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(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 isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    
    def Count(self, x):
        return -self.root[self.Find_Root(x)]


N, K, L = map(int, input().split())
U1 = UnionFind(N)
U2 = UnionFind(N)
for _ in range(K):
    a, b = map(int, input().split())
    U1.Unite(a, b)
for _ in range(L):
    a, b = map(int, input().split())
    U2.Unite(a, b)

Roots = [0]
dic = {}
for n in range(1, N+1):
    r1 = U1.Find_Root(n)
    r2 = U2.Find_Root(n)
    r = r1*2*N + r2
    Roots.append(r)
    if not r in dic.keys():
        dic[r] = 1
    else:
        dic[r] += 1
    
ans = []
for n in range(1, N+1):
    ans.append(dic[Roots[n]])

print(" ".join([str(a) for a in ans]))