結果

問題 No.1779 Magical Swap
ユーザー roarisroaris
提出日時 2021-12-08 04:59:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 396 ms / 2,000 ms
コード長 1,533 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 107,480 KB
最終ジャッジ日時 2023-09-23 07:49:10
合計ジャッジ時間 5,882 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,496 KB
testcase_01 AC 98 ms
71,436 KB
testcase_02 AC 193 ms
79,408 KB
testcase_03 AC 148 ms
78,648 KB
testcase_04 AC 350 ms
102,804 KB
testcase_05 AC 217 ms
87,316 KB
testcase_06 AC 230 ms
91,400 KB
testcase_07 AC 328 ms
99,100 KB
testcase_08 AC 127 ms
78,296 KB
testcase_09 AC 183 ms
83,908 KB
testcase_10 AC 378 ms
103,000 KB
testcase_11 AC 251 ms
90,024 KB
testcase_12 AC 168 ms
81,720 KB
testcase_13 AC 320 ms
96,724 KB
testcase_14 AC 396 ms
105,464 KB
testcase_15 AC 253 ms
79,348 KB
testcase_16 AC 370 ms
107,480 KB
testcase_17 AC 275 ms
78,944 KB
testcase_18 AC 94 ms
71,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

for _ in range(int(input())):
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    uf = Unionfind(N)
    
    for i in range(2, N+1):
        for j in range(2*i, N+1, i):
            uf.unite(i-1, j-1)
    
    da = defaultdict(set)
    db = defaultdict(set)
    
    for i in range(N):
        r = uf.root(i)
        da[r].add(A[i])
        db[r].add(B[i])
    
    for k in da:
        if da[k]!=db[k]:
            print('No')
            break
    else:
        print('Yes')
0