結果
問題 | No.74 貯金箱の退屈 |
ユーザー |
![]() |
提出日時 | 2020-12-06 22:17:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 36 ms / 5,000 ms |
コード長 | 1,307 bytes |
コンパイル時間 | 170 ms |
コンパイル使用メモリ | 82,192 KB |
実行使用メモリ | 53,804 KB |
最終ジャッジ日時 | 2024-09-17 13:18:50 |
合計ジャッジ時間 | 2,341 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
class UnionFind:def __init__(self, n):self.parent = list(range(n)) #親ノードself.size = [1]*n #グループの要素数def root(self, x): #root(x): xの根ノードを返す.while self.parent[x] != x:self.parent[x] = self.parent[self.parent[x]]x = self.parent[x]return xdef merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめるx, y = self.root(x), self.root(y)if x == y: return Falseif self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいようにself.size[x] += self.size[y] #xの要素数を更新self.parent[y] = x #yをxにつなぐreturn Truedef issame(self, x, y): #same(x,y): xとyが同じ組ならTruereturn self.root(x) == self.root(y)def getsize(self,x): #size(x): xのいるグループの要素数を返すreturn self.size[self.root(x)]n = int(input())*d, = map(int,input().split())*w, = map(int,input().split())UF = UnionFind(n+1)for i,di in enumerate(d):x = (i+di)%ny = (i-di)%nUF.merge(x,n if x==y else y)ura = [0]*nfor i,wi in enumerate(w):if wi==0 and not UF.issame(i,n): ura[UF.root(i)] ^= 1print("Yes" if all(ri==0 for ri in ura) else "No")