結果

問題 No.74 貯金箱の退屈
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-14 22:12:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 1,418 bytes
コンパイル時間 110 ms
コンパイル使用メモリ 11,912 KB
実行使用メモリ 10,248 KB
最終ジャッジ日時 2023-10-20 05:14:42
合計ジャッジ時間 2,691 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,244 KB
testcase_01 AC 33 ms
10,244 KB
testcase_02 AC 32 ms
10,244 KB
testcase_03 AC 31 ms
10,244 KB
testcase_04 AC 31 ms
10,244 KB
testcase_05 AC 29 ms
10,244 KB
testcase_06 AC 29 ms
10,248 KB
testcase_07 AC 30 ms
10,244 KB
testcase_08 AC 30 ms
10,244 KB
testcase_09 AC 31 ms
10,244 KB
testcase_10 AC 30 ms
10,244 KB
testcase_11 AC 31 ms
10,244 KB
testcase_12 AC 30 ms
10,244 KB
testcase_13 AC 31 ms
10,248 KB
testcase_14 AC 29 ms
10,248 KB
testcase_15 AC 30 ms
10,248 KB
testcase_16 AC 30 ms
10,248 KB
testcase_17 AC 30 ms
10,244 KB
testcase_18 AC 30 ms
10,244 KB
testcase_19 AC 29 ms
10,248 KB
testcase_20 AC 29 ms
10,248 KB
testcase_21 AC 29 ms
10,248 KB
testcase_22 AC 29 ms
10,244 KB
testcase_23 AC 29 ms
10,244 KB
testcase_24 AC 29 ms
10,244 KB
testcase_25 AC 31 ms
10,244 KB
testcase_26 AC 30 ms
10,248 KB
testcase_27 AC 30 ms
10,248 KB
testcase_28 AC 29 ms
10,248 KB
testcase_29 AC 30 ms
10,248 KB
testcase_30 AC 29 ms
10,248 KB
testcase_31 AC 29 ms
10,248 KB
testcase_32 AC 30 ms
10,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def size(self, x):
        return -self.root[self.find(x)]


N = int(input())
D = list(map(int, input().split()))
W = list(map(int, input().split()))
slf = [0] * N
uf = UF_tree(N)

for i, d in enumerate(D):
    x = (i + d) % N
    y = (i - d % N + N) % N
    if x == y:
        slf[x] = 1
    else:
        uf.unite(x, y)

leader = set(uf.find(i) for i in range(N))
group = {i: [] for i in leader}
for i in range(N):
    group[uf.find(i)].append(i)

ok = True
for g in group.values():
    cnt = sum(1 - W[i] for i in g)
    if cnt % 2 == 0:
        continue
    if sum(slf[i] for i in g):
        continue
    ok = False

if ok:
    print("Yes")
else:
    print("No")
0