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:
from operator import itemgetter
import sys
input = sys.stdin.readline

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


class UnionFind():
    # 作りたい要素数nで初期化
    # 使用するインスタンス変数の初期化
    def __init__(self, n):
        self.n = n
        # root[x]<0ならそのノードが根かつその値が木の要素数
        # rootノードでその木の要素数を記録する
        self.root = [-1]*(n+1)
        # 木をくっつける時にアンバランスにならないように調整する
        self.rnk = [0]*(n+1)

    # ノードxのrootノードを見つける
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            # ここで代入しておくことで、後の繰り返しを避ける
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    # 木の併合、入力は併合したい各ノード
    def Unite(self, x, y):
        # 入力ノードのrootノードを見つける
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        # すでに同じ木に属していた場合
        if(x == y):
            return 
        # 違う木に属していた場合rnkを見てくっつける方を決める
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            # rnkが同じ（深さに差がない場合）は1増やす
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    # xとyが同じグループに属するか判断
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    # ノードxが属する木のサイズを返す
    def Count(self, x):
        return -self.root[self.Find_Root(x)]


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

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

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

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

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

Submission status: Wrong Answer

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

#Union-Find
par=[-1 for i in range(n+1)]
def root(a):
    if par[a]<0:
        return a
    else:
        return root(par[a])
def f(a):
    if road[a]<0:
        return a
    else:
        return root(road[a])
def f2(a):
    if rail[a]<0:
        return a
    else:
        return root(rail[a])
def size(a):
    return -par[root(a)]
def connect(a,b):
    a=root(a)
    b=root(b)
    if a==b:
        return False
    if size(a)<size(b):
        a,b=b,a
    par[a]+=par[b]
    par[b]=a
    return True

for i in range(k):
    a=[int(j) for j in input().split()]
    if root(a[0])!=root(a[1]):
        connect(a[0],a[1])
import copy
road=copy.deepcopy(par)

par=[-1 for i in range(n+1)]
ans=[1 for i in range(n)]
for i in range(l):
    r,s=[int(j) for j in input().split()]
    if root(r)!=root(s):
        connect(r,s)
rail=par
l=[(f(i),f2(i)) for i in range(1,1+n)]
from collections import Counter
c=Counter(l)
print(*(c[i] for i in l))

Submission status: Wrong Answer

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

#Union-Find
par=[-1 for i in range(n)]
def root(a):
    if par[a]<0:
        return a
    else:
        return root(par[a])
def f(a):
    if road[a]<0:
        return a
    else:
        return root(road[a])
def f2(a):
    if rail[a]<0:
        return a
    else:
        return root(rail[a])
def size(a):
    return -par[root(a)]
def connect(a,b):
    a=root(a)
    b=root(b)
    if a==b:
        return False
    if size(a)<size(b):
        a,b=b,a
    par[a]+=par[b]
    par[b]=a
    return True

for i in range(k):
    a=[int(j)-1 for j in input().split()]
    if root(a[0])!=root(a[1]):
        connect(a[0],a[1])
import copy
road=copy.deepcopy(par)

par=[-1 for i in range(n)]
ans=[1 for i in range(n)]
for i in range(l):
    r,s=[int(j)-1 for j in input().split()]
    if root(r)!=root(s):
        connect(r,s)
rail=par
l=[(f(i),f2(i)) for i in range(n)]
from collections import Counter
c=Counter(l)
print(*(c[i] for i in l))

Submission status: Wrong Answer

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

#Union-Find
par=[-1 for i in range(n)]    #par<0なら自身が親で要素の個数、>0なら子で親の位置示す

def root(a):
    if par[a]<0:
        return a
    else:
        return root(par[a])

def size(a):
    return -par[root(a)]

def connect(a,b):
    a=root(a)
    b=root(b)
    if a==b:
        return False
    if size(a)<size(b):
        a,b=b,a
    par[a]+=par[b]
    par[b]=a
    return True

