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:
# 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 = [0] * N

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

print(*cnt)

Submission status: Wrong Answer

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

    def __init__(self,n):
        self.n = n
        self.par = [i for i in range(n)]

    def indep(self):
        dep =0
        for i in range(self.n):
            if i == self.par[i]:
                dep += 1

        return dep

    def unite(self,child,mas):
        if self.root(child) == self.root(mas):
            return
        else:
            self.par[self.root(child)] = self.root(mas)

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

    def same(self,x,y):
        if self.root(x) == self.root(y):
            return True
        else:
            return False
u1 = uf_tree(n)
u2 = uf_tree(n)

for i in range(k):
    c,m = map(int,input().split())
    u1.unite(c-1,m-1)

for i in range(l):
    c,m = map(int,input().split())
    u2.unite(c-1,m-1)

p1 = [0 for i in range(n)]
p2 = [0 for i in range(n)]
for i in range(n):
    p1[i] = u1.root(i)
    p2[i] = u2.root(i)
d1 = {}
d2 = {}

for i in range(n):
    if p1[i] not in d1:
        d1[p1[i]] = 1
    else:
        d1[p1[i]] += 1

    if p2[i] not in d2:
        d2[p2[i]] = 1
    else:
        d2[p2[i]] += 1

for i in range(n-1):
    print(min(d1[p1[i]],d2[p2[i]]),end=" ")
print(min(d1[p1[-1]],d2[p2[-1]]))

Submission status: Wrong Answer

Solution code:
N, K, L = map(int, input().split())
i=0
j=0
k=0
rel=[]
listc =[]
SK=[]
SL=[]
for _ in range(N):
    rel.append({})
for _ in range(K):
    SK.append(list(input().split()))
for _ in range(L):
    SL.append(list(input().split()))
for j in range(K):
    SK[j][0]=int(SK[j][0])-1
    SK[j][1]=int(SK[j][1])-1
for j in range(L):
    SL[j][0]=int(SL[j][0])-1
    SL[j][1]=int(SL[j][1])-1
for i in range(N):
    for j in range(i+1):
        if i in rel[j]:
            for k in range(K):
                if i == SK[k][0] and i<SK[k][1]:
                    rel[j] = rel[j].union({SK[k][1]})
                elif i == SK[k][1]:
                    rel[j]=rel[j].union({SK[k][0]})
                else:
                    continue
            k=0
            for k in range(L):
                if i == SL[k][0] and i<SL[k][1]:
                    rel[j] = rel[j].union({SL[k][1]})
                elif i == SL[k][1] and i<SL[k][0]:
                    rel[j] = rel[j].union({SL[k][0]})
                else:
                    continue
            break
        else:
            rel[i]={i}
            for k in range(K):
                if i == SK[k][0] and i<SK[k][1]:
                    rel[i] = rel[i].union({SK[k][1]})
                elif i == SK[k][1] and i<SK[k][1]:
                    rel[i]=rel[i].union({SK[k][0]})
                else:
                    continue
            for k in range(L):
                if i == SL[k][0] and i<SL[k][1]:
                    rel[i] = rel[i].union({SL[k][1]})
                elif i == SL[k][1] and i<SL[k][0]:
                    rel[i] = rel[i].union({SL[k][0]})
                else:
                    continue
            break
for i in range(N):
    for j in range(N):
        if i in rel[j]:
            listc.append(len(list(rel[j])))
Str = str(listc[0])
i=0
for i in range(N-1):
    Str = Str+" "+str(listc[i+1])
print(Str)

Submission status: Wrong Answer

Solution code:
N, K, L = map(int, input().split())
i=0
j=0
k=0
rel=[]
listc =[]
SK=[]
SL=[]
for _ in range(N):
    rel.append({})
for _ in range(K):
    SK.append(list(input().split()))
for _ in range(L):
    SL.append(list(input().split()))
for j in range(K):
    SK[j][0]=int(SK[j][0])-1
    SK[j][1]=int(SK[j][1])-1
