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



Sample input:
4 3 1
1 2
2 3
3 4
2 3


Sample output:
1 2 2 1

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)


def unite(par, i, j):
    j = get_par(par, j)
    par[j] = get_par(par, i)

def get_par(par, p):
    while par[p] != p:
        p = par[p]
    return par[p]

n,k,l = map(int, input().split())
pq = []
road = [i for i in range(n+1)]
rcnt = [1 for _ in range(n+1)]
rail = [i for i in range(n+1)]
acnt = [1 for _ in range(n+1)]
for i in range(k):
    p,q = map(int,input().split())
    pq.append((p,q))
    aa = get_par(road, p)
    bb = get_par(road, q)
    if aa == bb:
        continue
    # unite
    if rcnt[aa] > rcnt[bb]:
        road[bb] = aa
        rcnt[aa] += rcnt[bb]
    else:
        road[aa] = bb
        rcnt[bb] += rcnt[aa]

rs = []
for i in range(l):
    p,q = map(int,input().split())
    rs.append((p,q))
    aa = get_par(rail, p)
    bb = get_par(rail, q)
    if aa == bb:
        continue
    # unite
    if acnt[aa] > acnt[bb]:
        rail[bb] = aa
        acnt[aa] += acnt[bb]
    else:
        rail[aa] = bb
        acnt[bb] += acnt[aa]

ans = [0 for _ in range(n+1)]
for i in range(1,n+1):
    if ans[i] == 0:
        rr = set([j for j in range(1,n+1) if road[i] == road[j]])
        ll = set([j for j in range(1,n+1) if rail[i] == rail[j]])
        rl = rr & ll
        for j in rl:
            ans[j] = len(rl)

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import*
# import sys
# input = sys.stdin.readline


class UnionFind(object):
    def __init__(self, n):
        # 親の番号を格納する。親だった場合は-(その集合のサイズ)
        # 作るときはParentsの値を全て-1にする
        # こうすると全てバラバラになる
        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)

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

w = [list(map(int, input().split())) for i in range(k+l)]
ro = w[:k]
ra = w[l:]
for p, q in ro:
    ro_cl.union(p-1, q-1)
for p, q in ra:
    ra_cl.union(p-1, q-1)

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

# 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)])

Submission status: Wrong Answer

Solution code:
from collections import*
import sys
input = sys.stdin.readline


class UnionFind(object):
    def __init__(self, n):
        # 親の番号を格納する。親だった場合は-(その集合のサイズ)
        # 作るときはParentsの値を全て-1にする
        # こうすると全てバラバラになる
        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)

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

w = [list(map(int, input().split())) for i in range(k+l)]
ro = w[:k]
ra = w[l:]
for p, q in ro:
    ro_cl.union(p-1, q-1)
for p, q in ra:
    ra_cl.union(p-1, q-1)

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

# 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)])

Submission status: Wrong Answer

Solution code:
from collections import*
import sys
input = sys.stdin.readline


class UnionFind(object):
    def __init__(self, n):
        # 親の番号を格納する。親だった場合は-(その集合のサイズ)
        # 作るときはParentsの値を全て-1にする
        # こうすると全てバラバラになる
        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())
w = [list(map(int, input().split())) for i in range(k+l)]
ro = w[:k]
ra = w[l:]

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

for p, q in ro:
    ro_cl.union(p-1, q-1)
for p, q in ra:
    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)])

Submission status: Wrong Answer

Solution code:
from collections import*
import sys
input = sys.stdin.readline


class UnionFind(object):
    def __init__(self, n):
        # 親の番号を格納する。親だった場合は-(その集合のサイズ)
        # 作るときはParentsの値を全て-1にする
        # こうすると全てバラバラになる
        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

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


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_cl = UnionFind(n)
ra_cl = UnionFind(n)
d = defaultdict(int)

for p, q in ro:
    ro_cl.union(p-1, q-1)
for p, q in ra:
    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)])

Submission status: Wrong Answer