for i in range(k):
    a=[int(j)-1 for j in input().split()]
    if root(a[0])!=root(a[1]):
        connect(a[0],a[1])

ans=[1 for i in range(n)]
for i in range(l):
    r,s=[int(j)-1 for j in input().split()]
    if root(r)==root(s):
        ans[r]+=1
        ans[s]+=1
print(*ans)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, N):
        self.parent = [x for x in range(N+1)]
        self.rank = [1 for x in range(N+1)]

    def find(self, x):
        p = x
        while self.parent[p] != p:
            p = self.parent[p]
        self.parent[x] = p
        return p

    def union(self, r, l):
        x, y = self.find(r), self.find(l)
        if x == y:
            return
        rx, ry = self.rank[x], self.rank[y]
        if rx > ry:
            self.parent[y] = x
        elif rx == ry:
            self.parent[y] = x
            self.rank[x] += 1
        else:
            self.parent[x] = y

def main():
    N, K, L = map(int, input().split())
    
    rails = UnionFind(N)
    for _ in range(K):
        p, q = map(int, input().split())
        rails.union(p, q)

    loads = UnionFind(N)
    for _ in range(L):
        p, q = map(int, input().split())
        loads.union(p,q)
    print("1")
    RL = [(rails.find(u), loads.find(u)) for u in range(1, N+1)]
    from collections import Counter
    c = Counter(RL)

    print(*(c[x] for x in RL))

main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

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

pq=[list(map(int,input().split())) for _ in range(K)]
rs=[list(map(int,input().split())) for _ in range(L)]

city_road=set()
city_road_train=set()

city_roadgroup_dict={}
roadgroup_city_dict={}

city_traingroup_dict={}
traingroup_city_dict={}

for ch_pq in pq:
  for pq_i in range(2): 
    if not ch_pq[pq_i] in city_road:
      city_road.add(ch_pq[pq_i])
    
  if not ch_pq[0] in city_roadgroup_dict:
    if not ch_pq[1] in city_roadgroup_dict:
      new_group_num=ch_pq[0]
      city_roadgroup_dict[ch_pq[0]]=ch_pq[0]
      city_roadgroup_dict[ch_pq[1]]=ch_pq[0]
      roadgroup_city_dict[new_group_num]=set([ch_pq[0],ch_pq[1]])
    else:
      group_num=city_roadgroup_dict[ch_pq[1]]
      city_roadgroup_dict[ch_pq[0]]=group_num
      roadgroup_city_dict[group_num].add(ch_pq[0])
  else:
    if not ch_pq[1] in city_roadgroup_dict:
      group_num=city_roadgroup_dict[ch_pq[0]]
      city_roadgroup_dict[ch_pq[1]]=group_num
      roadgroup_city_dict[group_num].add(ch_pq[1])
    else:
      group_num_0=city_roadgroup_dict[ch_pq[0]]
      group_num_1=city_roadgroup_dict[ch_pq[1]]
      if group_num_0!=group_num_1:
        group_0=roadgroup_city_dict[group_num_0]
        group_1=roadgroup_city_dict[group_num_1]
        if len(group_0)<len(group_1):
          for city_n in list(group_0):
            city_roadgroup_dict[city_n]=group_num_1
          roadgroup_city_dict[group_num_1]|=group_0
          del(roadgroup_city_dict[group_num_0])

        else:
          for city_n in list(group_1):
            city_roadgroup_dict[city_n]=group_num_0
          roadgroup_city_dict[group_num_0]|=group_1
          del(roadgroup_city_dict[group_num_1])


