結果

問題 No.1779 Magical Swap
ユーザー brthyyjpbrthyyjp
提出日時 2021-12-26 15:32:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 1,576 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 81,772 KB
実行使用メモリ 109,700 KB
最終ジャッジ日時 2023-10-24 07:31:05
合計ジャッジ時間 4,858 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,596 KB
testcase_01 AC 45 ms
55,596 KB
testcase_02 AC 139 ms
77,208 KB
testcase_03 AC 92 ms
76,400 KB
testcase_04 AC 142 ms
104,796 KB
testcase_05 AC 108 ms
85,948 KB
testcase_06 AC 110 ms
87,908 KB
testcase_07 AC 135 ms
100,736 KB
testcase_08 AC 68 ms
70,744 KB
testcase_09 AC 111 ms
80,516 KB
testcase_10 AC 187 ms
105,364 KB
testcase_11 AC 139 ms
89,916 KB
testcase_12 AC 101 ms
78,092 KB
testcase_13 AC 160 ms
98,080 KB
testcase_14 AC 192 ms
106,492 KB
testcase_15 AC 204 ms
78,220 KB
testcase_16 AC 151 ms
109,700 KB
testcase_17 AC 170 ms
80,276 KB
testcase_18 AC 43 ms
55,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def Find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.Find(self.par[x])
            return self.par[x]

    def Unite(self, x, y):
        x = self.Find(x)
        y = self.Find(y)

        if x != y:
            if self.rank[x] < self.rank[y]:
                self.par[y] += self.par[x]
                self.par[x] = y
            else:
                self.par[x] += self.par[y]
                self.par[y] = x
                if self.rank[x] == self.rank[y]:
                    self.rank[x] += 1

    def Same(self, x, y):
        return self.Find(x) == self.Find(y)

    def Size(self, x):
        return -self.par[self.Find(x)]

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

from collections import defaultdict

t = int(input())
for _ in range(t):
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    uf = UnionFind(n)
    for k in range(2, n+1):
        j = 2*k
        while j < n+1:
            uf.Unite(k-1, j-1)
            j += k
    DA = defaultdict(lambda: [])
    for i, a in enumerate(A):
        DA[uf.Find(i)].append(a)
    DB = defaultdict(lambda: [])
    for i, b in enumerate(B):
        DB[uf.Find(i)].append(b)
    flag = True
    for k in DA.keys():
        if sorted(DA[k]) != sorted(DB[k]):
            flag = False
            break
    if flag:
        print('Yes')
    else:
        print('No')
0