結果

問題 No.1779 Magical Swap
ユーザー roarisroaris
提出日時 2021-12-08 04:57:26
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,527 bytes
コンパイル時間 636 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 107,636 KB
最終ジャッジ日時 2023-09-22 18:20:42
合計ジャッジ時間 8,038 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,676 KB
testcase_01 AC 93 ms
71,548 KB
testcase_02 AC 233 ms
79,184 KB
testcase_03 AC 144 ms
78,804 KB
testcase_04 AC 325 ms
103,072 KB
testcase_05 AC 206 ms
87,208 KB
testcase_06 AC 216 ms
91,364 KB
testcase_07 AC 292 ms
99,128 KB
testcase_08 AC 121 ms
78,504 KB
testcase_09 AC 676 ms
84,880 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

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!=db:
            print('No')
            break
    else:
        print('Yes')
0