for ch_rs in rs:
  for rs_i in range(2): 
    if not ch_rs[rs_i] in city_road_train and ch_rs[rs_i] in city_road:
      city_road_train.add(ch_rs[rs_i])
  
  if not ch_rs[0] in city_traingroup_dict:
    if not ch_rs[1] in city_traingroup_dict:
      new_group_num=ch_rs[0]
      city_traingroup_dict[ch_rs[0]]=new_group_num
      city_traingroup_dict[ch_rs[1]]=new_group_num
      traingroup_city_dict[new_group_num]=set([ch_rs[0],ch_rs[1]])
    else:
      group_num=city_traingroup_dict[ch_rs[1]]
      city_traingroup_dict[ch_rs[0]]=group_num
      traingroup_city_dict[group_num].add(ch_rs[0])
  else:
    if not ch_rs[1] in city_traingroup_dict:
      group_num=city_traingroup_dict[ch_rs[0]]
      city_traingroup_dict[ch_rs[1]]=group_num
      traingroup_city_dict[group_num].add(ch_rs[1])
    else:
      group_num_0=city_traingroup_dict[ch_rs[0]]
      group_num_1=city_traingroup_dict[ch_rs[1]]
      if group_num_0!=group_num_1:
        group_0=traingroup_city_dict[group_num_0]
        group_1=traingroup_city_dict[group_num_1]
        if len(group_0)<len(group_1):
          for city_n in list(group_0):
            city_traingroup_dict[city_n]=group_num_1
          traingroup_city_dict[group_num_1]|=group_0
          del(traingroup_city_dict[group_num_0])

        else:
          for city_n in list(group_1):
            city_traingroup_dict[city_n]=group_num_0
          traingroup_city_dict[group_num_0]|=group_1
          del(traingroup_city_dict[group_num_1])
          
city_both_conn_num=['1']*N
ched_city=set()
for ch_city in list(city_road_train):
  if not ch_city in ched_city:
    road_group_num=city_roadgroup_dict[ch_city]
    train_group_num=city_traingroup_dict[ch_city]
    intersect_group=roadgroup_city_dict[road_group_num] & traingroup_city_dict[train_group_num]
    ched_city|=roadgroup_city_dict[road_group_num]
    ched_city|=traingroup_city_dict[train_group_num]
    
    conn_size=str(len(intersect_group))
    for conn_city in list(intersect_group):
      city_both_conn_num[conn_city-1]=conn_size


print(' '.join(city_both_conn_num))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

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

pq=[list(map(int,input().split())) for _ in range(K)]
rs=[list(map(int,input().split())) for _ in range(L)]

city_road=set()
city_road_train=set()

city_roadgroup_dict={}
roadgroup_city_dict={}

city_traingroup_dict={}
traingroup_city_dict={}

for ch_pq in pq:
  for pq_i in range(2): 
    if not ch_pq[pq_i] in city_road:
      city_road.add(ch_pq[pq_i])
    
  if not ch_pq[0] in city_roadgroup_dict:
    if not ch_pq[1] in city_roadgroup_dict:
      new_group_num=ch_pq[0]
      city_roadgroup_dict[ch_pq[0]]=ch_pq[0]
      city_roadgroup_dict[ch_pq[1]]=ch_pq[0]
      roadgroup_city_dict[new_group_num]=set([ch_pq[0],ch_pq[1]])
    else:
      group_num=city_roadgroup_dict[ch_pq[1]]
      city_roadgroup_dict[ch_pq[0]]=group_num
      roadgroup_city_dict[group_num].add(ch_pq[0])
  else:
    if not ch_pq[1] in city_roadgroup_dict:
      group_num=city_roadgroup_dict[ch_pq[0]]
      city_roadgroup_dict[ch_pq[1]]=group_num
      roadgroup_city_dict[group_num].add(ch_pq[1])
    else:
      group_num_0=city_roadgroup_dict[ch_pq[0]]
      group_num_1=city_roadgroup_dict[ch_pq[1]]
      if group_num_0!=group_num_1:
        group_0=roadgroup_city_dict[group_num_0]
        group_1=roadgroup_city_dict[group_num_1]
        if len(group_0)<len(group_1):
          for city_n in list(group_0):
            city_roadgroup_dict[city_n]=group_num_1
          roadgroup_city_dict[group_num_1]|=group_0
          del(roadgroup_city_dict[group_num_0])

        else:
          for city_n in list(group_1):
            city_roadgroup_dict[city_n]=group_num_0
          roadgroup_city_dict[group_num_0]|=group_1
          del(roadgroup_city_dict[group_num_1])


