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:
N,K,L = map(int, input().split())
pq_list = []
for _ in range(K):
    pq_list.append(list(map(int, input().split())))

rs_list = []
for _ in range(L):
    rs_list.append(list(map(int, input().split())))

class UnionFind():
    def __init__(self):
        self.roots = {}
        self.ranks = {}

    def connect(self,x,y):
        root_x = self.pick_root(x)
        root_y = self.pick_root(y)
        if not root_x == root_y:
            if self.ranks[root_x] >= self.ranks[root_y]:
                # 短い方の根を長い方の根にする
                # 等しいときはxにくっつける
                self.roots[y] = root_x
            else:
                self.roots[x] = root_y

        if self.ranks[root_x]==self.ranks[root_y]:
            # 等しいときxにくっつけると、xのランクが1増える
            self.ranks[x] += 1

    def pick_root(self,k):
        if not k in self.roots:
            self.roots[k] = k
            self.ranks[k] = 1
            return k

        k_root = self.roots[k]
        if k_root == k:
            return k
        else:
            # 経路圧縮（のつもり）
            # pick_rootで毎回見に行くのが無駄にも思えるが、いつ根ノードがくっつくかわからないので必要な気がしている
            self.roots[k] = self.pick_root(self.roots[k_root])
            return self.roots[k]
#             return self.pick_root(self.roots[k])

UF_pq = UnionFind()
for p,q in pq_list:
    UF_pq.connect(p, q)

UF_rs = UnionFind()
for r,s in rs_list:
    UF_rs.connect(r, s)

root_tuples = []
for i in range(1,N+1):
    if not i in UF_pq.roots:
        root_tuples.append((None,None))
        continue
    if not i in UF_rs.roots:
        root_tuples.append((None,None))
        continue
    pq_root = UF_pq.roots[i]
    rs_root = UF_rs.roots[i]
    root_tuples.append((pq_root,rs_root))

from collections import Counter
counter = Counter(root_tuples)
counter[(None,None)] = 1

kotae_list = []
for i in range(N):
    kotae_list.append(counter[root_tuples[i]])

print(" ".join(map(str,kotae_list)))

Submission status: Wrong Answer

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

  def union(self, a, b):
    a = self.root(a)
    b = self.root(b)
    if(a == b):
      return
    if(self.size(a) < self.size(b)):
      a, b = b, a
    self.tree[a] += self.tree[b]
    self.tree[b] = a

  def root(self, a):
    if(self.tree[a] < 0):
      return a
    res = self.root(self.tree[a])
    self.tree[a] = res
    return res

  def size(self, a):
    return -self.tree[self.root(a)]

n,k,l = map(int,input().split())
uf1 = UnionFind(n)
uf2 = UnionFind(n)
for i in range(k):
    p,q = map(int,input().split())
    p -= 1
    q -= 1
    uf1.union(p,q)
for i in range(l):
    r,s = map(int,input().split())
    r -= 1
    s -=1
    if uf1.root(r)==uf1.root(s):
        uf2.union(r,s)
for i in range(n):
    print(uf2.size(i),end=" ")

Submission status: Wrong Answer

Solution code:
N,K,L = map(int, input().split())
pq_list = []
for _ in range(K):
    pq_list.append(list(map(int, input().split())))

rs_list = []
for _ in range(L):
    rs_list.append(list(map(int, input().split())))

class UnionFind():
    def __init__(self):
        self.roots = {}
        self.ranks = {}

    def connect(self,x,y):
        root_x = self.pick_root(x)
        root_y = self.pick_root(y)
        if not root_x == root_y:
            if self.ranks[root_x] >= self.ranks[root_y]:
                # 短い方の根を長い方の根にする
                # 等しいときはxにくっつける
                self.roots[y] = root_x
            else:
                self.roots[x] = root_y

        if self.ranks[root_x]==self.ranks[root_y]:
            # 等しいときxにくっつけると、xのランクが1増える
            self.ranks[x] += 1

    def pick_root(self,k):
        if not k in self.roots:
            self.roots[k] = k
            self.ranks[k] = 1
            return k

        k_root = self.roots[k]
        if k_root == k:
            return k
        else:
            # 経路圧縮（のつもり）
            # pick_rootで毎回見に行くのが無駄にも思えるが、いつ根ノードがくっつくかわからないので必要な気がしている
            self.roots[k] = self.pick_root(self.roots[k])
            return self.roots[k]
