結果

問題 No.778 クリスマスツリー
ユーザー lam6er
提出日時 2025-03-20 19:03:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 738 bytes
コンパイル時間 447 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 124,632 KB
最終ジャッジ日時 2025-03-20 19:04:35
合計ジャッジ時間 3,431 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    if N == 1:
        print(0)
        return
    A = list(map(int, input[ptr:ptr + N-1]))
    
    children = [[] for _ in range(N)]
    for j in range(1, N):
        parent = A[j-1]
        children[parent].append(j)
    
    total = 0
    count = [0] * N
    
    q = deque()
    q.append(0)
    while q:
        u = q.popleft()
        for v in children[u]:
            parent = A[v-1]
            count_v = count[parent] + (1 if parent < v else 0)
            count[v] = count_v
            total += count_v
            q.append(v)
    
    print(total)

if __name__ == "__main__":
    main()
0