for ch_rs in rs:
  for rs_i in range(2): 
    if not ch_rs[rs_i] in city_road_train and ch_rs[rs_i] in city_road:
      city_road_train.add(ch_rs[rs_i])
  
  if not ch_rs[0] in city_traingroup_dict:
    if not ch_rs[1] in city_traingroup_dict:
      new_group_num=ch_rs[0]
      city_traingroup_dict[ch_rs[0]]=new_group_num
      city_traingroup_dict[ch_rs[1]]=new_group_num
      traingroup_city_dict[new_group_num]=set([ch_rs[0],ch_rs[1]])
    else:
      group_num=city_traingroup_dict[ch_rs[1]]
      city_traingroup_dict[ch_rs[0]]=group_num
      traingroup_city_dict[group_num].add(ch_rs[0])
  else:
    if not ch_rs[1] in city_traingroup_dict:
      group_num=city_traingroup_dict[ch_rs[0]]
      city_traingroup_dict[ch_rs[1]]=group_num
      traingroup_city_dict[group_num].add(ch_rs[1])
    else:
      group_num_0=city_traingroup_dict[ch_rs[0]]
      group_num_1=city_traingroup_dict[ch_rs[1]]
      if group_num_0!=group_num_1:
        group_0=traingroup_city_dict[group_num_0]
        group_1=traingroup_city_dict[group_num_1]
        if len(group_0)<len(group_1):
          for city_n in list(group_0):
            city_traingroup_dict[city_n]=group_num_1
          traingroup_city_dict[group_num_1]|=group_0
          del(traingroup_city_dict[group_num_0])

        else:
          for city_n in list(group_1):
            city_traingroup_dict[city_n]=group_num_0
          traingroup_city_dict[group_num_0]|=group_1
          del(traingroup_city_dict[group_num_1])
          
city_both_conn_num=['1']*N
ched_city=set()
for ch_city in list(city_road_train):
  if not ch_city in ched_city:
    road_group_num=city_roadgroup_dict[ch_city]
    train_group_num=city_traingroup_dict[ch_city]
    intersect_group=roadgroup_city_dict[road_group_num] & traingroup_city_dict[train_group_num]
    ched_city|=roadgroup_city_dict[road_group_num]
    ched_city|=traingroup_city_dict[train_group_num]
    city_both_conn_num[ch_city-1]=str(len(intersect_group))


print(' '.join(city_both_conn_num))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

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

pq=[list(map(int,input().split())) for _ in range(K)]
rs=[list(map(int,input().split())) for _ in range(L)]

city_road=set()
city_road_train=set()

city_roadgroup_dict={}
roadgroup_city_dict={}

city_traingroup_dict={}
traingroup_city_dict={}

for ch_pq in pq:
  for pq_i in range(2): 
    if not ch_pq[pq_i] in city_road:
      city_road.add(ch_pq[pq_i])
    
  if not ch_pq[0] in city_roadgroup_dict:
    if not ch_pq[1] in city_roadgroup_dict:
      new_group_num=len(roadgroup_city_dict)
      city_roadgroup_dict[ch_pq[0]]=new_group_num
      city_roadgroup_dict[ch_pq[1]]=new_group_num
      roadgroup_city_dict[new_group_num]=set([ch_pq[0],ch_pq[1]])
    else:
      group_num=city_roadgroup_dict[ch_pq[1]]
      city_roadgroup_dict[ch_pq[0]]=group_num
      roadgroup_city_dict[group_num].add(ch_pq[0])
  elif not ch_pq[1] in city_roadgroup_dict:
    group_num=city_roadgroup_dict[ch_pq[0]]
    city_roadgroup_dict[ch_pq[1]]=group_num
    roadgroup_city_dict[group_num].add(ch_pq[1])

