結果
問題 | No.1779 Magical Swap |
ユーザー |
![]() |
提出日時 | 2021-12-26 15:32:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 187 ms / 2,000 ms |
コード長 | 1,576 bytes |
コンパイル時間 | 140 ms |
コンパイル使用メモリ | 82,604 KB |
実行使用メモリ | 109,956 KB |
最終ジャッジ日時 | 2024-09-23 00:14:28 |
合計ジャッジ時間 | 4,167 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 18 |
ソースコード
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')