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:
class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

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

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

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x != y:
            if self.rank[x] > self.rank[y]:
                self.parent[y] = self.parent[x]
                self.size[x] += self.size[y]
            elif self.rank[y] > self.rank[x]:
                self.parent[x] = self.parent[y]
                self.size[y] += self.size[x]
            else:
                self.parent[y] = self.parent[x]
                self.rank[x] += 1
                self.size[x] += self.size[y]

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

uf_road = UnionFind(N)
for _ in range(K):
    p, q = map(int, input().split())
    p -= 1
    q -= 1
    uf_road.union(p, q)

uf_road_railway = UnionFind(N)
for i in range(L):
    r, s = map(int, input().split())
    r -= 1
    s -= 1
    if uf_road.find(r, s):
        uf_road_railway.union(r, s)
print(" ".join((map(lambda x: str(uf_road_railway.size[x]), uf_road_railway.parent))))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す．
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]


import sys
input = sys.stdin.readline

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.merge(p,q)

UF2 = UnionFind(n)
for _ in range(l):
    p,q = [int(i)-1 for i in input().split()]
    if UF1.issame(p,q): UF2.merge(p,q)

print(*[UF2.getsize(i) for i in range(n)])

Submission status: Wrong Answer

Solution code:
N,K,L = list(map(int,input().split()))

e_list1 = [[] for i in range(N)]
e_list2 = [[] for i in range(N)]

for i in range(K):
    p,q = list(map(int,input().split()))
    e_list1[p-1].append(q-1)
    e_list1[q-1].append(p-1)

for i in range(L):
    p,q = list(map(int,input().split()))
    e_list2[p-1].append(q-1)
    e_list2[q-1].append(p-1)


from collections import deque

def BFS(vi,color,color_list,e_list):
    Q = deque([vi])
    color_list[vi]=color
    while len(Q)>0:
        v = Q.pop()
        for v1 in e_list[v]:
            if color_list[v1]==-1:
                color_list[v1]=color
                Q.appendleft(v1)
    return color_list


def BFS2(vi,color,color2,color_list,color_list2,e_list):
    Q = deque([vi])
    color_list2[vi]=color2
    while len(Q)>0:
        v = Q.pop()
        for v1 in e_list[v]:
            if color_list[v1]==color and color_list2[v1]==-1:
                color_list2[v1]=color2
                Q.appendleft(v1)
    return color_list2


color_list = [-1 for i in range(N)]
color = 0
for i in range(N):
    if color_list[i]==-1:
        color_list = BFS(i,color,color_list,e_list1)
        color+=1
color_list2 = [-1 for i in range(N)]
color2 = 0
for i in range(N):
    if color_list2[i]==-1:
        color_list2 = BFS2(i,color_list[i],color2,color_list,color_list2,e_list2)
        color2+=1

color_num_list = [0 for i in range(max(color_list2)+1)]

for i in range(N):
    color_num_list[color_list2[i]]+=1

ans = []

for i in range(N):
    ans.append(color_num_list[color_list2[i]])