Solution code:
import sys
from collections import*


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):
        find = self.find(x)
        return [i for i in range(self.n) if self.find(i) == find]

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

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

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

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


input = sys.stdin.readline
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:]
d = defaultdict(int)

ro_cl = UnionFind(n)
ra_cl = UnionFind(n)

for p, q in ro:
    ro_cl.union(p-1, q-1)

for p, q in ra:
    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)])

Submission status: Wrong Answer

Solution code:
import sys


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):
        find = self.find(x)
        return [i for i in range(self.n) if self.find(i) == find]

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

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

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

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


input = sys.stdin.readline
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:]
answer = ""

ro_cl = UnionFind(n)
ra_cl = UnionFind(n)

for p, q in ro:
    ro_cl.union(p-1, q-1)

for p, q in ra:
    ra_cl.union(p-1, q-1)

for i in range(n):
    print(ro_cl.members(i))
    print(ra_cl.members(i))
    if i != 0:
        answer += " "
    answer += str(len([n for n in ro_cl.members(i) if n in ra_cl.members(i)]))

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import*


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):
        find = self.find(x)
        return [i for i in range(self.n) if self.find(i) == find]

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

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

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

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


input = sys.stdin.readline
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:]
answer = ""
d = defaultdict(int)

ro_cl = UnionFind(n)
ra_cl = UnionFind(n)

for p, q in ro:
    ro_cl.union(p-1, q-1)

for p, q in ra:
    ra_cl.union(p-1, q-1)

for i in range(n):
    # if i != 0:
    #     answer += " "
    # # j_list = ro_cl.members(i) + ra_cl.members(i)
    # answer += str(len([n for n in ro_cl.members(i) if n in ra_cl.members(i)]))
    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)])

Submission status: Wrong Answer

Solution code:
import sys


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())


input = sys.stdin.readline
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:]
answer = ""

ro_cl = UnionFind(n)
ra_cl = UnionFind(n)

for p, q in ro:
    ro_cl.union(p-1, q-1)

for p, q in ra:
    ra_cl.union(p-1, q-1)

for i in range(n):
    if i != 0:
        answer += " "
    # j_list = ro_cl.members(i) + ra_cl.members(i)
    answer += str(len([n for n in ro_cl.members(i) if n in ra_cl.members(i)]))

print(answer)

Submission status: Wrong Answer

Solution code:
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())
w = [list(map(int, input().split())) for i in range(k+l)]

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

ro_cl = UnionFind(n)
ra_cl = UnionFind(n)

for p, q in ro:
    ro_cl.union(p-1, q-1)

for p, q in ra:
    ra_cl.union(p-1, q-1)

for i in range(n):
    j_list = ro_cl.members(i) + ra_cl.members(i)
    answer.append(len([x for x in set(j_list) if j_list.count(x) > 1]))

print(*answer)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# ユニオンファインド
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())
p= [list(map(int, input().split())) for i in range(k)]
r = [list(map(int, input().split())) for i in range(l)]
uf1=UnionFind(n)
uf2=UnionFind(n)
for i in range(k):
    uf1.union(p[i][0]-1,p[i][1]-1)

for i in range(l):
    uf2.union(r[i][0]-1,r[i][1]-1)
ans=[1]*n
print(uf1.find(3),uf2.find(3))
for i in range(n):
    for j in range(n):
        if i==j:
            continue
        if uf2.same(i,j):
            if uf1.same(i,j):
                ans[i]+=1

print(*ans)

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = [-1 for _ in range(n)]

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

    def root(self, x):
        if self.par[x]<0:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def unite(self,x,y):
        x = self.root(x)
        y = self.root(y)
        if x==y:
            return

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

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

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

from collections import defaultdict

n,k,l = map(int, input().split())
u = UnionFind(n+1)
v = UnionFind(n+1)

for _ in range(k):
  p,q = map(int, input().split())
  u.unite(p,q)

for _ in range(l):
  r,s = map(int, input().split())
  if u.same(r,s):
    v.unite(r,s)

res = []
for i in range(1,n+1):
  res.append(v.size(i))
print(*res)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from functools import reduce


