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 collections import defaultdict
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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


def same(par, x, y):
    return root(par, x) == root(par, y)


def unite(rank, par, x, y):
    x = root(par, x)
    y = root(par, 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 main():
    N, K, L = map(int, readline().split())
    m = map(int, read().split())
    pq = list(zip(m, m))
    pq, rs = pq[:K], pq[K:]

    d_c = defaultdict(list)
    d_t = defaultdict(list)

    par = list(range(N))
    rank = [0] * N
    for p, q in pq:
        p -= 1
        q -= 1
        unite(rank, par, p, q)

    par_t = list(range(N))
    rank_t = [0] * N
    for r, s in rs:
        r -= 1
        s -= 1
        unite(rank_t, par_t, r, s)

    for i in range(N):
        d_c[par[i]].append(i)
        d_t[par_t[i]].append(i)

    for i in range(N):
        d_c[i] = d_c[par[i]]
        d_t[i] = d_t[par_t[i]]

    cnt = [0] * N
    for i in range(N):
        cnt[i] = len(list(set(d_c[i]) & set(d_t[i])))
    print(*cnt)


main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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


def same(par, x, y):
    return root(par, x) == root(par, y)


def unite(rank, par, x, y):
    x = root(par, x)
    y = root(par, 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 main():
    N, K, L = map(int, readline().split())
    m = map(int, read().split())
    pq = list(zip(m, m))
    pq, rs = pq[:K], pq[K:]

    par = list(range(N))
    rank = [0] * N
    for p, q in pq:
        p -= 1
        q -= 1
        unite(rank, par, p, q)

    par_t = list(range(N))
    rank_t = [0] * N
    for r, s in rs:
        r -= 1
        s -= 1
        unite(rank_t, par_t, r, s)

    cnt = [0] * N
    for i in range(N):
        for j in range(N):
            if par[i] == par[j] and par_t[i] == par_t[j]:
                cnt[i] += 1

    print(*cnt)


main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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


def same(par, x, y):
    return root(par, x) == root(par, y)

def unite(rank, par, x, y):
    x = root(par, x)
    y = root(par, 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 main():
    N, K, L = map(int, readline().split())
    m = map(int, read().split())
    pq = list(zip(m, m))
    pq, rs = pq[:K], pq[K:]

    par = list(range(N))
    rank = [0] * N
    for p, q in pq:
        p -= 1
        q -= 1
        unite(rank, par, p, q)

    cnt = [1] * N
    for r, s in rs:
        r -= 1
        s -= 1
        if same(par, r, s):
            cnt[r] += 1
            cnt[s] += 1

    print(*cnt)


main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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


def unite(rank, par, x, y):
    x = root(par, x)
    y = root(par, 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 main():
    N, K, L = map(int, readline().split())
    m = map(int, read().split())
    pq = list(zip(m, m))
    pq, rs = pq[:K], pq[K:]

    par = list(range(N))
    rank = [0] * N
    for p, q in pq:
        p -= 1
        q -= 1
        unite(rank, par, p, q)

    cnt = [1] * N
    for r, s in rs:
        r -= 1
        s -= 1
        if par[r] == par[s]:
            cnt[r] += 1
            cnt[s] += 1

    print(*cnt)


main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    S = read().decode('utf-8')
    S = list(S.replace('\n', ''))[::-1]
    T = [
            list('dream')[::-1],
            list('dreamer')[::-1],
            list('erase')[::-1],
            list('eraser')[::-1]
        ]

    while len(S) != 0:
        if S[0:5] == T[0] or S[0:5] == T[2]:
            del S[0:5]
        elif S[0:7] == T[1]:
            del S[0:7]
        elif S[0:6] == T[3]:
            del S[0:6]
        else:
            print('NO')
            sys.exit()

    print('YES')


main()

Submission status: Wrong Answer

Solution code:
#D
import bisect
N,K,L = map(int,input().split())
PQ = [list(map(int,input().split())) for i in range(K)]
RS = [list(map(int,input().split())) for i in range(L)]

#UnionFind
class UF:
    def __init__(self,n):
        self.n = n
        self.root = [-1]*n #root[x]<0 ならxが根かつその値が木の要素数
        self.rank = [0]*n #木の深さ
    def __del__(self):
        pass
        
    def find(self,x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[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.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[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) #bool
    
    def count(self,x):
        return -self.root[self.find(x)]
    
uf1 = UF(N)
for pq in PQ:
    p,q = pq
    uf1.unite(p-1,q-1)
ar = [0]*N
for i in range(N):
    if uf1.find(i) < 0:
        ar[i] = i
    else:
        ar[i] = uf1.find(i)

del uf1

uf2 = UF(N)
for rs in RS:
    r,s = rs
    uf2.unite(r-1,s-1)
br = [0]*N
for i in range(N):
    if uf2.find(i) < 0:
        br[i] = i
    else:
        br[i] = uf2.find(i)
cr = [[ar[i],br[i]] for i in range(N)]
dr = cr[::]
dr.sort()
for i in range(N):
    print(bisect.bisect_right(dr,cr[i]) - bisect.bisect_left(dr,cr[i]) + 1,end=" ")

Submission status: Wrong Answer

Solution code:
#D
import bisect
N,K,L = map(int,input().split())
PQ = [list(map(int,input().split())) for i in range(K)]
RS = [list(map(int,input().split())) for i in range(L)]

#UnionFind
class UF:
    def __init__(self,n):
        self.n = n
        self.root = [-1]*n #root[x]<0 ならxが根かつその値が木の要素数
        self.rank = [0]*n #木の深さ
    def __del__(self):
        pass
        
    def find(self,x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[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.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[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) #bool
    
    def count(self,x):
        return -self.root[self.find(x)]
    
uf1 = UF(N)
for pq in PQ:
    p,q = pq
    uf1.unite(p-1,q-1)
ar = [0]*N
for i in range(N):
    if uf1.find(i) < 0:
        ar[i] = i
    else:
        ar[i] = uf1.find(i)

del uf1

uf2 = UF(N)
for rs in RS:
    r,s = rs
    uf2.unite(r-1,s-1)
br = [0]*N
for i in range(N):
    if uf2.find(i) < 0:
        br[i] = i
    else:
        br[i] = uf2.find(i)
cr = [[ar[i],br[i]] for i in range(N)]
dr = cr[::]
dr.sort()
for i in range(N):
    print(bisect.bisect_right(dr,cr[i]) - bisect.bisect_left(dr,cr[i]) + 1)

Submission status: Wrong Answer

Solution code:
#D
import bisect
N,K,L = map(int,input().split())
PQ = [list(map(int,input().split())) for i in range(K)]
RS = [list(map(int,input().split())) for i in range(L)]

#UnionFind
class UF:
    def __init__(self,n):
        self.n = n
        self.root = [-1]*n #root[x]<0 ならxが根かつその値が木の要素数
        self.rank = [0]*n #木の深さ
    def __del__(self):
        pass
        
    def find(self,x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[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.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[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) #bool
    
    def count(self,x):
        return -self.root[self.find(x)]
    
uf1 = UF(N)
for pq in PQ:
    p,q = pq
    uf1.unite(p-1,q-1)
ar = [0]*N
for i in range(N):
    if uf1.find(i) < 0:
        ar[i] = i
    else:
        ar[i] = uf1.find(i)

del uf1

uf2 = UF(N)
for rs in RS:
    r,s = rs
    uf2.unite(r-1,s-1)
br = [0]*N
for i in range(N):
    if uf2.find(i) < 0:
        br[i] = i
    else:
        br[i] = uf2.find(i)
"""
cr = [[ar[i],br[i]] for i in range(N)]
dr = cr[::]
dr.sort()
for i in range(N):
    print(bisect.bisect_right(dr,cr[i]) - bisect.bisect_left(dr,cr[i]) + 1)
"""
print("yes")

Submission status: Wrong Answer

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

    def find(self, x):
        if self.par[x] == x:
            return x
        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
            x, y = y, x
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        self.size[x] += self.size[y]

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

    def get_size(self, x):
        return self.size[self.find(x)]
      
def abc049_d():
    n, k, l = map(int, input().split())

    road = UnionFind(n)
    for i in range(k):
        p, q = map(int, input().split())
        road.unite(p - 1, q - 1)

    train = UnionFind(n)
    for i in range(l):
        r, s = map(int, input().split())
        train.unite(r - 1, s - 1)

    ans = []
    for i in range(n):
        a = 0
        a += 1 if road.get_size(i) > 1 else 0
        a += 1 if train.get_size(i) > 1 else 0
        ans.append(a)
    print(*ans)


if __name__ == '__main__':
    abc049_d()

Submission status: Wrong Answer

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

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

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

        if x == y:
            return

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

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

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

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

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

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

    def __str__(self):
        return '\n'.join('{}: {}'.format(r,self.members(r)) for r in self.roots())
N,K,L = map(int,input().split())
uf = UnionFind(N+1)
ans = [1]*(N+1)
for i in range(K):
    p,q = map(int,input().split())
    uf.union(p,q)
for i in range(L):
    r,s = map(int,input().split())
    if uf.same(r,s):
        ans[r] += 1
        ans[s] += 1
ans.pop(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**7)

N, K, L = [int(x) for x in input().strip().split()]
road_par = [n for n in range(N)]
road_rank = [0 for _ in range(N)]
train_par = [n for n in range(N)]
train_rank = [0 for _ in range(N)]

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

def union(par, rank, x, y):
    x = par[x]
    y = par[y]
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

for ｋ in range(K):
    p, q = [int(x) - 1 for x in input().strip().split()]
    union(road_par, road_rank, p, q)
for l in range(L):
    r, s = [int(x) - 1 for x in input().strip().split()]
    union(train_par, train_rank, r, s)

d = defaultdict(int)
for n in range(N):
    r = find(road_par, n)
    t = find(train_par, n)
    d[(r, t)] += 1

for n in range(N):
    print(d[(road_par[n], train_par[n])], end=' ')

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**7)

class UnionFind():
    def __init__(self, N):
        self.N = N
        self.par = [n for n in range(N)]
        self.rank = [0 for _ in range(N)]

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

    def union(self, x, y):
        x = self.par[x]
        y = self.par[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 isSame(self, x, y):
        return self.par[x] == self.par[y]

    def refresh(self):
        for p in range(N):
            self.find(p)

N, K, L = [int(x) for x in input().strip().split()]
road = UnionFind(N)
train = UnionFind(N)

for ｋ in range(K):
    p, q = [int(x) - 1 for x in input().strip().split()]
    road.union(p, q)
for l in range(L):
    r, s = [int(x) - 1 for x in input().strip().split()]
    train.union(r, s)

d = defaultdict(int)
for n in range(N):
    r = road.find(n)
    t = train.find(n)
    d[(r, t)] += 1

for n in range(N):
    print(d[(road.par[n], train.par[n])], end=' ')

Submission status: Wrong Answer

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

class UnionFind():
    def __init__(self, N):
        self.N = N
        self.par = [n for n in range(N)]
        self.rank = [0 for _ in range(N)]

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

    def union(self, x, y):
        x = self.par[x]
        y = self.par[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 isSame(self, x, y):
        return self.par[x] == self.par[y]

    def refresh(self):
        for p in range(N):
            self.find(p)

N, K, L = [int(x) for x in input().strip().split()]
road = UnionFind(N)
train = UnionFind(N)

for ｋ in range(K):
    p, q = [int(x) - 1 for x in input().strip().split()]
    road.union(p, q)
for l in range(L):
    r, s = [int(x) - 1 for x in input().strip().split()]
    train.union(r, s)

for n in range(N):
    road.find(n)
    train.find(n)

ans = [1] * N
for i in range(N - 1):
    for j in range(i + 1, N):
        if i == j:
            continue
        if road.par[i] == road.par[j] and train.par[i] == train.par[j]:
            ans[i] += 1
            ans[j] += 1

print(*ans)

Submission status: Wrong Answer

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

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

def unite(x,y):
    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)

 
def main():
    n,k,l = map(int,input().split()) 
 
    global par,rank
    par  = [i for i in range(2*n)]
    rank = [0 for _ in range(2*n)] 
    print(par)
    for _ in range(k):
        p,q = map(int,input().split())
        p-=1
        q-=1
        unite(p,q)
    for _ in range(l):
        p,q = map(int,input().split())
        p-=1
        q-=1
        unite(p+n,q+n)

    ans=[0 for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if same(i,j) and same(i+n,j+n):
                ans[i]+=1
    print(par)
    for i in range(n):
        print(ans[i])

main()

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

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

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

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

n,k,l = inpl()
ro = UnionFind(n)
tr = UnionFind(n)
for i in range(k):
    a,b = inpl()
    ro.union(a-1,b-1)
for i in range(l):
    a,b = inpl()
    tr.union(a-1,b-1)

d = defaultdict(int)
for i in range(n):
    d[(ro.par[i],tr.par[i])] += 1
for i in range(n):
    print(d[(ro.par[i],tr.par[i])])

Submission status: Wrong Answer

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

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

n,k,l = map(int,input().split())
Road = [list(map(int,input().split())) for i in range(k)]
Train = [list(map(int,input().split())) for i in range(l)]

uf_r = UnionFind(n)
uf = UnionFind(n)

for i in range(k):
    uf_r.union(Road[i][0]-1,Road[i][1]-1)

for i in range(l):
    if uf_r.find(Train[i][0]-1) == uf_r.find(Train[i][1]-1):
        uf.union(Train[i][0]-1,Train[i][1]-1)

ans = []
for i in range(n):
    ans.append(uf.size(i))

print(*ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
input = sys.stdin.readline


def main():
    n, k, l = map(int, input().split())
    p, q = [], []
    for _ in range(k):
        pi, qi = map(int, input().split())
        p.append(pi)
        q.append(qi)
    r, s = [], []
    for _ in range(l):
        ri, si = map(int, input().split())
        r.append(ri)
        s.append(si)

    road_tree = make_union_find(p, q, n)
    rail_tree = make_union_find(r, s, n)

    a, b = [], []
    x = defaultdict(int)
    for i in range(n):
        a.append(root(road_tree, i))
        b.append(root(rail_tree, i))
        key = (a[i], b[i])
        x[key] += 1

    for i in range(n):
        print(x[(a[i], b[i])], end=' ')

    print("\n")
    print(road_tree)
    print(rail_tree)


def make_union_find(a, b, n):
    union_find_tree = [i for i in range(n)]

    for i in range(len(a)):
        x = root(union_find_tree, a[i] - 1)
        y = root(union_find_tree, b[i] - 1)
        if x != y:
            union_find_tree[x] = min(x, y)
            union_find_tree[y] = union_find_tree[x]

    return union_find_tree


# def make_union_find(a, b, n):
#     x = [i for i in range(n)]

#     for i in range(len(a)):
#         root = a[i] - 1
#         leaf = b[i] - 1
#         x[leaf] = root
#     return x


def root(union_find_tree, target):
    if union_find_tree[target] == target:
        return target
    else:
        union_find_tree[target] = root(
            union_find_tree, union_find_tree[target])
        return union_find_tree[target]


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
input = sys.stdin.readline


def main():
    n, k, l = map(int, input().split())
    p, q = [], []
    for _ in range(k):
        pi, qi = map(int, input().split())
        p.append(pi)
        q.append(qi)
    r, s = [], []
    for _ in range(l):
        ri, si = map(int, input().split())
        r.append(ri)
        s.append(si)

    road_tree = make_union_find(p, q, n)
    rail_tree = make_union_find(r, s, n)

    a, b = [], []
    x = defaultdict(int)
    for i in range(n):
        a.append(root(road_tree, i))
        b.append(root(rail_tree, i))
        key = (a[i], b[i])
        x[key] += 1

    for i in range(n):
        print(x[(a[i], b[i])], end=' ')


def make_union_find(a, b, n):
    x = [i for i in range(n)]

    for i in range(len(a)):
        root = a[i] - 1
        leaf = b[i] - 1
        x[leaf] = root
    return x


def root(union_find_tree, target):
    if union_find_tree[target] == target:
        return target
    else:
        union_find_tree[target] = root(
            union_find_tree, union_find_tree[target])
        return union_find_tree[target]


def same(union_find_tree, x, y):
    return root(union_find_tree, x) == root(union_find_tree, y)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
input = sys.stdin.readline


def main():
    n, k, l = map(int, input().split())
    p, q = [], []
    for _ in range(k):
        pi, qi = map(int, input().split())
        p.append(pi)
        q.append(qi)
    r, s = [], []
    for _ in range(l):
        ri, si = map(int, input().split())
        r.append(ri)
        s.append(si)

    road_tree = make_union_find(p, q, n)
    rail_tree = make_union_find(r, s, n)

    a = []
    for i in range(n):
        a.append(root(road_tree, i))
    b = []
    for i in range(n):
        b.append(root(rail_tree, i))

    # default dict(collection)
    x = defaultdict(int)
    for i in range(n):
        key = (a[i], b[i])
        x[key] += 1

    for i in range(n):
        print(x[(a[i], b[i])], end=' ')


def make_union_find(a, b, n):
    x = [i for i in range(n)]

    for i in range(len(a)):
        root = a[i] - 1
        leaf = b[i] - 1
        x[leaf] = root
    return x


def root(union_find_tree, target):
    if union_find_tree[target] == target:
        return target
    else:
        union_find_tree[target] = root(
            union_find_tree, union_find_tree[target])
        return union_find_tree[target]


def same(union_find_tree, x, y):
    return root(union_find_tree, x) == root(union_find_tree, y)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import fractions
import sys
input = sys.stdin.readline


def main():
    n, k, l = map(int, input().split())
    p, q = [], []
    for _ in range(k):
        pi, qi = map(int, input().split())
        p.append(pi)
        q.append(qi)
    r, s = [], []
    for _ in range(l):
        ri, si = map(int, input().split())
        r.append(ri)
        s.append(si)

    road_tree = make_union_find(p, q, n)
    rail_tree = make_union_find(r, s, n)

    a = []
    for i in range(n):
        a.append(root(road_tree, i))
    b = []
    for i in range(n):
        b.append(root(rail_tree, i))

    # default dict(collection)
    x = {}
    for i in range(n):
        key = (a[i], b[i])
        if key in x:
            x[key].append(i)
        else:
            x[key] = [i]

    result = [0] * n
    for i in x.values():
        for j in i:
            result[j] = len(i)

    print(*result)


def make_union_find(a, b, n):
    x = [i for i in range(n)]

    for i in range(len(a)):
        root = a[i] - 1
        leaf = b[i] - 1
        x[leaf] = root
    return x


def root(union_find_tree, target):
    if union_find_tree[target] == target:
        return target
    else:
        union_find_tree[target] = root(
            union_find_tree, union_find_tree[target])
        return union_find_tree[target]


def same(union_find_tree, x, y):
    return root(union_find_tree, x) == root(union_find_tree, y)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import fractions
import sys
input = sys.stdin.readline


def main():
    n, k, l = map(int, input().split())
    p, q = [], []
    for _ in range(k):
        pi, qi = map(int, input().split())
        p.append(pi)
        q.append(qi)
    r, s = [], []
    for _ in range(l):
        ri, si = map(int, input().split())
        r.append(ri)
        s.append(si)

    road_tree = make_union_find(p, q, n)
    rail_tree = make_union_find(r, s, n)

    a = []
    for i in range(n):
        a.append(root(road_tree, i))
    b = []
    for i in range(n):
        b.append(root(rail_tree, i))

    x = {}
    for i in range(n):
        key = (a[i], b[i])
        if key in x:
            x[key].append(i)
        else:
            x[key] = [i]

    result = [0] * n
    for i in x.values():
        for j in i:
            result[j] = len(i)

    print(*result)


def make_union_find(a, b, n):
    x = [i for i in range(n)]

    for i in range(len(a)):
        root = a[i] - 1
        leaf = b[i] - 1
        x[leaf] = root
    return x


def root(union_find_tree, target):
    if union_find_tree[target] == target:
        return target
    else:
        union_find_tree[target] = root(
            union_find_tree, union_find_tree[target])
        return union_find_tree[target]


def same(union_find_tree, x, y):
    return root(union_find_tree, x) == root(union_find_tree, y)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import fractions
import sys
input = sys.stdin.readline


def main():
    n, k, l = map(int, input().split())
    p, q = [], []
    for _ in range(k):
        pi, qi = map(int, input().split())
        p.append(pi)
        q.append(qi)
    r, s = [], []
    for _ in range(l):
        ri, si = map(int, input().split())
        r.append(ri)
        s.append(si)

    road_tree = make_union_find(p, q, n)
    rail_tree = make_union_find(r, s, n)

    a = []
    for i in range(n):
        a.append(root(road_tree, i))
    b = []
    for i in range(n):
        b.append(root(rail_tree, i))

    x = {}
    for i in range(n):
        key = (a[i], b[i])
        if key in x:
            x[key].append(i)
        else:
            x[key] = [i]

    result = [0] * n
    for i in x.values():
        if len(i) > 1:
            for j in i:
                result[j] = len(i)
        else:
            for j in i:
                result[j] = 1

    print(*result)


def make_union_find(a, b, n):
    x = [i for i in range(n)]

    for i in range(len(a)):
        root = a[i] - 1
        leaf = b[i] - 1
        x[leaf] = root
    return x


def root(union_find_tree, target):
    if union_find_tree[target] == target:
        return target
    else:
        union_find_tree[target] = root(
            union_find_tree, union_find_tree[target])
        return union_find_tree[target]


def same(union_find_tree, x, y):
    return root(union_find_tree, x) == root(union_find_tree, y)


if __name__ == '__main__':
    main()