for ch_rs in rs:
  for rs_i in range(2): 
    if not ch_rs[rs_i] in city_road_train and ch_rs[rs_i] in city_road:
      city_road_train.add(ch_rs[rs_i])
  
  if not ch_rs[0] in city_traingroup_dict:
    if not ch_rs[1] in city_traingroup_dict:
      new_group_num=len(traingroup_city_dict)
      city_traingroup_dict[ch_rs[0]]=new_group_num
      city_traingroup_dict[ch_rs[1]]=new_group_num
      traingroup_city_dict[new_group_num]=set([ch_rs[0],ch_rs[1]])
    else:
      group_num=city_traingroup_dict[ch_rs[1]]
      city_traingroup_dict[ch_rs[0]]=group_num
      traingroup_city_dict[group_num].add(ch_rs[0])
  elif not ch_rs[1] in city_traingroup_dict:
    group_num=city_traingroup_dict[ch_rs[0]]
    city_traingroup_dict[ch_rs[1]]=group_num
    traingroup_city_dict[group_num].add(ch_rs[1])
  
city_both_conn_num=['1']*N
for ch_city in list(city_road_train):
  road_group_num=city_roadgroup_dict[ch_city]
  train_group_num=city_traingroup_dict[ch_city]
  intersect_group=roadgroup_city_dict[road_group_num] & traingroup_city_dict[train_group_num]
  city_both_conn_num[ch_city-1]=str(len(intersect_group))


print(' '.join(city_both_conn_num))

Submission status: Wrong Answer

Solution code:
from sys import stdin

def find(parent, i):
  t = parent[i]
  if t == -1:
    return i
  t = find(parent, t)
  parent[i] = t
  return t

def unite(parent, i, j):
  i = find(parent, i)
  j = find(parent, j)
  if i == j:
    return
  parent[i] = j

def main():
  n, k, l = map(int, stdin.readline().split())

  roads = [-1] * n
  rails = [-1] * n

  for i in range(k):
    p, q = map(int, stdin.readline().split())
    unite(roads, p - 1, q - 1)

  for i in range(l):
    p, q = map(int, stdin.readline().split())
    unite(rails, p - 1, q - 1)

  d = {}
  for i in range(n):
    t = (find(roads, i), find(rails, i))
    if t in d:
      d[t] += 1
    else:
      d[t] = 1

  print(*[d[(find(roads, i), find(rails, i))] for i in range(n)])

Submission status: Wrong Answer

Solution code:
class UnionFind():
  def __init__(self, n):
    self.n = n
    # n+1なのはノード番号とリストのインデックスを一致させるため
    # root[x]<0ならそのノードが根かつその絶対値が木の要素数
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
  
  def Find_Root(self, x):
    if self.root[x] < 0:
      return x
    else:
      self.root[x] = self.Find_Root(self.root[x])
      return self.root[x]
    
  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return
    elif(self.rnk[x] > self.rnk[y]):
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1
 
  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)
  
  def Count(self, x):
    return -self.root[self.Find_Root(x)]
  
n, k, l = map(int, input().split())
UFd= UnionFind(n)
UFt= UnionFind(n)
 
for i in range(k):
  p, q = map(int, input().split())
  UFd.Unite(p, q)
 
for j in range(l):
  p, q = map(int, input().split())
  UFt.Unite(p, q)
  
ids = [0]*n
d = {}
for i in range(n):
  if UFd.root[i+1] < 0:
    idd = i+1
  else:
    idd = UFd.root[i+1]
  if UFt.root[i+1] < 0:
    idt = i+1
  else:
    idt = UFt.root[i+1]
  iddt = idd*10**12+idt
  ids[i] = iddt
  if iddt in d:
    d[iddt] += 1
  else:
    d[iddt] = 1
ans = ''
for i in range(n):
  if i != 0:
    ans += ' '
  ans += str(d[ids[i]])
print(ans)

Submission status: Wrong Answer

