結果

問題 No.2638 Initial fare
ユーザー titiatitia
提出日時 2024-02-20 04:44:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,070 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 88,576 KB
最終ジャッジ日時 2024-02-20 04:44:45
合計ジャッジ時間 30,123 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
9,984 KB
testcase_01 AC 26 ms
9,984 KB
testcase_02 AC 26 ms
9,984 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 28 ms
9,984 KB
testcase_07 WA -
testcase_08 AC 1,199 ms
79,016 KB
testcase_09 WA -
testcase_10 AC 1,386 ms
85,424 KB
testcase_11 AC 25 ms
9,984 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())
E=[[] for i in range(N)]

for i in range(N-1):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)
    
# 木のHL分解+LCA

ROOT=0

QUE=[ROOT] 
Parent=[-1]*N
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N)]
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)

Childs=[[0,0,0] for i in range(N)]

for x in TOP_SORT[::-1]:
    if Child[x]==[]:
        continue

    for c in Child[x]:
        Childs[x][0]+=1
        Childs[x][1]+=Childs[c][0]
        Childs[x][2]+=Childs[c][1]

ANS=0
for i in range(N):
    ANS+=Childs[i][0]+Childs[i][1]+Childs[i][2]

    SUM1=Childs[i][0]
    SUM2=Childs[i][1]

    for c in Child[i]:
        SUM1-=1
        ANS+=SUM1

        SUM2-=Childs[c][0]
        ANS+=SUM2

print(ANS)

    
0