結果

問題 No.1779 Magical Swap
ユーザー 👑 H20H20
提出日時 2021-12-08 01:03:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 590 ms / 2,000 ms
コード長 1,989 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 120,900 KB
最終ジャッジ日時 2023-09-23 07:48:22
合計ジャッジ時間 5,469 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,384 KB
testcase_01 AC 97 ms
71,344 KB
testcase_02 AC 282 ms
80,252 KB
testcase_03 AC 147 ms
78,464 KB
testcase_04 AC 231 ms
113,700 KB
testcase_05 AC 174 ms
89,824 KB
testcase_06 AC 173 ms
91,764 KB
testcase_07 AC 210 ms
106,384 KB
testcase_08 AC 122 ms
78,060 KB
testcase_09 AC 157 ms
84,112 KB
testcase_10 AC 249 ms
112,908 KB
testcase_11 AC 186 ms
95,496 KB
testcase_12 AC 154 ms
81,680 KB
testcase_13 AC 214 ms
106,576 KB
testcase_14 AC 244 ms
120,900 KB
testcase_15 AC 590 ms
79,548 KB
testcase_16 AC 209 ms
108,148 KB
testcase_17 AC 275 ms
80,160 KB
testcase_18 AC 96 ms
71,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict
from collections import Counter

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


T = int(input())
for _ in range(T):
    N = int(input())
    uf = UnionFind(N+1)
    A = list(map(int, input().split())) 
    B = list(map(int, input().split()))
    for x in range(2,N+1):
        for y in range(x*2,N+1,x):
            uf.union(x,y)
    GM = uf.all_group_members()
    ans = True
    for k,l in GM.items():
        CA = Counter()
        CB = Counter()
        for v in l:
            if v==0:
                continue
            CA[A[v-1]]+=1
            CB[B[v-1]]+=1
        ans = ans and CA==CB
    if ans:
        print('Yes')
    else:
        print('No')

0