print(*ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


class UnionFind(object):
    __slots__ = ["nodes"]

    def __init__(self, n: int):
        self.nodes = [-1]*n # 親: -(属するノード数), それ以外: (親ノードindex)

    def find(self, x: int) -> int:
        if self.nodes[x] < 0:
            return x
        else:
            self.nodes[x] = self.find(self.nodes[x]) # compression
            return self.nodes[x]

    def has_same_root(self, x, y) -> bool:
        return self.find(x) == self.find(y)
    
    def size(self, x) -> int:
        return -self.nodes[self.find(x)]

    def unite(self, x: int, y: int) -> None:
        if self.has_same_root(x, y):
            return

        root_x, root_y = self.find(x), self.find(y)
        if self.nodes[root_x] < self.nodes[root_y]:
            bigroot, smallroot = (root_x, root_y)
        else: 
            bigroot, smallroot = (root_y, root_x)
        self.nodes[bigroot] += self.nodes[smallroot]
        self.nodes[smallroot] = bigroot


def solve(N: int, K: int, L: int, p: "List[int]", q: "List[int]", r: "List[int]", s: "List[int]"):
    con_road = UnionFind(N)
    con_both = UnionFind(N)
    for i in range(K):
        con_road.unite(p[i], q[i])
    for i in range(L):
        if con_road.has_same_root(r[i], s[i]):
            con_both.unite(r[i], s[i])
    print(' '.join(str(con_both.size(i)) for i in range(N)))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    L = int(next(tokens))  # type: int
    p = [int()] * (K)  # type: "List[int]" 
    q = [int()] * (K)  # type: "List[int]" 
    for i in range(K):
        p[i] = int(next(tokens))-1
        q[i] = int(next(tokens))-1
    r = [int()] * (L)  # type: "List[int]" 
    s = [int()] * (L)  # type: "List[int]" 
    for i in range(L):
        r[i] = int(next(tokens))-1
        s[i] = int(next(tokens))-1
    solve(N, K, L, p, q, r, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K,L=map(int,input().split())
Road_P=[i for i in range(N+1)]
Road_size=[1 for i in range(N+1)]

def find_road(a):
    if a==Road_P[a]:
        return a
    else:
        return find_road(Road_P[a])
        
def union_road(b,c):
    B=find_road(b)
    C=find_road(c)
    if Road_size[B]>=Road_size[C]:
        Road_P[C]=Road_P[B]
        Road_size[B]+=Road_size[C]
    else:
        Road_P[B]=Road_P[C]
        Road_size[C]+=Road_size[B]

for i in range(K):
    p,q=map(int,input().split())
    if find_road(p)!=find_road(q):
        union_road(p,q)
#print(Road_P)

Rail_P=[i for i in range(N+1)]
Rail_size=[1 for i in range(N+1)]

def find_rail(a):
    if a==Rail_P[a]:
        return a
    else:
        return find_rail(Rail_P[a])
        
def union_rail(b,c):
    B=find_rail(b)
    C=find_rail(c)
    if Rail_size[B]>=Rail_size[C]:
        Rail_P[C]=Rail_P[B]
        Rail_size[B]+=Rail_size[C]
    else:
        Rail_P[B]=Rail_P[C]
        Rail_size[C]+=Rail_size[B]

for i in range(L):
    p,q=map(int,input().split())
    if find_rail(p)!=find_rail(q):
        union_rail(p,q)
#print(Rail_P)

comb={}
for i in range(1,N+1):
    if (Road_P[i],Rail_P[i]) in comb:
        comb[(Road_P[i],Rail_P[i])]+=1
    else:
        comb[(Road_P[i],Rail_P[i])]=1

#print(comb)

for i in range(1,N+1):
    print(comb[(Road_P[i],Rail_P[i])])

Submission status: Wrong Answer

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


class UNION_FIND(object):
    def __init__(self,n):
        #親の番号を格納する。親だった場合は-(その集合のサイズ)
        #作るときはParentの値を全て-1にする
        #こうすると全てバラバラになる
        self.parent=[-1 for i in range(n)]
        
    def root(self,x):
        #Aがどのグループに属しているか調べる
        if self.parent[x]<0:
            return x
        else:
            self.parent[x]=self.root(self.parent[x])  #親を再帰で探知 再帰は最後に上のifにひっかかる
            return self.parent[x]
 
    def size(self,x):
        #自分のいるグループの頂点数を調べる
        return -self.parent[self.root(x)]   #親をとってきたい
        
    def union(self,x,y):
        #AとBをくっ付ける
        #AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
        #print(self.parent)
        x=self.root(x)
        y=self.root(y)
        #print(x,y)
        if x==y:   #すでにくっついてるからくっ付けない
            return False
            
        #大きい方(A)に小さいほう(B)をくっ付けたい
        #大小が逆だったらひっくり返しちゃう。
        if self.size(x)<self.size(y):
            x,y=y,x
        self.parent[x]+=self.parent[y]  #Aのサイズを更新する
        self.parent[y]=x   #Bの親をAに変更する
        return True
        
n,k,l=map(int,input().split())
u1=UNION_FIND(n)
u2=UNION_FIND(n)
d1=defaultdict(set)
d2=defaultdict(set)
for i in range(k):
    p,q=map(int,input().split())
    u1.union(p-1,q-1)
for i in range(l):
    p,q=map(int,input().split())
    u2.union(p-1,q-1)

for k,v in [[k if v<0 else v,k] for k,v in enumerate(u1.parent)]:
    d1[k].add(v)
for k,v in [[k if v<0 else v,k] for k,v in enumerate(u2.parent)]:
    d2[k].add(v)
table=[0]*n
#print(d1.values(),d2.values())
for i in d1.values():
    for j in d2.values():
        if len(i&j)==1:
            table[list(i&j)[0]]=1
        else:
            for h in i&j:
                table[h]=2
print(*table)

Submission status: Wrong Answer

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


class UNION_FIND(object):
    def __init__(self,n):
        #親の番号を格納する。親だった場合は-(その集合のサイズ)
        #作るときはParentの値を全て-1にする
        #こうすると全てバラバラになる
        self.parent=[-1 for i in range(n)]
        
    def root(self,x):
        #Aがどのグループに属しているか調べる
        if self.parent[x]<0:
            return x
        else:
            self.parent[x]=self.root(self.parent[x])  #親を再帰で探知 再帰は最後に上のifにひっかかる
            return self.parent[x]
 
    def size(self,x):
        #自分のいるグループの頂点数を調べる
        return -self.parent[self.root(x)]   #親をとってきたい
        
    def union(self,x,y):
        #AとBをくっ付ける
        #AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
        #print(self.parent)
        x=self.root(x)
        y=self.root(y)
        #print(x,y)
        if x==y:   #すでにくっついてるからくっ付けない
            return False
            
        #大きい方(A)に小さいほう(B)をくっ付けたい
        #大小が逆だったらひっくり返しちゃう。
        if self.size(x)<self.size(y):
            x,y=y,x
        self.parent[x]+=self.parent[y]  #Aのサイズを更新する
        self.parent[y]=x   #Bの親をAに変更する
        return True
        
n,k,l=map(int,input().split())
u1=UNION_FIND(n)
u2=UNION_FIND(n)
d= defaultdict(int)
for i in range(k):
    p,q=map(int,input().split())
    u1.union(p-1,q-1)
for i in range(l):
    p,q=map(int,input().split())
    u2.union(p-1,q-1)
for i in range(n):
    p=u1.root(i)
    q=u2.root(i)
    for j in range(n):
        if i!=j:
            if (p==u1.root(j)):
                d[i]+=1
                break
    for j in range(n):
        if i!=j:
            if (q==u2.root(j)):
                d[i]+=1
                break
print(*d.values())

Submission status: Wrong Answer

Solution code:
from collections import*
class UNION_FIND(object):
    def __init__(self,n):
        #親の番号を格納する。親だった場合は-(その集合のサイズ)
        #作るときはParentの値を全て-1にする
        #こうすると全てバラバラになる
        self.parent=[-1 for i in range(n)]
        
    def root(self,x):
        #Aがどのグループに属しているか調べる
        if self.parent[x]<0:
            return x
        else:
            self.parent[x]=self.root(self.parent[x])  #親を再帰で探知 再帰は最後に上のifにひっかかる
            return self.parent[x]
 
    def size(self,x):
        #自分のいるグループの頂点数を調べる
        return -self.parent[self.root(x)]   #親をとってきたい
        
    def union(self,x,y):
        #AとBをくっ付ける
        #AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
        #print(self.parent)
        x=self.root(x)
        y=self.root(y)
        #print(x,y)
        if x==y:   #すでにくっついてるからくっ付けない
            return False
            
        #大きい方(A)に小さいほう(B)をくっ付けたい
        #大小が逆だったらひっくり返しちゃう。
        if self.size(x)<self.size(y):
            x,y=y,x
        self.parent[x]+=self.parent[y]  #Aのサイズを更新する
        self.parent[y]=x   #Bの親をAに変更する
        return True
        
n,k,l=map(int,input().split())
u1=UNION_FIND(n)
u2=UNION_FIND(n)
d= defaultdict(int)
for i in range(k):
    p,q=map(int,input().split())
    u1.union(p-1,q-1)
for i in range(l):
    p,q=map(int,input().split())
    u2.union(p-1,q-1)

Submission status: Wrong Answer

Solution code:
from collections import*
class UNION_FIND(object):
    def __init__(self,n):
        #親の番号を格納する。親だった場合は-(その集合のサイズ)
        #作るときはParentの値を全て-1にする
        #こうすると全てバラバラになる
        self.parent=[-1 for i in range(n)]
        
    def root(self,x):
        #Aがどのグループに属しているか調べる
        if self.parent[x]<0:
            return x
        else:
            self.parent[x]=self.root(self.parent[x])  #親を再帰で探知 再帰は最後に上のifにひっかかる
            return self.parent[x]
 
    def size(self,x):
        #自分のいるグループの頂点数を調べる
        return -self.parent[self.root(x)]   #親をとってきたい
        
    def union(self,x,y):
        #AとBをくっ付ける
        #AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
        #print(self.parent)
        x=self.root(x)
        y=self.root(y)
        #print(x,y)
        if x==y:   #すでにくっついてるからくっ付けない
            return False
            
        #大きい方(A)に小さいほう(B)をくっ付けたい
        #大小が逆だったらひっくり返しちゃう。
        if self.size(x)<self.size(y):
            x,y=y,x
        self.parent[x]+=self.parent[y]  #Aのサイズを更新する
        self.parent[y]=x   #Bの親をAに変更する
        return True
        
n,k,l=map(int,input().split())
u1=UNION_FIND(n)
u2=UNION_FIND(n)
d= defaultdict(int)
for i in range(k):
    p,q=map(int,input().split())
    u1.union(p-1,q-1)
for i in range(l):
    p,q=map(int,input().split())
    u2.union(p-1,q-1)
for i in range(n):
    p=u1.root(i)
    q=u2.root(i)
    for j in range(n):
        if i!=j:
            if (p==u1.root(j)):
                d[i]+=1
                break
    for j in range(n):
        if i!=j:
            if (q==u2.root(j)):
                d[i]+=1
                break
print(*d.values())

Submission status: Wrong Answer

Solution code:
import copy

def init(n):
    global par, rank
    par = [i for i in range(n)]
    rank = [0] * n

def find(x):
    global par
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]

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

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

N, K, L = map(int, input().split())
init(N)
for i in range(K):
    p, q = map(int, input().split())
    unite(p - 1, q - 1)
par2 = copy.copy(par)
init(N)
for i in range(L):
    p, q = map(int, input().split())
    unite(p - 1, q - 1)
d = {}
for i in range(N):
    d[par2[i], par[i]] = d.get((par2[i], par[i]), 0) + 1
for i in range(N):
    print(d[par2[i], par[i]], end = " ")
print()

Submission status: Wrong Answer

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

ar = [i for i in range(n)]
ah = [0 for _ in range(n)]
br = [i for i in range(n)]
bh = [0 for _ in range(n)]

def root(l, i):
  if l[i] == i:
    return i
  else:
    l[i] = root(l, l[i])
    return l[i]

def union(l, h, i, j):
  ip = root(l, i)
  jp = root(l, j)
  if ip == jp:
    return
  else:
    if h[ip] < h[jp]:
      l[ip] = jp
    elif h[ip] > h[jp]:
      l[jp] = ip
    else:
      if ip < jp:
        l[jp] = ip
        h[ip] += 1
      else:
        l[ip] = jp
        h[jp] += 1

for _ in range(k):
  p, q = map(int, input().split())
  if p < q:
    union(ar, ah, p-1, q-1)
  else:
    union(ar, ah, q-1, p-1)

for _ in range(l):
  r, s = map(int, input().split())
  if r < s:
    union(br, bh, r-1, s-1)
  else:
    union(br, bh, s-1, r-1)

print(ar, br)

abd = {}
for a, b in zip(ar, br):
  if (root(ar, a), root(br, b)) in abd:
    abd[(root(ar, a), root(br, b))] += 1
  else:
    abd[(root(ar, a), root(br, b))] = 1

for a, b in zip(ar, br):
  print(abd[(root(ar, a), root(br, b))])

Submission status: Wrong Answer

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

ar = [i for i in range(n)]
ah = [0 for _ in range(n)]
br = [i for i in range(n)]
bh = [0 for _ in range(n)]

def root(l, i):
  if l[i] == i:
    return i
  else:
    l[i] = root(l, l[i])
    return l[i]

def union(l, h, i, j):
  ip = root(l, i)
  jp = root(l, j)
  if ip == jp:
    return
  else:
    if h[ip] < h[jp]:
      l[ip] = jp
    elif h[ip] > h[jp]:
      l[jp] = ip
    else:
      l[jp] = ip
      h[ip] += 1

for _ in range(k):
  p, q = map(int, input().split())
  if p < q:
    union(ar, ah, p-1, q-1)
  else:
    union(ar, ah, q-1, p-1)

for _ in range(l):
  r, s = map(int, input().split())
  if r < s:
    union(br, bh, r-1, s-1)
  else:
    union(br, bh, s-1, r-1)

abd = {}
for a, b in zip(ar, br):
  if (a, b) in abd:
    abd[(a, b)] += 1
  else:
    abd[(a, b)] = 1

for a, b in zip(ar, br):
  print(abd[(a, b)])

Submission status: Wrong Answer

Solution code:
N,K,L = map(int, input().split())


class UFT:
    def __init__(self, N):
        self.UFdata = [-1] * (N+1)
        self.rank   = [0]  * (N+1)
        self.connected_num = [1] * (N+1)

    def find(self,x):
        UFdata = self.UFdata
        while UFdata[x] != -1:
            x = UFdata[x]
        return x

    def unite(self,x,y):
        UFdata = self.UFdata
        rank = self.rank
        connected_num = self.connected_num
        find = self.find

        x = find(x)
        y = find(y)
        if x != y:
            if rank[x] < rank[y]:
                x,y = y,x
            if rank[x] == rank[y]:
                rank[x] += 1
            UFdata[y] = x       
            connected_num[x] += connected_num[y]
            

UF1 = UFT(N)
UF2 = UFT(N)
import sys
input = sys.stdin.readline
PQ = [ [int(j) for j in input().split()] for _ in range(K)]
RS = [ [int(j) for j in input().split()] for _ in range(L)]

for i in range(K):
    p,q = PQ[i]
    # if UF1.find(p) != UF1.find(q) :
    UF1.unite(p,q)

for i in range(L):
    p,q = RS[i]

    if  UF1.find(p) == UF1.find(q) :
        UF2.unite(p, q)

ans = [0]*(N+1)
for i in range(1,N+1):
    ans[i] = UF2.connected_num[UF2.find(i)]

del ans[0]
print(" ".join( map(lambda x: str(x), ans)))

Submission status: Wrong Answer

Solution code:
N,K,L = map(int, input().split())


class UFT:
    def __init__(self, N):
        self.UFdata = [-1] * (N+1)
        self.rank   = [0]  * (N+1)
        self.connected_num = [1] * (N+1)

    def find(self,x):
        UFdata = self.UFdata
        while UFdata[x] != -1:
            x = UFdata[x]
        return x

    def unite(self,x,y):
        UFdata = self.UFdata
        rank = self.rank
        connected_num = self.connected_num
        find = self.find

        x = find(x)
        y = find(y)
        if x != y:
            if rank[x] < rank[y]:
                # UFdata[x] = UFdata[y]
                x,y = y,x
            if rank[x] == rank[y]:
                rank[x] += 1
            UFdata[y] = x       
            connected_num[x] += connected_num[y]
            

UF1 = UFT(N)
UF2 = UFT(N)
ans = [1] * (N+1)
# MAP= [ [0] * (N+1) for _ in range(N+1)]
import sys
input = sys.stdin.readline
PQ = [ [int(j) for j in input().split()] for _ in range(K)]
RS = [ [int(j) for j in input().split()] for _ in range(L)]

for i in range(K):
    p,q = PQ[i]
    if UF1.find(p) != UF1.find(q) :
        UF1.unite(p,q)

    

for i in range(L):
    p,q = RS[i]

    if  UF1.find(p) == UF1.find(q) :
        UF2.unite(p, q)

ans = [0]*(N+1)
for i in range(1,N+1):
    # ans.append (  UF2.connected_num[ UF2.find( i) ] )
    ans[i] = UF2.connected_num[UF2.find(i)]

del ans[0]
# print(" ".join( map(lambda x: str(x), connected_num[1:])))
print(" ".join( map(lambda x: str(x), ans)))

Submission status: Wrong Answer

Solution code:
N,K,L = map(int, input().split())


class UFT:
    def __init__(self, N):
        self.UFdata = [-1] * (N+1)
        self.rank   = [0]  * (N+1)
        self.connected_num = [1] * (N+1)

    def find(self,x):
        UFdata = self.UFdata
        while UFdata[x] != -1:
            x = UFdata[x]
        return x

    def unite(self,x,y):
        UFdata = self.UFdata
        rank = self.rank
        connected_num = self.connected_num
        find = self.find

        x = find(x)
        y = find(y)
        if x != y:
            if rank[x] < rank[y]:
                UFdata[x] = UFdata[y]
                x,y = y,x
            if rank[x] == rank[y]:
                rank[x] += 1
            UFdata[y] = x       
            connected_num[x] += connected_num[y]
            

UF1 = UFT(N)
UF2 = UFT(N)
ans = [1] * (N+1)
# MAP= [ [0] * (N+1) for _ in range(N+1)]
import sys
input = sys.stdin.readline
PQ = [ [int(j) for j in input().split()] for _ in range(K)]
RS = [ [int(j) for j in input().split()] for _ in range(L)]

for i in range(K):
    p,q = PQ[i]
    if UF1.find(p) != -1 and UF1.find(p) != UF1.find(q)  or UF1.find(p) == UF1.find(q) == -1:
        UF1.unite(p,q)

    

for i in range(L):
    p,q = RS[i]

    if UF1.find(p) != -1 and UF1.find(p) == UF1.find(q) :
        UF2.unite(p, q)

ans = [0]*(N+1)
for i in range(1,N+1):
    # ans.append (  UF2.connected_num[ UF2.find( i) ] )
    ans[i] = UF2.connected_num[UF2.find(i)]

del ans[0]
# print(" ".join( map(lambda x: str(x), connected_num[1:])))
print(" ".join( map(lambda x: str(x), ans)))

Submission status: Wrong Answer

Solution code:
N,K,L = map(int, input().split())


class UFT:
    def __init__(self, N):
        self.UFdata = [-1] * (N+1)
        self.rank   = [0]  * (N+1)
        self.connected_num = [1] * (N+1)

    def find(self,x):
        UFdata = self.UFdata
        while UFdata[x] != -1:
            x = UFdata[x]
        return x

    def unite(self,x,y):
        UFdata = self.UFdata
        rank = self.rank
        connected_num = self.connected_num
        find = self.find

        x = find(x)
        y = find(y)
        if x != y:
            if rank[x] < rank[y]:
                UFdata[x] = UFdata[y]
                x,y = y,x
            if rank[x] == rank[y]:
                rank[x] += 1
            UFdata[y] = x       
            connected_num[x] += connected_num[y]
            

UF1 = UFT(N)
UF2 = UFT(N)
ans = [1] * (N+1)
# MAP= [ [0] * (N+1) for _ in range(N+1)]
import sys
input = sys.stdin.readline
PQ = [ [int(j) for j in input().split()] for _ in range(K)]
RS = [ [int(j) for j in input().split()] for _ in range(L)]

for i in range(K):
    p,q = PQ[i]
    if UF1.find(p) != -1 and UF1.find(p) != UF1.find(q)  or UF1.find(p) == UF1.find(q) == -1:
        UF1.unite(p,q)

    

for i in range(L):
    p,q = RS[i]

    if UF1.find(p) != -1 and UF1.find(p) == UF1.find(q) :
        UF2.unite(p, q)

ans = [0]*(N+1)
par_idx = [0]*(N+1)
for i in range(1,N+1):
    par_idx[i] = UF2.find(i)
for i in range(1,N+1):
    ans[i] = UF2.connected_num[par_idx[i]]

del ans[0]
# print(" ".join( map(lambda x: str(x), connected_num[1:])))
print(" ".join( map(lambda x: str(x), ans)))

Submission status: Wrong Answer

Solution code:
# D
class UnionFind:
    """size(int)を指定
    root(x:int)->int:      根を求める
    unite(x,y:int):        要素をグループにまとめる 
    isSame(x,y:int)->bool: 要素同士が同じグループか判定する
    """
    # 分離はできない
    # 始め各要素は別々のグループ  
    def __init__(self, size: int):
        self.par = [-1]*size # 親の番号
        for i in range(size):
            self.par[i] = i  # はじめ すべての頂点が根
    
    def root(self, x: int) -> int:
        """根を求める"""
        if self.par[x] == x: # if root
            return x
        else:
            self.par[x] = self.root(self.par[x]) # 経路圧縮
            return self.par[x]
    
    def isSame(self, x:int, y:int)->bool:
        """x と y が同じ集合に属するか否か"""
        return self.root(x)==self.root(y)
    
    def unite(self, x:int, y:int):
        """x と y の属する集合を併合"""
        x = self.root(x)
        y = self.root(y)
        if x == y: return
        self.par[x] = y
        
# ------------------
N,K,L = map(int, input().split())
pq, rs = set(), set()
for _ in range(K):
    p,q = map(int, input().split())
    pq.add((p-1, q-1))
for _ in range(L):
    r,s= map(int, input().split())
    rs.add((r-1, s-1))

edge = pq&rs
uf = UnionFind(N)
for s,t in edge:
    uf.unite(s,t)
cnt = [0]*N
for i in range(N):
    r = uf.root(i)
    cnt[r] += 1
ans = []
for i in range(N):
    r = uf.root(i)
    ans.append(cnt[r])
print(*ans, sep=" ", end="\n")

Submission status: Wrong Answer

Solution code:
# D
class UnionFind:
    """size(int)を指定
    root(x:int)->int:      根を求める
    unite(x,y:int):        要素をグループにまとめる 
    isSame(x,y:int)->bool: 要素同士が同じグループか判定する
    """
    # 分離はできない
    # 始め各要素は別々のグループ  
    def __init__(self, size: int):
        self.par = [-1]*size # 親の番号
        for i in range(size):
            self.par[i] = i  # はじめ すべての頂点が根
    
    def root(self, x: int) -> int:
        """根を求める"""
        if self.par[x] == x: # if root
            return x
        else:
            self.par[x] = self.root(self.par[x]) # 経路圧縮
            return self.par[x]
    
    def isSame(self, x:int, y:int)->bool:
        """x と y が同じ集合に属するか否か"""
        return self.root(x)==self.root(y)
    
    def unite(self, x:int, y:int):
        """x と y の属する集合を併合"""
        x = self.root(x)
        y = self.root(y)
        if x == y: return
        self.par[x] = y
        
# ------------------
N,K,L = map(int, input().split())
pq, rs = set(), set()
for _ in range(K):
    p,q = map(int, input().split())
    pq.add((p-1, q-1))
for _ in range(L):
    r,s= map(int, input().split())
    rs.add((r-1, s-1))

edge = pq&rs
uf = UnionFind(N)
for s,t in edge:
    uf.unite(s,t)
cnt = [0]*N
for i in range(N):
    r = uf.root(i)
    cnt[r] += 1
ans = []
for i in range(N):
    r = uf.root(i)
    ans.append(cnt[r])
print(ans, sep=" ")

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class UnionFind:
    def __init__(self, size):
        self.parent = [i for i in range(size)]
        self.rank = [0 for _ in range(size)]

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

    def union(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
        else:
            self.parent[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)

    def __str__(self):
        ret = "parents\n"
        ret += " ".join(map(str, self.parent))
        ret += '\n'
        ret += " ".join(map(str, self.rank))
        return ret


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


def solve1():
    root = UnionFind(N)
    train = UnionFind(N)

    for _ in range(K):
        a, b = map(lambda x: int(x)-1, input().split())
        root.union(a, b)

    for _ in range(L):
        a, b = map(lambda x: int(x)-1, input().split())
        train.union(a, b)
    ans = [1]*N
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            else:
                if root.same(i, j) and train.same(i, j):
                    ans[i] += 1
    print(" ".join(map(str, ans)))


def solve2():
    ans = [0]*N
    root = UnionFind(N)
    train = UnionFind(N)
    for _ in range(K):
        a, b = map(lambda x: int(x)-1, input().split())
        root.union(a, b)

    for _ in range(L):
        a, b = map(lambda x: int(x)-1, input().split())
        if root.same(a, b):
            train.union(a, b)

    parent = [0]*N
    for i in range(N):
        p = train.find(i)
        parent[p] += 1
    for i in range(N):
        ans[i] = parent[train.find(i)]
    print(" ".join(map(str, ans)))


# solve1()
solve2()

Submission status: Wrong Answer

Solution code:
import array
import collections


class UnionFind(object):
    def __init__(self, number_of_nodes):
        # Don't use self.par directly! (call self.root)
        self.par = array.array("L", range(number_of_nodes))
        self.rank = array.array("L", (0 for _ in range(number_of_nodes)))

    def root(self, node):
        if self.par[node] == node:
            return node
        else:
            r = self.root(self.par[node])
            self.par[node] = r
            return r

    def in_the_same_set(self, node1, node2):
        return self.root(node1) == self.root(node2)

    def unite(self, node1, node2):
        x = self.root(node1)
        y = self.root(node2)
        if x == y:
            pass
        elif 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

n, k, l = map(int, input().split())
roads = [map(int, input().split()) for _ in range(k)]
trains = [map(int, input().split()) for _ in range(l)]

uf1 = UnionFind(n+1)
for p, q in roads:
  uf1.unite(p, q)

uf2 = UnionFind(n+1)
for r, s in trains:
  if uf1.root(r) == uf1.root(s):
    uf2.unite(r, s)

count = collections.Counter([uf2.root(i) for i in range(1,n+1)])

print(' '.join([str(count[uf2.root(i)]) for i in range(1,n+1)]))