Solution code:
class UnionFind():
  def __init__(self, n):
    self.n = n
    # n+1なのはノード番号とリストのインデックスを一致させるため
    # root[x]<0ならそのノードが根かつその絶対値が木の要素数
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
  
  def Find_Root(self, x):
    if self.root[x] < 0:
      return x
    else:
      self.root[x] = self.Find_Root(self.root[x])
      return self.root[x]
    
  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return
    elif(self.rnk[x] > self.rnk[y]):
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1
 
  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)
  
  def Count(self, x):
    return -self.root[self.Find_Root(x)]
  
n, k, l = map(int, input().split())
UFd= UnionFind(n)
UFt= UnionFind(n)
 
for i in range(k):
  p, q = map(int, input().split())
  UFd.Unite(p, q)
 
for j in range(l):
  p, q = map(int, input().split())
  UFt.Unite(p, q)
  
ids = [0]*n
d = {}
for i in range(n):
  if UFd.root[i+1] < 0:
    idd = i+1
  else:
    idd = UFd.root[i+1]
  if UFt.root[i+1] < 0:
    idt = i+1
  else:
    idt = UFt.root[i+1]
  iddt = idd*10**6+idt
  ids[i] = iddt
  if iddt in d:
    d[iddt] += 1
  else:
    d[iddt] = 1
ans = ''
for i in range(n):
  if i != 0:
    ans += ' '
  ans += str(d[ids[i]])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
 
class UnionFind():
  def __init__(self, n):
    self.n = n
    # n+1なのはノード番号とリストのインデックスを一致させるため
    # root[x]<0ならそのノードが根かつその絶対値が木の要素数
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
  
  def Find_Root(self, x):
    if self.root[x] < 0:
      return x
    else:
      self.root[x] = self.Find_Root(self.root[x])
      return self.root[x]
    
  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return
    elif(self.rnk[x] > self.rnk[y]):
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1
 
  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)
  
  def Count(self, x):
    return -self.root[self.Find_Root(x)]
  
n, k, l = map(int, input().split())
UFd= UnionFind(n)
UFt= UnionFind(n)
 
for i in range(k):
  p, q = map(int, input().split())
  UFd.Unite(p, q)
 
for j in range(l):
  p, q = map(int, input().split())
  UFt.Unite(p, q)
  
ids = [0]*n
d = {}
for i in range(n):
  if UFd.root[i+1] < 0:
    idd = i+1
  else:
    idd = UFd.root[i+1]
  if UFt.root[i+1] < 0:
    idt = i+1
  else:
    idt = UFt.root[i+1]
  iddt = idd*10**6+idt
  ids[i] = iddt
  if iddt in d:
    d[iddt] += 1
  else:
    d[iddt] = 1
ans = ''
for i in range(n):
  if i != 0:
    ans += ' '
  ans += str(d[ids[i]])
print(ans)

Submission status: Wrong Answer

Solution code:
class UnionFind():
  def __init__(self, n):
    self.n = n
    # n+1なのはノード番号とリストのインデックスを一致させるため
    # root[x]<0ならそのノードが根かつその絶対値が木の要素数
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
  
  def Find_Root(self, x):
    if self.root[x] < 0:
      return x
    else:
      self.root[x] = self.Find_Root(self.root[x])
      return self.root[x]
    
  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return
    elif(self.rnk[x] > self.rnk[y]):
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1
 
  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)
  
  def Count(self, x):
    return -self.root[self.Find_Root(x)]
  
n, k, l = map(int, input().split())
UFd= UnionFind(n)
UFt= UnionFind(n)

for i in range(k):
  p, q = map(int, input().split())
  UFd.Unite(p, q)

for j in range(l):
  p, q = map(int, input().split())
  UFt.Unite(p, q)
  
ids = []
d = {}
for i in range(n):
  if UFd.root[i+1] > 0:
    idd = UFd.root[i+1]
  else:
    idd = i+1
  if UFt.root[i+1] > 0:
    idt = UFt.root[i+1]
  else:
    idt = i+1
  iddt = idd*10**6+idt
  ids.append(iddt)
  if iddt in d:
    d[iddt] += 1
  else:
    d[iddt] = 1