#             return self.pick_root(self.roots[k])

UF_pq = UnionFind()
for p,q in pq_list:
    UF_pq.connect(p, q)

UF_rs = UnionFind()
for r,s in rs_list:
    UF_rs.connect(r, s)

root_tuples = []
for i in range(1,N+1):
    if not i in UF_pq.roots:
        root_tuples.append((None,None))
        continue
    if not i in UF_rs.roots:
        root_tuples.append((None,None))
        continue
    pq_root = UF_pq.roots[i]
    rs_root = UF_rs.roots[i]
    root_tuples.append((pq_root,rs_root))

from collections import Counter
counter = Counter(root_tuples)
counter[(None,None)] = 1

kotae_list = []
for i in range(N):
    kotae_list.append(counter[root_tuples[i]])

print(" ".join(map(str,kotae_list)))

Submission status: Wrong Answer

Solution code:
N,K,L = map(int, input().split())
pq_list = []
for _ in range(K):
    pq_list.append(list(map(int, input().split())))

rs_list = []
for _ in range(L):
    rs_list.append(list(map(int, input().split())))

class UnionFind():
    def __init__(self):
        self.roots = {}
        self.ranks = {}

    def connect(self,x,y):
        root_x = self.pick_root(x)
        root_y = self.pick_root(y)
        if not root_x == root_y:
            if self.ranks[root_x] >= self.ranks[root_y]:
                # 短い方の根を長い方の根にする
                # 等しいときはxにくっつける
                self.roots[y] = root_x
            else:
                self.roots[x] = root_y

        if self.ranks[root_x]==self.ranks[root_y]:
            # 等しいときxにくっつけると、xのランクが1増える
            self.ranks[x] += 1

    def pick_root(self,k):
        if not k in self.roots:
            self.roots[k] = k
            self.ranks[k] = 1
            return k

        if self.roots[k] == k:
            return k
        else:
            # roots[k] = pick_root(roots[k])を書き足す必要は無いか？
            return self.pick_root(self.roots[k])

UF_pq = UnionFind()

for p,q in pq_list:
    UF_pq.connect(p, q)

UF_rs = UnionFind()
for r,s in rs_list:
    UF_rs.connect(r, s)

root_tuples = []
for i in range(1,N+1):
    if not i in UF_pq.roots:
        root_tuples.append((None,None))
        continue
    if not i in UF_rs.roots:
        root_tuples.append((None,None))
        continue
    pq_root = UF_pq.roots[i]
    rs_root = UF_rs.roots[i]
    root_tuples.append((pq_root,rs_root))

from collections import Counter
counter = Counter(root_tuples)
counter[(None,None)] = 1

text = ""
for i in range(N):
    text += str(counter[root_tuples[i]]) + " "

print(text[:-1])

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, size):
        self.data = [i for i in range(size)]
    def find(self, x):
        if self.data[x] != x:
            self.data[x] = self.find(self.data[x])
        return self.data[x]
    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x != y:
            self.data[x] = min(x, y)
            self.data[y] = self.data[x]

N, K, L = [int(i) for i in input().split()]

uf1 = UnionFind(N)
for _ in range(K):
    p, q = [int(i) - 1 for i in input().split()]
    uf1.union(p, q)

uf2 = UnionFind(N)
for _ in range(L):
    r, s = [int(i) - 1 for i in input().split()]
    uf2.union(r, s)

from collections import Counter
Z = [(a, b) for a, b in zip(uf1.data, uf2.data)]
C = Counter(Z)
print(*[C[z] for z in Z])

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, size):
        self.data = [i for i in range(size)]
    def find(self, x):
        if self.data[x] != x:
            self.data[x] = self.find(self.data[x])
        return self.data[x]
    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x != y:
            self.data[x] = min(x, y)
            self.data[y] = self.data[x]

