結果

問題 No.778 クリスマスツリー
ユーザー burita083burita083
提出日時 2021-07-17 12:36:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,709 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 11,028 KB
実行使用メモリ 68,528 KB
最終ジャッジ日時 2023-09-21 05:52:09
合計ジャッジ時間 4,867 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,716 KB
testcase_01 AC 19 ms
8,712 KB
testcase_02 AC 18 ms
8,580 KB
testcase_03 AC 19 ms
8,592 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())

A = list(map(int, input().split()))

from collections import deque
graph = [[] for _ in range(N)]

for idx, a in enumerate(A):
  graph[idx+1].append(a)
  graph[a].append(idx+1)


def EulerTour(n, X, i0):
    done = [0] * n
    Q = [~i0, i0] # 根をスタックに追加
    ET = []
    while Q:
        i = Q.pop()
        if i >= 0: # 行きがけの処理
            done[i] = 1
            ET.append(i)
            for a in X[i][::-1]:
                if done[a]: continue
                Q.append(~a) # 帰りがけの処理をスタックに追加
                Q.append(a) # 行きがけの処理をスタックに追加

        else: # 帰りがけの処理
            ET.append(~i)

    return ET


dist = [-1] * N #Visitedとして使う
def EulerTourWithOutPost(n, X, i0, dist):
    done = [0] * n
    Q = [~i0, i0] # 根をスタックに追加
    ET = []
    dist[0] = 0 #スタートはゼロと仮定しているがこことqueueの初期値を変える
    while Q:
        i = Q.pop()
        if i >= 0: # 行きがけの処理
            done[i] = 1
            ET.append(i)
            for a in X[i][::-1]:
                if done[a]: continue
                dist[a] = dist[i] + 1
                Q.append(~a) # 帰りがけの処理をスタックに追加
                Q.append(a) # 行きがけの処理をスタックに追加

        else: # 帰りがけの処理
            pass # ET.append(~i) # ←これは使わないなら外してOK

    return ET

route = EulerTourWithOutPost(N, graph, 0, dist)

ans = 0
for i in range(len(route)-1):
  for j in range(i+1, len(route)):
    if route[i] < route[j] and dist[i] < dist[j]:
      ans += 1

print(ans)
0