class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [1 for _ in range(n)]

    def findRoot(self, i):
        if self.parent[i] == i:  # 親が自分ならroot
            return i
        else:
            self.parent[i] = self.findRoot(self.parent[i])  # 親をrootに付け替える
            return self.parent[i]

    def unite(self, i, j):
        i_root = self.findRoot(i)
        j_root = self.findRoot(j)
        if i_root == j_root:
            return

        if self.rank[i_root] > self.rank[j_root]:  # ランクが高い方を親にする。
            self.parent[j_root] = i_root
        elif self.rank[i_root] == self.rank[j_root]:
            self.parent[j_root] = i_root
            self.rank[i_root] += 1
        else:
            self.parent[i_root] = j_root

        return i

    def isSame(self, i, j):
        return self.findRoot(i) == self.findRoot(j)

    def setGroup(self, elements):
        reduce(self.unite, elements)


class UnionFindCounter:
    def __init__(self, n):
        self.N = n
        self.parent = list(range(n))
        self.rank = [1 for _ in range(n)]
        self._count = [1 for _ in range(n)]

    def findRoot(self, i):
        if self.parent[i] == i:  # 親が自分ならroot
            return i
        else:
            self.parent[i] = self.findRoot(self.parent[i])  # 親をrootに付け替える
            return self.parent[i]

    def unite(self, i, j):
        i_root = self.findRoot(i)
        j_root = self.findRoot(j)
        if i_root == j_root:
            return

        if self.rank[i_root] > self.rank[j_root]:  # ランクが高い方を親にする。
            self.parent[j_root] = i_root
            self._count[i_root] += self._count[j_root]
        elif self.rank[i_root] == self.rank[j_root]:
            self.parent[j_root] = i_root
            self.rank[i_root] += 1
            self._count[i_root] += self._count[j_root]
        else:
            self.parent[i_root] = j_root
            self._count[j_root] += self._count[i_root]

        return i

    def isSame(self, i, j):
        return self.findRoot(i) == self.findRoot(j)

    def setGroup(self, elements):
        reduce(self.unite, elements)

    def getCount(self):
        return [self._count[self.findRoot(i)] for i in range(self.N)]


def resolve():
    # S = [x for x in sys.stdin.readline().split()][0]  # 文字列 一つ
    # N = [int(x) for x in sys.stdin.readline().split()][0]  # int 一つ
    N, K, L = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)]  # 文字列grid
    grid = [
        tuple([int(x) - 1 for x in sys.stdin.readline().split()])
        for _ in range(K + L)
    ]  # int grid

    logger.debug('{}'.format([]))

    p_uf = UnionFind(N)
    r_uf = UnionFind(N)
    both_uf = UnionFindCounter(N)

    for k in range(K):
        p, q = grid[k]
        p_uf.unite(p, q)

    for l in range(K, K + L):
        r, s = grid[l]
        r_uf.unite(r, s)
    for (p, q) in set(grid):
        if p_uf.isSame(p, q) and r_uf.isSame(p, q):
            both_uf.unite(p, q)

    print(' '.join([str(x) for x in both_uf.getCount()]))


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4 3 1
1 2
2 3
3 4
2 3"""
        output = """1 2 2 1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4 2 2
1 2
2 3
1 4
2 3"""
        output = """1 2 2 1"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7"""
        output = """1 1 2 1 2 2 2"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """2 1 1
1 2
1 2"""
        output = """2 2"""
        self.assertIO(input, output)

    def test_入力例_5(self):
        input = """4 3 3
