結果
| 問題 |
No.74 貯金箱の退屈
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-07-27 21:31:05 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 32 ms / 5,000 ms |
| コード長 | 1,482 bytes |
| コンパイル時間 | 101 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 11,008 KB |
| 最終ジャッジ日時 | 2024-07-16 04:14:36 |
| 合計ジャッジ時間 | 1,884 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
class DisjointSet(object):
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
self.num = n # number of disjoint sets
def union(self, x, y):
self._link(self.find_set(x), self.find_set(y))
def _link(self, x, y):
if x == y:
return
self.num -= 1
if self.rank[x] > self.rank[y]:
self.parent[y] = x
else:
self.parent[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def find_set(self, x):
xp = self.parent[x]
if xp != x:
self.parent[x] = self.find_set(xp)
return self.parent[x]
def read_data():
N = int(input())
Ds = list(map(int, input().split()))
Ws = list(map(int, input().split()))
return N, Ds, Ws
def can_make_all_heads(N, Ds, Ws):
djs = DisjointSet(N)
same = []
for i, d in enumerate(Ds):
a = (i + d) % N
b = (i - d) % N
djs.union(a, b)
if a == b:
same.append(a)
n_zeros = [0] * N
for i, w in enumerate(Ws):
root = djs.find_set(i)
n_zeros[root] += (1 - w)
for s in same:
root = djs.find_set(s)
n_zeros[root] = 0
for n_zero in n_zeros:
if n_zero & 1:
return False
return True
if __name__ == '__main__':
N, Ds, Ws = read_data()
if can_make_all_heads(N, Ds, Ws):
print('Yes')
else:
print('No')