結果

問題 No.1779 Magical Swap
ユーザー NoneNone
提出日時 2022-01-26 06:00:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 374 ms / 2,000 ms
コード長 2,832 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 104,824 KB
最終ジャッジ日時 2023-08-23 07:16:02
合計ジャッジ時間 6,563 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,268 KB
testcase_01 AC 68 ms
71,168 KB
testcase_02 AC 152 ms
79,020 KB
testcase_03 AC 111 ms
77,784 KB
testcase_04 AC 293 ms
93,236 KB
testcase_05 AC 173 ms
85,784 KB
testcase_06 AC 190 ms
86,960 KB
testcase_07 AC 261 ms
95,088 KB
testcase_08 AC 93 ms
77,468 KB
testcase_09 AC 163 ms
81,040 KB
testcase_10 AC 333 ms
103,464 KB
testcase_11 AC 221 ms
88,284 KB
testcase_12 AC 144 ms
78,472 KB
testcase_13 AC 277 ms
96,708 KB
testcase_14 AC 374 ms
104,824 KB
testcase_15 AC 245 ms
80,132 KB
testcase_16 AC 324 ms
96,632 KB
testcase_17 AC 252 ms
78,944 KB
testcase_18 AC 68 ms
71,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def find(self, x):
        """ 根を見つける関数を定義(同時にxを直接根にくっつける操作も行う)"""
        tmp = []
        parents = self.parents
        while parents[x] >= 0:
            tmp.append(x)
            x = parents[x]
        for y in tmp:
            parents[y] = x
        return 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 same(self, x, y):
        """ xとyが同じ根の子かを判定 """
        return self.find(x) == self.find(y)

    def size(self, x):
        """ xの根のparent(= 要素数)を返す """
        return -self.parents[self.find(x)]

    def members(self, x):
        """ xが属するグループの要素をリストとして返す O(N)"""
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        """ 全ての根の要素をリストとして返す O(N)"""
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ グループの数を返す O(N)"""
        return len(self.roots())

    def size_list(self):
        """ 各グループの要素数のリストを返す(根の番号は返さない) O(N)"""
        return [-x for x in self.parents if x < 0]

    def all_group_members(self):
        """ {根:[根の子(根を含む)のリスト],...}を辞書で返す O(N)"""
        res = [[] for _ in range(self.n)]
        for i in range(self.n):
            x = self.find(i)
            res[x].append(i)
        return {r: res[r] for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

##############################################################################################################
import sys
input = sys.stdin.readline

T=int(input())
for _ in range(T):
    N=int(input())
    A=list(map(int, input().split()))
    B=list(map(int, input().split()))
    U = UnionFind(N)
    for k in range(2,N+1):
        for i in range(1,N+1):
            if (i+1)*k>N:
                break
            U.union(i*k-1,(i+1)*k-1)
    for _,p in U.all_group_members().items():
        AA=[]
        BB=[]
        for i in p:
            AA.append(A[i])
            BB.append(B[i])
        AA.sort()
        BB.sort()
        if AA!=BB:
            print("No")
            break
    else:
        print("Yes")
0