ans = ''
for i in range(n):
  if i != 0:
    ans += ''
  ans += str(d[ids[i]])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
def find_root(edges, root, group, before, now):
    group.append(now)
    children = edges[now]
    for child in children:
        if child != before and child not in group:
            root, group = find_root(edges, root, group, now, child)
    return root, group
    
if __name__ == '__main__':
    N, K, L = map(int, input().split())
    roads = [[] for _ in range(N)]
    rails = [[] for _ in range(N)]
    road_roots = [None]*N
    rail_roots = [None]*N
    answer = [1]*N
        
    for i in range(K):
        p, q = map(int, input().split())
        p -= 1
        q -= 1
        roads[p].append(q)
        roads[q].append(p)
    
    for i in range(L):
        r, s = map(int, input().split())
        r -= 1
        s -= 1
        rails[r].append(s)
        rails[s].append(r)
    
    for i, r in enumerate(road_roots):
        if r is None:
            root, group = find_root(roads, i, [], -1, i)
            for g in group:
                road_roots[g] = root

    for i, r in enumerate(rail_roots):
        if r is None:
            root, group = find_root(rails, i, [], -1, i)
            for g in group:
                rail_roots[g] = root
                
    railroads=[[rail, road] for rail, road in zip(road_roots, rail_roots)]

    print('test WA')
    for n in range(N):
        for d in range(1, N-n):
            if railroads[n] == railroads[n+d]:
                answer[n] += 1 
                answer[n+d] += 1
                
    print(' '.join(list(map(str, answer))))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N, K, L = map(int, input().split())
    PQ = [None]*K
    RS = [None]*L
    roads = [set() for _ in range(N)]
    rails = [set() for _ in range(N)]
    answer = [None]*N
        
    for i in range(K):
        p, q = map(int, input().split())
        p -= 1
        q -= 1
        if roads[p] and roads[q]: # どちらも空集合でない
            roads[p] |= roads[q]
            roads[q] |= roads[p]
        elif not roads[p] and roads[q]:
            roads[q].add(p)
            roads[p] = roads[q]
        elif roads[p] and not roads[q]:
            roads[p].add(q)
            roads[q] = roads[p]
        else:
            roads[p].add(p)
            roads[p].add(q)
            roads[q] = roads[p]

    for i in range(L):
        r, s = map(int, input().split())
        r -= 1
        s -= 1
        if rails[r] and rails[s]: # どちらも空集合でない
            rails[r] |= rails[s]
            rails[s] |= rails[r]
        elif not rails[r] and rails[s]:
            rails[s].add(r)
            rails[r] = rails[s]
        elif rails[r] and not rails[s]:
            rails[r].add(s)
            rails[s] = rails[r]
        else:
            rails[r].add(r)
            rails[r].add(s)
            rails[s] = rails[r]

    for n in range(N):
        roads[n].add(n)
        rails[n].add(n)
        answer[n]= len(roads[n] & rails[n])
    
    
            
    print(' '.join(map(str, answer)))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N, K, L = map(int, input().split())
    PQ = [None]*K
    RS = [None]*L
    roads = [set() for _ in range(N)]
    rails = [set() for _ in range(N)]
    answer = [None]*N
        
    for i in range(K):
        PQ[i] = list(map(int, input().split()))
    #PQ = sorted(PQ, key=min)
    
    
    for i in range(K):
        if not roads[PQ[i][1] - 1]:
            roads[PQ[i][0] - 1].add(PQ[i][0] - 1)
            roads[PQ[i][0] - 1].add(PQ[i][1] - 1)
            roads[PQ[i][1] - 1] = roads[PQ[i][0] - 1]
        else:
            roads[PQ[i][1] - 1].add(PQ[i][0] - 1)
            roads[PQ[i][1] - 1].add(PQ[i][1] - 1)
            roads[PQ[i][0] - 1] = roads[PQ[i][1] - 1]

    for i in range(L):
        RS[i] = list(map(int, input().split()))
    #RS = sorted(RS, key=min)
    
    for i in range(L):
        if not rails[RS[i][1] - 1]:
            rails[RS[i][0] - 1].add(RS[i][0] - 1)
            rails[RS[i][0] - 1].add(RS[i][1] - 1)
            rails[RS[i][1] - 1] = rails[RS[i][0] - 1]
        else:
            rails[RS[i][1] - 1].add(RS[i][0] - 1)
            rails[RS[i][1] - 1].add(RS[i][1] - 1)
            rails[RS[i][0] - 1] = rails[RS[i][1] - 1]

    for n in range(N):
        roads[n].add(n)
        rails[n].add(n)
        answer[n]= len(roads[n] & rails[n])
    
    
            
    print(' '.join(map(str, answer)))