for j in range(L):
    SL[j][0]=int(SL[j][0])-1
    SL[j][1]=int(SL[j][1])-1
for i in range(N):
    for j in range(i+1):
        if i in rel[j]:
            for k in range(K):
                if i == SK[k][0] and i<SK[k][1]:
                    rel[j] = rel[j].union({SK[k][1]})
                elif i == SK[k][1]:
                    rel[j]=rel[j].union({SK[k][0]})
                else:
                    continue
            k=0
            for k in range(L):
                if i == SL[k][0] and i<SL[k][1]:
                    rel[j] = rel[j].union({SL[k][1]})
                elif i == SL[k][1] and i<SL[k][0]:
                    rel[j] = rel[j].union({SL[k][0]})
                else:
                    continue
            break
        else:
            rel[i]={i}
            for k in range(K):
                if i == SK[k][0] and i<SK[k][1]:
                    rel[i] = rel[i].union({SK[k][1]})
                elif i == SK[k][1] and i<SK[k][1]:
                    rel[i]=rel[i].union({SK[k][0]})
                else:
                    continue
            for k in range(L):
                if i == SL[k][0] and i<SL[k][1]:
                    rel[i] = rel[i].union({SL[k][1]})
                elif i == SL[k][1] and i<SL[k][0]:
                    rel[i] = rel[i].union({SL[k][0]})
                else:
                    continue
            break
for i in range(N):
    for j in range(N):
        if i in rel[j]:
            listc.append(len(list(rel[j])))
print(listc)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n+1))
        self.rank = [0]*(n+1)

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

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

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


N, K, L = map(int, input().split())
uf = UnionFind(N)
ans = [1]*N
for _ in range(K):
    x, y = map(int, input().split())
    uf.unite(x, y)

for _ in range(L):
    x, y = map(int, input().split())
    if uf.are_same(x, y):
        ans[x-1] += 1
        ans[y-1] += 1

