結果

問題 No.74 貯金箱の退屈
ユーザー rpy3cpprpy3cpp
提出日時 2015-07-27 21:31:05
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,482 bytes
コンパイル時間 72 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 8,456 KB
最終ジャッジ日時 2023-09-23 04:04:17
合計ジャッジ時間 2,159 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,332 KB
testcase_01 AC 17 ms
8,196 KB
testcase_02 AC 17 ms
8,272 KB
testcase_03 AC 17 ms
8,320 KB
testcase_04 AC 17 ms
8,228 KB
testcase_05 AC 17 ms
8,448 KB
testcase_06 AC 17 ms
8,332 KB
testcase_07 AC 17 ms
8,228 KB
testcase_08 AC 17 ms
8,228 KB
testcase_09 AC 17 ms
8,256 KB
testcase_10 AC 16 ms
8,048 KB
testcase_11 AC 16 ms
8,260 KB
testcase_12 AC 17 ms
8,264 KB
testcase_13 AC 16 ms
8,452 KB
testcase_14 AC 16 ms
8,388 KB
testcase_15 AC 17 ms
8,344 KB
testcase_16 AC 17 ms
8,228 KB
testcase_17 AC 17 ms
8,256 KB
testcase_18 AC 17 ms
8,228 KB
testcase_19 AC 17 ms
8,456 KB
testcase_20 AC 17 ms
8,232 KB
testcase_21 AC 17 ms
8,396 KB
testcase_22 AC 17 ms
8,268 KB
testcase_23 AC 17 ms
8,356 KB
testcase_24 AC 16 ms
8,408 KB
testcase_25 AC 17 ms
8,312 KB
testcase_26 AC 17 ms
8,304 KB
testcase_27 AC 16 ms
8,400 KB
testcase_28 AC 16 ms
8,340 KB
testcase_29 AC 16 ms
8,456 KB
testcase_30 AC 16 ms
8,296 KB
testcase_31 AC 16 ms
8,052 KB
testcase_32 AC 16 ms
8,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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')
0