結果

問題 No.74 貯金箱の退屈
ユーザー H3PO4
提出日時 2020-04-24 13:58:15
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 984 ms / 5,000 ms
コード長 724 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 57,032 KB
最終ジャッジ日時 2024-10-15 01:50:54
合計ジャッジ時間 35,803 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix

N = int(input())
D = tuple(map(int, input().split()))
W = tuple(map(int, input().split()))

same_set = set()
edge = []
for i, d in enumerate(D):
    if (p := (i - d) % N) == (q := (i + d) % N):
        same_set.add(p)
    else:
        edge.append((p, q))
edge = np.array(edge).T

matr = csr_matrix((np.ones(len(edge[0]), dtype="int64"), edge), (N, N))
p, labels = connected_components(matr)

lst = [set() for _ in range(p)]
for i, x in enumerate(labels):
    lst[x].add(i)
for st in lst:
    if st.isdisjoint(same_set) and sum((1 - W[i]) for i in st) % 2:
        print('No')
        break
else:
    print('Yes')
0