N, K, L = [int(i) for i in input().split()]

uf1 = UnionFind(N)
for _ in range(K):
    p, q = [int(i) - 1 for i in input().split()]
    uf1.union(p, q)

uf2 = UnionFind(N)
for _ in range(L):
    r, s = [int(i) - 1 for i in input().split()]
    uf2.union(r, s)

from collections import Counter
C = Counter([(a, b) for a, b in zip(uf1.data, uf2.data)])
print(*[C[(a, b)] for a, b in zip(uf1.data, uf2.data)])

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, size):
        self.data = [-1 for _ in range(size)]
    def find(self, x):
        if self.data[x] < 0:
            return x
        else:
            self.data[x] = self.find(self.data[x])
            return self.data[x]
    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x != y:
            if self.data[y] < self.data[x]:
                x, y = y, x
            self.data[x] += self.data[y]
            self.data[y] = x
        return (x != y)
    def same(self, x, y):
        return (self.find(x) == self.find(y))
    def size(self, x):
        return -self.data[self.find(x)]


N, K, L = [int(i) for i in input().split()]

uf1 = UnionFind(N)
for _ in range(K):
    p, q = [int(i) - 1 for i in input().split()]
    uf1.union(p, q)

uf2 = UnionFind(N)
for _ in range(L):
    r, s = [int(i) - 1 for i in input().split()]
    uf2.union(r, s)

from collections import Counter
A = [a if a >= 0 else i for i, a in enumerate(uf1.data)]
B = [b if b >= 0 else i for i, b in enumerate(uf2.data)]
C = Counter([(a, b) for a, b in zip(A, B)])
print(*[C[(a, b)] for a, b in zip(A, B)])

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, size):
        self.data = [-1 for _ in range(size)]
    def find(self, x):
        if self.data[x] < 0:
            return x
        else:
            self.data[x] = self.find(self.data[x])
            return self.data[x]
    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x != y:
            if self.data[y] < self.data[x]:
                x, y = y, x
            self.data[x] += self.data[y]
            self.data[y] = x
        return (x != y)
    def same(self, x, y):
        return (self.find(x) == self.find(y))
    def size(self, x):
        return -self.data[self.find(x)]


N, K, L = [int(i) for i in input().split()]

uf1 = UnionFind(N)
for _ in range(K):
    p, q = [int(i) - 1 for i in input().split()]
    uf1.union(p, q)

uf2 = UnionFind(N)
for _ in range(L):
    r, s = [int(i) - 1 for i in input().split()]
    uf2.union(r, s)

from collections import Counter
A = [a if a >= 0 else i for i, a in enumerate(uf1.data)]
B = [b if b >= 0 else i for i, b in enumerate(uf2.data)]
C = Counter([(a, b) for a, b in zip(A, B)])
for a, b in zip(A, B):
    print(C[(a, b)])

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, size):
        self.data = [-1 for _ in range(size)]
    def find(self, x):
        if self.data[x] < 0:
            return x
        else:
            self.data[x] = self.find(self.data[x])
            return self.data[x]
    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x != y:
            if self.data[y] < self.data[x]:
                x, y = y, x
            self.data[x] += self.data[y]
            self.data[y] = x
        return (x != y)
    def same(self, x, y):
        return (self.find(x) == self.find(y))
    def size(self, x):
        return -self.data[self.find(x)]


N, K, L = [int(i) for i in input().split()]
uf = UnionFind(N)
for _ in range(K):
    p, q = [int(i) - 1 for i in input().split()]
    uf.union(p, q)
count = [1] * N
for _ in range(L):
    r, s = [int(i) - 1 for i in input().split()]
    if uf.same(r, s):
        count[r] += 1
        count[s] += 1
print(*count)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, elems):
        self.parent = dict()
        self.num_unite = dict()
        for e in elems:
            self.parent[e] = e
            self.num_unite[e] = 1

    def root(self, e):
        if self.parent[e] == e:
            return e

        self.parent[e] = self.root(self.parent[e])
        return self.parent[e]

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

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

        rx = self.root(x)
        ry = self.root(y)
        self.parent[rx] = ry
        self.num_unite[ry] += self.num_unite[rx]

    def count(self, x):
        return self.num_unite[self.root(x)]


