結果

問題 No.1687 What the Heck?
ユーザー tktk_snsntktk_snsn
提出日時 2021-09-24 23:13:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,069 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 30,792 KB
最終ジャッジ日時 2023-09-18 22:18:41
合計ジャッジ時間 4,504 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,308 KB
testcase_01 AC 17 ms
8,304 KB
testcase_02 WA -
testcase_03 AC 16 ms
8,360 KB
testcase_04 AC 16 ms
8,356 KB
testcase_05 AC 17 ms
8,228 KB
testcase_06 AC 17 ms
8,352 KB
testcase_07 AC 149 ms
16,428 KB
testcase_08 AC 206 ms
19,796 KB
testcase_09 WA -
testcase_10 AC 98 ms
13,492 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 405 ms
29,996 KB
testcase_14 AC 399 ms
30,156 KB
testcase_15 AC 394 ms
30,100 KB
testcase_16 AC 401 ms
30,792 KB
testcase_17 AC 41 ms
10,124 KB
testcase_18 AC 418 ms
30,136 KB
testcase_19 AC 19 ms
8,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def find(self, x):
        stack = []
        while self.root[x] >= 0:
            stack.append(x)
            x = self.root[x]
        for i in stack:
            self.root[i] = x
        return x

    def same(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 False
        if x > y:
            x, y = y, x
        self.root[y] += self.root[x]
        self.root[x] = y
        return True

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


N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))
uf = UF_tree(N + 1)

ans = 0
for i in reversed(range(1, N + 1)):
    p = A[i-1]
    c = uf.find(p + 1)
    if c < N:
        ans += i
        uf.unite(c, c + 1)
    elif uf.find(p) == p:
        uf.unite(p, p + 1)
    else:
        c = uf.find(0)
        ans -= i
        uf.unite(c, c + 1)

print(ans)
0