print(' '.join([str(_) for _ in ans]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC54 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n,k,l = map(int,input().split())
par1 = [i for i in range(n)]
par2 = [i for i in range(n)]

rank1 = [0]*(n)
rank2 = [0]*(n)

def find(x,i):
    if i == 1:
        if par1[x] == x:
            return x
        else:
            par1[x] = find(par1[x],i)
            return par1[x]
    else:
        if par2[x] == x:
            return x
        else:
            par2[x] = find(par2[x],i)
            return par2[x]

def union(x,y,i):
    x = find(x,i)
    y = find(y,i)
    if x == y:
        return
    if i == 1:
        if rank1[x] < rank1[y]:
            par1[x] = y
        else:
            par1[y] = x
            if rank1[x] == rank1[y]:
                rank1[x] += 1
    else:
        if rank2[x] < rank2[y]:
            par2[x] = y
        else:
            par2[y] = x
            if rank2[x] == rank2[y]:
                rank2[x] += 1

for _ in range(k):
    p,q = map(int,input().split())
    union(p-1,q-1,1)
for _ in range(l):
    r,s = map(int,input().split())
    union(r-1,s-1,2)

x = [(par1[i],par2[i]) for i in range(n)]
y = defaultdict(lambda : 0)
for i in x:
    y[i] += 1
ans = []
for i in x:
    ans.append(y[i])
print(*ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC54 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n,k,l = map(int,input().split())
par1 = [i for i in range(n)]
par2 = [i for i in range(n)]

rank1 = [0]*(n)
rank2 = [0]*(n)

def find(x,i):
    if i == 1:
        if par1[x] == x:
            return x
        else:
            par1[x] = find(par1[x],i)
            return par1[x]
    else:
        if par2[x] == x:
            return x
        else:
            par2[x] = find(par2[x],i)
            return par2[x]

def check(x,y,i):
    return find(x,i) == find(y,i)

def union(x,y,i):
    x = find(x,i)
    y = find(y,i)
    if i == 1:
        if rank1[x] < rank1[y]:
            par1[x] = y
        else:
            par1[y] = x
            if rank1[x] == rank1[y]:
                rank1[x] += 1
    else:
        if rank2[x] < rank2[y]:
            par2[x] = y
        else:
            par2[y] = x
            if rank2[x] == rank2[y]:
                rank2[x] += 1

for _ in range(k):
    p,q = map(int,input().split())
    union(p-1,q-1,1)
for _ in range(l):
    r,s = map(int,input().split())
    union(r-1,s-1,2)

x = [(par1[j],par2[j]) for j in range(n)]
y = defaultdict(lambda : 0)
for i in x:
    y[i] += 1
ans = []
for i in x:
    ans.append(y[i])
print(*ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC54 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n,k,l = map(int,input().split())
par1 = [i for i in range(n)]
par2 = [i for i in range(n)]

rank1 = [0]*(n)
rank2 = [0]*(n)

def find(x,i):
    if i == 1:
        if par1[x] == x:
            return x
        else:
            par1[x] = find(par1[x],i)
            return par1[x]
    else:
        if par2[x] == x:
            return x
        else:
            par2[x] = find(par2[x],i)
            return par2[x]

def check(x,y,i):
    return find(x,i) == find(y,i)

def union(x,y,i):
    x = find(x,i)
    y = find(y,i)
    if i == 1:
        if rank1[x] < rank1[y]:
            par1[x] = y
        else:
            par1[y] = x
            if rank1[x] == rank1[y]:
                rank1[x] += 1
    else:
        if rank2[x] < rank2[y]:
            par2[x] = y
        else:
            par2[y] = x
            if rank2[x] == rank2[y]:
                rank2[x] += 1

for _ in range(k):
    p,q = map(int,input().split())
    union(p-1,q-1,1)
for _ in range(l):
    r,s = map(int,input().split())
    union(r-1,s-1,2)

x = [(par1[i],par2[i]) for i in range(n)]
y = defaultdict(lambda : 0)
for i in x:
    y[i] += 1
ans = []
for i in x:
    ans.append(y[i])
print(*ans)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
n, k, l = [ int(v) for v in input().split() ]
road_parent = [ i for i in range(n) ]
rail_parent = [ i for i in range(n) ]

def parent(x,a,inlist):
    if inlist[x] == x:
        return x, a
    else:
        return parent(inlist[x],a+1,inlist)

def union(z,inlist):
    x, y = z[0], z[1]
    inlist[x], rank_x = parent(x,0,inlist)
    inlist[y], rank_y = parent(y,0,inlist)
    if inlist[x] != inlist[y]:
        if rank_x > rank_y:
            inlist[inlist[y]] = inlist[x]
        else:
            inlist[inlist[x]] = inlist[y]
    return

for i in range(k):
    road = [ int(v)-1 for v in input().split() ]
    union(road,road_parent)

for i in range(l):
    rail = [ int(v)-1 for v in input().split() ]
    union(rail,rail_parent)

road_parent = [ parent(i,0,road_parent)[0] for i in range(n) ]
rail_parent = [ parent(i,0,rail_parent)[0] for i in range(n) ]
print(road_parent)
print(rail_parent)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, k, l = [ int(v) for v in input().split() ]
road_parent = [ i for i in range(n) ]
road_set = [ set([i]) for i in range(n) ]
road_list =  [ tuple( int(v)-1 for v in input().split() ) for i in range(k) ]
rail_parent = [ i for i in range(n) ]
rail_set = [ set([i]) for i in range(n) ]
rail_list =  [ tuple( int(v)-1 for v in input().split() ) for i in range(l) ]

def parent(x,a,inlist):
    if inlist[x] == x:
        return x, a
    else:
        return parent(inlist[x],a+1,inlist)

def union(z,inlist):
    x, y = z[0], z[1]
    inlist[x], rank_x = parent(x,0,inlist)
    inlist[y], rank_y = parent(y,0,inlist)
    if inlist[x] != inlist[y]:
        if rank_x > rank_y:
            inlist[inlist[y]] = inlist[x]
        else:
            inlist[inlist[x]] = inlist[y]
    return

for i in road_list:
    union(i,road_parent)

for i in rail_list:
    union(i,rail_parent)

road_parent = [ parent(i,0,road_parent)[0] for i in range(n) ]
rail_parent = [ parent(i,0,rail_parent)[0] for i in range(n) ]
print(road_parent)
print(rail_parent)

Submission status: Wrong Answer

Solution code:
# 2019/07/14

from collections import Counter


class unionfind():
    def __init__(self,n):
        self.par=list(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 is_same(self,x,y):
        return self.find(x)==self.find(y)
    def unite(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


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

uf1=unionfind(n)
uf2=unionfind(n)

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

for i in range(l):
    r,s=map(int,input().split())
    uf2.unite(r,s)

res=[]

for i in range(n+1):
    tmp=(uf1.find(i),uf2.find(i))
    res.append(tmp)
print(res)
c=Counter(res)
ans=[]
for i in range(n+1):
    ans.append(c[res[i]])
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
# 2019/07/13
# Union-Find
# Disjoint-Set

import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)

class union_find():
    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 is_same(self,x,y):
        return self.find(x)==self.find(y)
    def unite(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


n,k,l=map(int,input().split())
res=[1]*(n+1)
uf=union_find(n)
for i in range(k):
    p,q=map(int,input().split())
    uf.unite(p,q)

for i in range(l):
    r,s=map(int,input().split())
    if uf.is_same(r,s):
        res[r]+=1
        res[s]+=1

print(*res[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

class UnionFind():

    def __init__(self, n):
        self.par = [i for i in range(n)]

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

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

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

        if x == y:
            return
        
        self.par[x] = y
        return


def main():
    N, K, L = map(int, input().split())
    
    uf1 = UnionFind(N)
    uf2 = UnionFind(N)

    # O(K)
    for _ in range(K):
        p, q = map(int, input().split())
        uf1.unite(p-1, q-1)
    
    # O(L)
    for _ in range(L):
        r, s = map(int, input().split())
        uf2.unite(r-1, s-1)

    # O(N) x O(log_N)
    # 理解が少し難しいが、根ペアをキーに加算していく 
    # => 根から見ると、連結している頂点数（自身を含む）を数えている。
    cnts = defaultdict(int)
    for i in range(N):
        pos = (uf1.root(i), uf2.root(i))
        print(pos)
        cnts[pos] += 1

    ans = []
    for i in range(N):
        pos = (uf1.root(i), uf2.root(i))
        ans.append(cnts[pos])

    print(*ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

class UnionFind():

    def __init__(self, n):
        self.par = [i for i in range(n)]

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

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

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

        if x == y:
            return
        
        self.par[x] = y
        return


def main():
    N, K, L = map(int, input().split())
    
    uf1 = UnionFind(N)
    uf2 = UnionFind(N)

    # O(K)
    for _ in range(K):
        p, q = map(int, input().split())
        uf1.unite(p-1, q-1)
    
    # O(L)
    for _ in range(L):
        r, s = map(int, input().split())
        uf2.unite(r-1, s-1)

    # O(N) x O(log_N)
    # 理解が少し難しいが、根ペアをキーに加算していく 
    # => 根から見ると、連結している頂点数（自身を含む）を数えている。
    cnts = defaultdict(int)
    for i in range(N):
        pos = (uf1.root(i), uf2.root(i))
        print(pos)
        cnts[pos] += 1

    print(cnts)

    ans = []
    for i in range(N):
        pos = (uf1.root(i), uf2.root(i))
        ans.append(cnts[pos])

    print(*ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

class UnionFind:
    def __init__(self, n):
        self.data = [0] * N
        self.dd = [{i} for i in range(N)]
        for i in range(N):
            self.data[i] = i
            
    def get_root(self, x):
        r = self.data[x]
        if r == x:
            return r
        p = self.get_root(r)
        self.data[x] = p
        
        self.dd[x].add(p)
        self.dd[p].add(x)
        
        return p
    
    def unite(self, x, y):
        rx = self.get_root(x)
        ry = self.get_root(y)
        
        if rx != ry:
            self.data[rx] = ry

            self.dd[x].add(y)
            self.dd[y].add(x)
            
    def same(self, x, y):
        return self.get_root(x) == self.get_root(y)

N, K, L = map(int, input().split())
LA = [[int(v) - 1 for v in input().split()] for _ in range(K)]
LB = [[int(v) - 1 for v in input().split()] for _ in range(L)]

u1 = UnionFind(N)
for x, y in LA:
    u1.unite(x, y)
    
u2 = UnionFind(N)
for x, y in LB:
    u2.unite(x, y)
    
exit()
result = [0] * N
for i in range(N):
    a = u1.get_root(i)
    b = u2.get_root(i)
    
    result[i] = len(u1.dd[a] & u2.dd[b])
    
print(" ".join(map(str, result)))

Submission status: Wrong Answer

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

class UnionFind:
    def __init__(self, n):
        self.data = [0] * N
        self.dd = [{i} for i in range(N)]
        for i in range(N):
            self.data[i] = i
            
    def get_root(self, x):
        r = self.data[x]
        if r == x:
            return r
        p = self.get_root(r)
        self.data[x] = p
        
        self.dd[x].add(p)
        self.dd[p].add(x)
        
        return p
    
    def unite(self, x, y):
        rx = self.get_root(x)
        ry = self.get_root(y)
        
        if rx != ry:
            self.data[rx] = ry

            self.dd[x].add(y)
            self.dd[y].add(x)
            
    def same(self, x, y):
        return self.get_root(x) == self.get_root(y)

N, K, L = map(int, input().split())
LA = [[int(v) - 1 for v in input().split()] for _ in range(K)]
LB = [[int(v) - 1 for v in input().split()] for _ in range(L)]

u1 = UnionFind(N)
for x, y in LA:
    u1.unite(x, y)
    
u2 = UnionFind(N)
for x, y in LB:
    u2.unite(x, y)
    
result = [0] * N
for i in range(N):
    a = u1.get_root(i)
    b = u2.get_root(i)
    
    result[i] = len(u1.dd[a] & u2.dd[b])
    
print(" ".join(map(str, result)))

Submission status: Wrong Answer

Solution code:
print('1 2 2 1')
print('1 2 2 1')

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(1000000000)
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 unite(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)


N, K, L = map(int,  input().split())
mark = 0
U1 = UnionFind(N)
U2 = UnionFind(N)
for i in range(K):
    p, q = map(int, input().split())
    U1.unite(p-1, q-1)
for i in range(L):
    r, s = map(int, input().split())
    U2.unite(r-1, s-1)
List=[]
for i in range(N):
    List.append((U1.par[i], U2.par[i], i))
List = sorted(List)
cnt = 0
member = []
ans = [0]*N
for i in range(N-1):
    if List[i][0]==List[i+1][0] and List[i][1]==List[i+1][1]:
        cnt+=1
        if member:
            member.append(List[i+1][2])
        else:
            member.append(List[i][2])
            member.append(List[i+1][2])
    else:
        for j in member:
            ans[j]=cnt
        cnt = 0
        member = []
for j in member:
            ans[j]=cnt
for i in range(N):
    print(ans[i]+1)

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


N, K, L = map(int,  input().split())
mark = 0
U1 = UnionFind(N)
U2 = UnionFind(N)
for i in range(K):
    p, q = map(int, input().split())
    U1.unite(p-1, q-1)
for i in range(L):
    r, s = map(int, input().split())
    U2.unite(r-1, s-1)
List=[]
for i in range(N):
    List.append((U1.par[i], U2.par[i], i))
List = sorted(List)
cnt = 0
member = []
ans = [0]*N
for i in range(N-1):
    if List[i][0]==List[i+1][0] and List[i][1]==List[i+1][1]:
        cnt+=1
        if member:
            member.append(List[i+1][2])
        else:
            member.append(List[i][2])
            member.append(List[i+1][2])
    else:
        for j in member:
            ans[j]=cnt
        cnt = 0
        member = []
for j in member:
            ans[j]=cnt
for i in range(N):
    print(ans[i]+1)

Submission status: Wrong Answer

Solution code:
class UnionFind(object):
    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 unite(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)


N, K, L = map(int,  input().split())
mark = 0
U1 = UnionFind(N)
U2 = UnionFind(N)
for i in range(K):
    p, q = map(int, input().split())
    U1.unite(p-1, q-1)
for i in range(L):
    r, s = map(int, input().split())
    U2.unite(r-1, s-1)
List=[]
for i in range(N):
    List.append((U1.parent[i], U2.parent[i], i))
List = sorted(List)
cnt = 0
member = []
ans = [0]*N
for i in range(N-1):
    if List[i][0]==List[i+1][0] and List[i][1]==List[i+1][1]:
        cnt+=1
        if member:
            member.append(List[i+1][2])
        else:
            member.append(List[i][2])
            member.append(List[i+1][2])
    else:
        for j in member:
            ans[j]=cnt
        cnt = 0
        member = []
for j in member:
            ans[j]=cnt
for i in range(N):
    print(ans[i]+1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K, L = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(K)]
Y = [list(map(int, input().split())) for _ in range(L)]

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = list(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 unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        
        if x == y:
            return
        
        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(self, x, y):
        return self.find(x) == self.find(y)
    
    def __repr__(self):
        return ', '.join(str(x) for x in self.par)

t1 = UnionFind(N)
t2 = UnionFind(N)

for p, q in X:
    t1.unite(p - 1, q - 1)
    
for r, s in Y:
    t2.unite(r - 1, s - 1)

connect = [(a, b) for a, b in zip(t1.par, t2.par)]
ctr = Counter(connect)
print(*[ctr[key] for key in connect])

Submission status: Wrong Answer

Solution code:
import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18

N, K, L = list(map(int, sys.stdin.readline().split()))
P, Q = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(K)]))
R, S = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(L)]))


class UnionFind:
    def __init__(self, size=None, nodes=None):
        """
        size か nodes どっちか指定。
        nodes は set、size は list を使う。
        set の最悪計算量は O(N) なので size 指定のほうが若干速い
        :param int size:
        :param collections.Iterable nodes:
        """
        assert size is not None or nodes is not None
        if size is not None:
            self._parents = [i for i in range(size)]
            self._ranks = [0 for _ in range(size)]
            self._sizes = [1 for _ in range(size)]
        else:
            self._parents = {k: k for k in nodes}
            self._ranks = {k: 0 for k in nodes}
            self._sizes = {k: 1 for k in nodes}

    def unite(self, x, y):
        """
        x が属する木と y が属する木を併合
        :param x:
        :param y:
        :return:
        """
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return

        # rank が小さい方が下
        if self._ranks[x] > self._ranks[y]:
            # x が root
            self._parents[y] = x
            self._sizes[x] += self._sizes[y]
        else:
            # y が root
            self._parents[x] = y
            self._sizes[y] += self._sizes[x]
            if self._ranks[x] == self._ranks[y]:
                self._ranks[y] += 1

    def find(self, x):
        """
        x が属する木の root
        :param x:
        :return:
        """
        if self._parents[x] == x:
            return x
        self._parents[x] = self.find(self._parents[x])
        return self._parents[x]

    def size(self, x):
        """
        x が属する木のノード数
        :param x:
        :return:
        """
        return self._sizes[self.find(x)]


uf1 = UnionFind(size=N + 1)
for p, q in zip(P, Q):
    uf1.unite(p, q)
uf2 = UnionFind(size=N + 1)
for r, s in zip(R, S):
    if uf1.find(r) == uf1.find(s):
        uf2.unite(r, s)

ans = []
for i in range(1, N + 1):
    ans.append(uf2.size(i))
print(' '.join(map(str, ans)))