1 2
1 4
1 3
1 4
4 3
2 3"""
        output = """4 4 4 4"""
        self.assertIO(input, output)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from functools import reduce


class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [1 for _ in range(n)]

    def findRoot(self, i):
        if self.parent[i] == i:  # 親が自分ならroot
            return i
        else:
            self.parent[i] = self.findRoot(self.parent[i])  # 親をrootに付け替える
            return self.parent[i]

    def unite(self, i, j):
        i_root = self.findRoot(i)
        j_root = self.findRoot(j)
        if i_root == j_root:
            return

        if self.rank[i_root] > self.rank[j_root]:  # ランクが高い方を親にする。
            self.parent[j_root] = i_root
        elif self.rank[i_root] == self.rank[j_root]:
            self.parent[j_root] = i_root
            self.rank[i_root] += 1
        else:
            self.parent[i_root] = j_root

        return i

    def isSame(self, i, j):
        return self.findRoot(i) == self.findRoot(j)

    def setGroup(self, elements):
        reduce(self.unite, elements)


class UnionFindCounter:
    def __init__(self, n):
        self.N = n
        self.parent = list(range(n))
        self.rank = [1 for _ in range(n)]
        self._count = [1 for _ in range(n)]

    def findRoot(self, i):
        if self.parent[i] == i:  # 親が自分ならroot
            return i
        else:
            self.parent[i] = self.findRoot(self.parent[i])  # 親をrootに付け替える
            return self.parent[i]

    def unite(self, i, j):
        i_root = self.findRoot(i)
        j_root = self.findRoot(j)
        if i_root == j_root:
            return

        if self.rank[i_root] > self.rank[j_root]:  # ランクが高い方を親にする。
            self.parent[j_root] = i_root
            self._count[i_root] += self._count[j_root]
        elif self.rank[i_root] == self.rank[j_root]:
            self.parent[j_root] = i_root
            self.rank[i_root] += 1
            self._count[i_root] += self._count[j_root]
        else:
            self.parent[i_root] = j_root
            self._count[j_root] += self._count[i_root]

        return i

    def isSame(self, i, j):
        return self.findRoot(i) == self.findRoot(j)

    def setGroup(self, elements):
        reduce(self.unite, elements)

    def getCount(self):
        return [self._count[self.findRoot(i)] for i in range(self.N)]


def resolve():
    # S = [x for x in sys.stdin.readline().split()][0]  # 文字列 一つ
    # N = [int(x) for x in sys.stdin.readline().split()][0]  # int 一つ
    N, K, L = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)]  # 文字列grid
    grid = [[int(x) - 1 for x in sys.stdin.readline().split()]
            for _ in range(K + L)]  # int grid

    logger.debug('{}'.format([]))

    p_uf = UnionFind(N)
    r_uf = UnionFind(N)
    both_uf = UnionFindCounter(N)

    for k in range(K):
        p, q = grid[k]
        p_uf.unite(p, q)

    for l in range(K, K + L):
        r, s = grid[l]
        r_uf.unite(r, s)
        if p_uf.isSame(r, s):
            both_uf.unite(r, s)

    print(' '.join([str(x) for x in both_uf.getCount()]))


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4 3 1
1 2
2 3
3 4
2 3"""
        output = """1 2 2 1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4 2 2
1 2
2 3
1 4
2 3"""
        output = """1 2 2 1"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7"""
        output = """1 1 2 1 2 2 2"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """2 1 1
1 2
1 2"""
        output = """2 2"""
        self.assertIO(input, output)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def deep_search(L,i,f):
    Q=L[i]
    if f[i]==0:
        f[i]=i
        for j in L[i]:
            f[j]=i
        while Q!=[]:
            p=Q.pop()
            Q=Q+L[p]
            for j in L[p]:
                f[j]=i
    return f

N,K,L=input().split(" ")
N=int(N)
K=int(K)
L=int(L)
a=[0 for i in range(N+1)]
b=[0 for i in range(N+1)]
road=[[] for i in range(N+1)]
train=[[] for i in range(N+1)]
for i in range(K+L):
    p,q=input().split(" ")
    p=int(p)
    q=int(q)
    if i<K:
        road[p].append(q)
    else:
        train[p].append(q)
for i in range(1,N+1):
    a=deep_search(road,i,a)
    b=deep_search(train,i,b)
c=defaultdict(lambda:0)
for i in range(1,N+1):
    c[a[i],b[i]]+=1
for i in range(1,N+1):
    print(c[a[i],b[i]],end=" ")