Submission status: Wrong Answer

Solution code:
import sys;sys.setrecursionlimit(10**9)
class u():#UnionFind f-根探し u-連結 s-連結? c-要素数
  def __init__(self,n):self.n,self.r=[-1]*n,[0]*n
  def f(self,x):
    if self.n[x]<0:return x
    else:self.n[x]=self.f(self.n[x]);return self.n[x]
  def u(self,x,y):
    x,y=self.f(x),self.f(y)
    if x==y:return
    elif self.r[x]>self.r[y]:self.n[x]+=self.n[y];self.n[y]=x
    else:self.n[y]+=self.n[x];self.n[x]=y;self.r[y]+=self.r[x]==self.r[y]
  def s(self,x,y):return self.f(x)==self.f(y)
  def c(self,x):return -self.n[self.f(x)]
n,k,l=map(int,input().split())
d=u(n)
for i in range(k):
  a,b=map(int,input().split())
  d.u(a-1,b-1)
x=[];t=u(n)
for i in range(l):
  a,b=map(int,input().split())
  if d.s(a-1,b-1):t.u(a-1,b-1)
for i in range(n):x.append(t.c(i))
print(*x)

Submission status: Wrong Answer

Solution code:
# UnionFind
class UnionFind():

    #0~Nまでの頂点数で初期化していることに注意
    def __init__(self, n):
        self.n = n
        self.root = [-1] * n

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

    def Unite(self, x, y):
        x = self.FindRoot(x)
        y = self.FindRoot(y)

        if x == y:
            return False
        if self.Size(x) < self.Size(y):
            x, y = y, x
        self.root[x] += self.root[y]
        self.root[y] = x

        return True

    def isSameGroup(self, x, y):
        return self.FindRoot(x) == self.FindRoot(y)

    def Size(self, x):
        return -self.root[self.FindRoot(x)]

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

Uni1 = UnionFind(N)

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

cnt = [1] * N

for _ in range(L):
    r, s = map(int, input().split())
    if Uni1.isSameGroup(r-1, s-1):        
        cnt[r-1] += 1
        cnt[s-1] += 1

print(*cnt)

Submission status: Wrong Answer

Solution code:
# UnionFind
class UnionFind():

    #0~Nまでの頂点数で初期化していることに注意
    def __init__(self, n):
        self.n = n
        self.root = [-1] * n

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

    def Unite(self, x, y):
        x = self.FindRoot(x)
        y = self.FindRoot(y)

        if x == y:
            return False
        if self.Size(x) < self.Size(y):
            x, y = y, x
        self.root[x] += self.root[y]
        self.root[y] = x

        return True

    def isSameGroup(self, x, y):
        return self.FindRoot(x) == self.FindRoot(y)

    def Size(self, x):
        return -self.root[self.FindRoot(x)]

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

Uni1 = UnionFind(N)

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

Uni2 = UnionFind(N)

for _ in range(L):
    r, s = map(int, input().split())
    if Uni1.isSameGroup(r-1, s-1):
        Uni2.Unite(r-1, s-1)

cnt = [1] * N

for i in range(N):
    cnt[i] = Uni2.Size(i)

print(*cnt)