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):
import copy
import itertools


def flatdupdel(fl):
    return set(list(itertools.chain.from_iterable(fl)))


def find_root(roads, search):
    # s_list = [search]
    global rootlist
    if len(roads) >= 1 and len(search) >= 1:
        for num in search:
            list_mutch = []
            for ind, road in enumerate(roads):
                if num in road:
                    list_mutch.append(road)
                    roads.pop(ind)
            if len(list_mutch) >= 1:
                li = flatdupdel(list_mutch)
                rootlist.append(li)
                find_root(roads, li)
    return 0


def find_dup(l1, l2):
    j_list = l1 + l2
    return [x for x in set(j_list) if j_list.count(x) > 1]


n, k, l = map(int, input().split())
w = [list(map(int, input().split())) for i in range(k+l)]

ro = w[:k]
ra = w[l:]

ro_roots = {}
ra_roots = {}
answer = []

for num in range(1, n+1):
    ro_c = copy.copy(ro)
    rootlist = []
    find_root(ro_c, [num])
    final = list(flatdupdel(rootlist))
    if final:
        final.remove(num)
    ro_roots[num] = final

for num in range(1, n+1):
    ra_c = copy.copy(ra)
    rootlist = []
    find_root(ra_c, [num])
    final = list(flatdupdel(rootlist))
    if final:
        final.remove(num)
    ra_roots[num] = final

for i in range(1, n+1):
    answer.append(len(find_dup(ro_roots[i], ra_roots[i])) + 1)

print(*answer)

Fixed, accepted submission:
from collections import*
import sys
input = sys.stdin.readline


class UnionFind(object):
    def __init__(self, n):
        
        
        
        self.parents = [-1 for i in range(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
        return

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

ro_cl = UnionFind(n)
ra_cl = UnionFind(n)
d = defaultdict(int)

# =================================================================



# ra = w[l:]
# for p, q in ro:

# for p, q in ra:


# =================================================================

for i in range(k):
    p, q = map(int, input().split())
    ro_cl.union(p-1, q-1)
for i in range(l):
    p, q = map(int, input().split())
    ra_cl.union(p-1, q-1)

# =================================================================

for i in range(n):
    d[(ro_cl.find(i), ra_cl.find(i))] += 1
print(*[d[(ro_cl.find(i), ra_cl.find(i))]for i in range(n)])