結果

問題 No.1779 Magical Swap
ユーザー lloyzlloyz
提出日時 2024-01-11 20:12:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 2,450 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 132,236 KB
最終ジャッジ日時 2024-01-11 20:12:33
合計ジャッジ時間 4,955 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,612 KB
testcase_01 AC 41 ms
55,612 KB
testcase_02 AC 151 ms
77,404 KB
testcase_03 AC 97 ms
76,680 KB
testcase_04 AC 189 ms
116,316 KB
testcase_05 AC 127 ms
89,968 KB
testcase_06 AC 131 ms
93,844 KB
testcase_07 AC 195 ms
109,156 KB
testcase_08 AC 78 ms
76,948 KB
testcase_09 AC 116 ms
86,480 KB
testcase_10 AC 221 ms
128,100 KB
testcase_11 AC 150 ms
98,032 KB
testcase_12 AC 100 ms
78,960 KB
testcase_13 AC 193 ms
116,380 KB
testcase_14 AC 242 ms
132,236 KB
testcase_15 AC 226 ms
77,840 KB
testcase_16 AC 147 ms
97,940 KB
testcase_17 AC 168 ms
77,296 KB
testcase_18 AC 40 ms
55,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
import sys
input = sys.stdin.readline

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(i * 2, n + 1, i):
            UF.union(i - 1, j - 1)
    DA = [defaultdict(int) for _ in range(n)]
    DB = [defaultdict(int) for _ in range(n)]
    for i in range(n):
        DA[UF.find(i)][A[i]] += 1
        DB[UF.find(i)][B[i]] += 1
    flag = True
    for i in range(n):
        if DA[i] != DB[i]:
            flag = False
            break
    if flag:
        print("Yes")
    else:
        print("No")
0