n, k, l = map(int, input().split())
uf1 = UnionFind(list(range(n)))
uf = UnionFind(list(range(n)))

for i in range(k):
    a, b = map(lambda x: int(x) - 1, input().split())
    uf1.unite(a, b)

for i in range(l):
    a, b = map(lambda x: int(x) - 1, input().split())
    if uf1.same(a, b):
        uf.unite(a, b)


nums = [uf.count(i) for i in range(n)]
print(' '.join(map(str, nums)))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, K, L = [int(a) for a in input().split(" ")]
P, Q = zip(*[[int(v) for v in input().split(" ")] for i in range(K)])
R, S = zip(*[[int(v) for v in input().split(" ")] for i in range(L)])

def find(tab, a):
    if tab[a] == -1: return a
    tab[a] = find(tab, tab[a])
    return tab[a]

def union(tab, a, b):
    pa, pb = (find(tab, a), find(tab, b))
    if pa!=pb:
        tab[pb] = pa

loads = [-1]*N
rails = [-1]*N
both = [-1]*N
for p, q in zip(P, Q):
    union(loads, p-1, q-1)

for r, s in zip(R, S):
    union(rails, r-1, s-1)

for p, q in zip(P, Q):
    if find(rails, p-1)==find(rails, q-1):
        union(both, p-1, q-1)

for r, s in zip(R, S):
    if find(loads, r-1)==find(loads, s-1):
        union(both, r-1, s-1)

dic = defaultdict(lambda: 0)
for i in range(N):
    dic[find(both, i)]+=1
