結果
問題 | No.1779 Magical Swap |
ユーザー | Shirotsume |
提出日時 | 2021-12-08 00:21:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 205 ms / 2,000 ms |
コード長 | 2,541 bytes |
コンパイル時間 | 276 ms |
コンパイル使用メモリ | 82,272 KB |
実行使用メモリ | 100,480 KB |
最終ジャッジ日時 | 2024-07-16 07:38:14 |
合計ジャッジ時間 | 3,301 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
54,144 KB |
testcase_01 | AC | 46 ms
54,656 KB |
testcase_02 | AC | 145 ms
77,568 KB |
testcase_03 | AC | 86 ms
76,928 KB |
testcase_04 | AC | 89 ms
93,952 KB |
testcase_05 | AC | 62 ms
78,592 KB |
testcase_06 | AC | 66 ms
81,152 KB |
testcase_07 | AC | 88 ms
95,616 KB |
testcase_08 | AC | 47 ms
60,544 KB |
testcase_09 | AC | 114 ms
80,128 KB |
testcase_10 | AC | 192 ms
98,036 KB |
testcase_11 | AC | 142 ms
88,832 KB |
testcase_12 | AC | 110 ms
78,076 KB |
testcase_13 | AC | 176 ms
95,460 KB |
testcase_14 | AC | 205 ms
100,480 KB |
testcase_15 | AC | 154 ms
77,620 KB |
testcase_16 | AC | 95 ms
94,336 KB |
testcase_17 | AC | 166 ms
77,236 KB |
testcase_18 | AC | 44 ms
54,016 KB |
ソースコード
import sys from collections import Counter input = lambda: sys.stdin.readline().rstrip() INF = 10 ** 19 from math import gcd class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) x=self.leader(a) y=self.leader(b) if x==y: return x if (-self.parent_or_size[x]<-self.parent_or_size[y]): x,y=y,x self.parent_or_size[x]+=self.parent_or_size[y] self.parent_or_size[y]=x return x def same(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) return self.leader(a)==self.leader(b) def leader(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) if (self.parent_or_size[a]<0): return a self.parent_or_size[a]=self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf=[0 for i in range(self.n)] group_size=[0 for i in range(self.n)] for i in range(self.n): leader_buf[i]=self.leader(i) group_size[leader_buf[i]]+=1 result=[[] for i in range(self.n)] for i in range(self.n): result[leader_buf[i]].append(i) result2=[] for i in range(self.n): if len(result[i])>0: result2.append(result[i]) return result2 def solve(): n = int(input()) a = [0] + list(map(int,input().split())) b = [0] + list(map(int,input().split())) if a[1] != b[1]: return 'No' now = 1 ans = 'Yes' U = dsu(n + 1) for i in range(2,n + 1): for j in range(i, n + 1, i): U.merge(i, j) a_now = [[] for _ in range(n + 1)] b_now = [[] for _ in range(n + 1)] for i in range(2, n + 1): a_now[U.leader(i)].append(a[i]) b_now[U.leader(i)].append(b[i]) for i in range(n): a_now[i].sort() b_now[i].sort() if a_now[i] != b_now[i]: ans = 'No' break return ans t = int(input()) for _ in range(t): print(solve())