結果

問題 No.74 貯金箱の退屈
ユーザー maspy
提出日時 2020-03-07 07:31:41
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 1,060 bytes
コンパイル時間 112 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-10-14 11:34:27
合計ジャッジ時間 2,365 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
N = int(readline())
D = list(map(int, readline().split()))
W = list(map(int, readline().split()))

# %%
graph = [[] for _ in range(N)]
free = set()
for i, x in enumerate(D):
    j = (i + x) % N
    k = (i - x) % N
    if j == k:
        free.add(j)
    graph[j].append(k)
    graph[k].append(j)


# %%
comp = [0] * N
n = 0
for v in range(N):
    if comp[v]:
        continue
    n += 1
    comp[v] = n
    stack = [v]
    while stack:
        v = stack.pop()
        for w in graph[v]:
            if comp[w]:
                continue
            comp[w] = n
            stack.append(w)

# %%
comp_ind = [[] for _ in range(max(comp) + 1)]
for i, x in enumerate(comp):
    comp_ind[x].append(i)


# %%
answer = 'Yes'
for ind in comp_ind:
    is_free = bool(set(ind) & free)
    if is_free:
        continue
    ura = sum(1 - W[i] for i in ind)
    if ura & 1:
        answer = 'No'
        break
print(answer)
0