ans = [dic[find(both, i)] for i in range(N)]
print(" ".join([str(n) for n in ans]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, K, L = [int(a) for a in input().split(" ")]
P, Q = zip(*[[int(v) for v in input().split(" ")] for i in range(K)])
R, S = zip(*[[int(v) for v in input().split(" ")] for i in range(L)])

def find(tab, a):
    if tab[a] == -1: return a
    tab[a] = find(tab, tab[a])
    return tab[a]

def union(tab, a, b):
    pa, pb = (find(tab, a), find(tab, b))
    if pa!=pb:
        tab[pb] = pa

loads = [-1]*N
both = [-1]*N
for p, q in zip(P, Q):
    union(loads, p-1, q-1)

for r, s in zip(R, S):
    if find(loads, r-1) == find(loads, s-1):
        union(both, r-1, s-1)

dic = defaultdict(lambda: 0)
for i in range(N):
    dic[find(both, i)] += 1

print(" ".join([str(dic[find(both, i)]) for i in range(N)]))

Submission status: Wrong Answer

Solution code:
n, k, l = [int(item) for item in input().split()]
pq = [[int(item) for item in input().split()] for _ in range(k)]
rs = [[int(item) for item in input().split()] for _ in range(l)]

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 same_check(self, x, y):
        return self.find(x) == self.find(y)

    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

UFA = UnionFind(n)
UFB = UnionFind(n)
for p, q in pq:
    UFA.union(p-1, q-1)
for r, s in rs:
    UFB.union(r-1, s-1)

par = list(zip(UFA.par, UFB.par, [i for i in range(n)]))
par = sorted(par, key=lambda x:(x[0], x[1]))
prev_a, prev_b = -1, -1
ans = [0] * n
count = 0
member = []
for a, b, index in par:
    if prev_a != a or prev_b != b:
        for mem in member:
            ans[mem] = count
        count = 1
        member = [index]
        prev_a = a
        prev_b = b
    else:
        member.append(index)
        count += 1
for mem in member:
    ans[mem] = count
print(" ".join([str(item) for item in ans]))

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

class UnionFind:
    def __init__(self, n):
        self.root = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1] * (n+1)  # The number of nodes under the target node.
        self.weight = [0] * (n+1)  # Distance from the root

    def find(self, x):
        if self.root[x] == x:
            return x
        else:
            y = self.find(self.root[x])
            self.weight[x] += self.weight[self.root[x]]
            self.root[x] = y
            return self.root[x]

    def unite(self, x, y, w=0):
        rx = self.find(x)
        ry = self.find(y)
        if self.rank[rx] < self.rank[ry]:
            self.root[rx] = ry
            self.size[ry] += self.size[rx]
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        else:
            self.root[ry] = rx
            self.size[rx] += self.size[ry]
            self.weight[ry] = -w - self.weight[y] + self.weight[x]
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1

    def is_same(self, x, y):
        return self.find(x) == self.find(y)
    
    def diff(self, x, y):
        return self.weight[y] - self.weight[x]

def main():
    input = sys.stdin.readline
    N, K, L = map(int, input().split())
    road = UnionFind(N)
    rail = UnionFind(N)

    for _ in range(K):
        p, q = map(int, input().split())
        road.unite(p-1, q-1)

    for _ in range(L):
        r, s = map(int, input().split())
        rail.unite(r-1, s-1)

    road_r = defaultdict(set)
    rail_r = defaultdict(set)
    for i in range(N):
        road_r[road.root[i]].add(i)
        rail_r[rail.root[i]].add(i)

    ans = [0 for _ in range(N)]
    for i in range(N):
        ans[i] = len(road_r[road.root[i]] & rail_r[rail.root[i]])

    return print(*ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    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 same_check(self, x, y):
        return self.find(x) == self.find(y)

def solve():
 n,k,l = (int(i) for i in input().split())
 ufr = UnionFind(n-1)
 for i in range(k):
   tmp1,tmp2 = (int(i) for i in input().split())
   ufr.union(tmp1-1,tmp2-1)
 ansr = [0]*n
 tp = ufr.par
 for i in range(n):
    if tp.count(ufr.par[i]) >= 2:
      ansr[i] = 1

 uft = UnionFind(n-1)
 for i in range(l):
   tmp1,tmp2 = (int(i) for i in input().split())
   uft.union(tmp1-1,tmp2-1)
 anst = [0]*n
 tp = uft.par
 for i in range(n):
    if tp.count(uft.par[i]) >= 2:
      anst[i] = 1
 finalans = []
 for i in range(n):
  finalans.append(ansr[i]+anst[i])
 print(*finalans)


solve()

Submission status: Wrong Answer

Solution code:
class UnionFindNode(object):
    """
    Union-Find構造
    ノードのグループ併合や、所属グループ判定を高速に処理する
    """

    def __init__(self, group_id, parent=None, value=None):
        self.group_id_ = group_id
        self.parent_ = parent
        self.value = value
        self.size_ = 1

    def __str__(self):
        template = "UnionFindNode(group_id: {}, \n\tparent: {}, value: {}, size: {})"
        return template.format(self.group_id_, self.parent_, self.value, self.size_)

    def is_root(self):
        return not self.parent_

    def root(self):
        parent = self
        while not parent.is_root():
            parent = parent.parent_
            self.parent_ = parent
        return parent

    def find(self):
        parent = self.root()
        return parent.group_id_

    def size(self):
        parent = self.root()
        return parent.size_

    def unite(self, unite_node):
        parent = self.root()
        unite_parent = unite_node.root()

        if parent.group_id_ != unite_parent.group_id_:
            if parent.size() > unite_parent.size():
                unite_parent.parent_ = parent
                parent.size_ = parent.size_ + unite_parent.size_
            else:
                parent.parent_ = unite_parent
                unite_parent.size_ = parent.size_ + unite_parent.size_


N,K,L=map(int,input().split())
inf1=[]
for _ in range(K):
    inf1.append(list(map(int,input().split())))
inf2=[]
for _ in range(L):
    inf2.append(list(map(int,input().split())))
U=[UnionFindNode(i) for i in range(N+1)]
for i in range(K):
    x=inf1[i][0]
    y=inf1[i][1]
    U[x].unite(U[y])
U_=[UnionFindNode(i) for i in range(N+1)]
for i in range(L):
    x=inf2[i][0]
    y=inf2[i][1]
    if U[x].find()==U[y].find():
        U_[x].unite(U_[y])
for i in range(1,N+1):
    print(U_[i].size(),end=" ")

Submission status: Wrong Answer

Solution code:
class UnionFindNode(object):
    def __init__(self, group_id, parent=None, value=None):
        self.group_id_ = group_id
        self.parent_ = parent
        self.value = value
        self.size_ = 1

    def __str__(self):
        template = "UnionFindNode(group_id: {}, \n\tparent: {}, value: {}, size: {})"
        return template.format(self.group_id_, self.parent_, self.value, self.size_)

    def is_root(self):
        return not self.parent_

    def root(self):
        parent = self
        while not parent.is_root():
            parent = parent.parent_
            self.parent_ = parent
        return parent

    def find(self):
        parent = self.root()
        return parent.group_id_

    def size(self):
        parent = self.root()
        return parent.size_

    def unite(self, unite_node):
        parent = self.root()
        unite_parent = unite_node.root()

        if parent.group_id_ != unite_parent.group_id_:
            if parent.size() > unite_parent.size():
                unite_parent.parent_ = parent
                parent.size_ = parent.size_ + unite_parent.size_
            else:
                parent.parent_ = unite_parent
                unite_parent.size_ = parent.size_ + unite_parent.size_


N,K,L=map(int,input().split())
inf1=[]
for _ in range(K):
    inf1.append(list(map(int,input().split())))
inf2=[]
for _ in range(L):
    inf2.append(list(map(int,input().split())))
U=[UnionFindNode(i) for i in range(N+1)]
for i in range(K):
    x=inf1[i][0]
    y=inf1[i][1]
    U[x].unite(U[y])
U_=[UnionFindNode(i) for i in range(N+1)]
for i in range(L):
    x=inf2[i][0]
    y=inf2[i][1]
    if U[x].find()==U[y].find():
        U_[x].unite(U_[y])
for i in range(1,N+1):
    print(U_[i].size(),end=" ")

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size=[1]*(n+1)
    # 検索
    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 Size(self,x):
        return self.size[self.find(x)]
    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        ysize=self.Size(y)
        xsize=self.Size(x)
        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
        self.size[self.find(x)]+=ysize

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

N,K,L=map(int,input().split())
inf1=[]
for _ in range(K):
    inf1.append(list(map(int,input().split())))
inf2=[]
for _ in range(L):
    inf2.append(list(map(int,input().split())))
U=UnionFind(N)
ans=0
for i in range(K):
    x=inf1[i][0]
    y=inf1[i][1]
    U.union(x,y)
U_=UnionFind(N)
for i in range(L):
    x=inf2[i][0]
    y=inf2[i][1]
    if U.same_check(x,y):
        U_.union(x,y)
for i in range(1,N+1):
    print(U_.Size(i),end=" ")

Submission status: Wrong Answer

Solution code:
n, k, l = map(int, input().split())
load = [-1] * (n + 1)
train = [-1] * (n + 1)

def find_load(x, u = load):
    if u[x] < 0 :
        return x
    else:
        u[x] = find_load(u[x])
        return find_load(u[x])

def find_train(x, u = train):
    if u[x] < 0 :
        return x
    else:
        u[x] = find_train(u[x])
        return find_train(u[x])

for i in range(k):

    p, q = map(int, input().split())
    p, q = map(find_load, [p, q])
    p, q = sorted((p, q))
    if p != q:
        load[q] = p

load = [i if x < 0 else x for i, x in enumerate(load)]

for i in range(l):
    r, s = map(int, input().split())
    r, s = map(find_train, [r, s])
    r, s = sorted((r, s))
    if r != s:
        train[s] = r
train = [i if x < 0 else x for i, x in enumerate(train)]

keys = []
dic  = {}

for node in range(n):
    key = (load[node + 1], train[node + 1])
    keys.append(key)
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1

ans = [str(dic[key]) for key in keys]
print(" ".join(ans))

Submission status: Wrong Answer

Solution code:
n, k, l = map(int, input().split())
load = [-1] * (n + 1)
train = [-1] * (n + 1)

def find_load(x, u = load):
    if u[x] < 0 :
        return x
    else:
        u[x] = find_load(u[x])
        return u[x]

def find_train(x, u = train):
    if u[x] < 0 :
        return x
    else:
        u[x] = find_train(u[x])
        return u[x]

for i in range(k):

    p, q = map(int, input().split())
    p, q = map(find_load, [p, q])
    p, q = sorted((p, q))
    if p != q:
        load[q] = p

load = [i if x < 0 else x for i, x in enumerate(load)]

for i in range(l):
    r, s = map(int, input().split())
    r, s = map(find_train, [r, s])
    r, s = sorted((r, s))
    if r != s:
        train[s] = r
train = [i if x < 0 else x for i, x in enumerate(train)]

keys = []
dic  = {}

for node in range(n):
    key = (load[node + 1], train[node + 1])
    keys.append(key)
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1

ans = [str(dic[key]) for key in keys]
print(" ".join(ans))

Submission status: Wrong Answer

Solution code:
n, k, l = map(int, input().split())
load = list(range(n + 1))
train = list(range(n + 1))

def find_load(x, u = load):
    if u[x] == x :
        return x
    else:
        return find_load(u[x])

def find_train(x, u = train):
    if u[x] == x :
        return x
    else:
        return find_train(u[x])

for i in range(k):
    p, q = map(int, input().split())
    rp, rq = map(find_load, [p, q])
    if rp != rq:
        thisroot = min(rp, rq)
        load[q] = thisroot
        load[p] = thisroot
        load[rp] = thisroot
        load[rq] = thisroot

for i in range(l):
    r, s = map(int, input().split())
    rr, rs = map(find_train, [r, s])
    if r != s:
        thisroot = min(rr, rs)
        train[s] = thisroot
        train[r] = thisroot
        train[rr] = thisroot
        train[rs] = thisroot

ans = []
for x, y in zip(load, train):
    if x == -1 or y == -1:
        ans.append(1)
    else:
        ans.append(sum([(x == X and y == Y) for X, Y in zip(load,train)]))

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
n, k, l = map(int, input().split())
load = list(range(n + 1))
train = list(range(n + 1))

def find_load(x, u = load):
    if u[x] == x :
        return x
    else:
        return find_load(u[x])

def find_train(x, u = train):
    if u[x] == x :
        return x
    else:
        return find_train(u[x])

for i in range(k):
    p, q = map(int, input().split())
    rp, rq = map(find_load, [p, q])
    if rp != rq:
        thisroot = min(rp, rq)
        load[q] = thisroot
        load[p] = thisroot
        load[rp] = thisroot
        load[rq] = thisroot

for i in range(l):
    r, s = map(int, input().split())
    rr, rs = map(find_train, [r, s])
    if r != s:
        thisroot = min(rr, rs)
        train[s] = thisroot
        train[r] = thisroot
        train[rs] = thisroot
        train[rs] = thisroot

ans = []
for x, y in zip(load, train):
    if x == -1 or y == -1:
        ans.append(1)
    else:
        ans.append(sum([(x == X and y == Y) for X, Y in zip(load,train)]))

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
n, k, l = map(int, input().split())
load = [-1] * (n + 1)
train = [-1] * (n + 1)

def find_load(x, u = load):
    if u[x] < 0 :
        return x
    else:
        return find_load(u[x])

def find_train(x, u = train):
    if u[x] < 0 :
        return x
    else:
        return find_train(u[x])

for i in range(k):
    p, q = map(int, input().split())
    p, q = map(find_load, [p, q])
    p, q = sorted((p, q))
    if p != q:
        load[p] += load[q]
        load[q] = p

for i in range(l):
    r, s = map(int, input().split())
    r, s = map(find_train, [r, s])
    r, s = sorted((r, s))
    if r != s:
        train[r] += train[s]
        train[s] = r

for i, x in enumerate(load):
    if x < -1:
        load[i] = i

for i, x in enumerate(train):
    if x < -1:
        train[i] = i
ans = []
for x, y in zip(load, train):
    if x == -1 or y == -1:
        ans.append(1)
    else:
        ans.append(sum([(x == X and y == Y) for X, Y in zip(load,train)]))

print(*ans[1:])