結果

問題 No.2638 Initial fare
ユーザー titiatitia
提出日時 2024-03-22 01:21:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 707 ms / 2,000 ms
コード長 1,050 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 161,112 KB
最終ジャッジ日時 2024-03-22 01:22:07
合計ジャッジ時間 11,900 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 38 ms
53,460 KB
testcase_04 AC 578 ms
133,748 KB
testcase_05 AC 625 ms
138,136 KB
testcase_06 AC 37 ms
53,460 KB
testcase_07 AC 523 ms
141,672 KB
testcase_08 AC 357 ms
142,924 KB
testcase_09 AC 423 ms
159,620 KB
testcase_10 AC 379 ms
161,112 KB
testcase_11 AC 37 ms
53,460 KB
testcase_12 AC 543 ms
139,032 KB
testcase_13 AC 345 ms
129,956 KB
testcase_14 AC 410 ms
153,408 KB
testcase_15 AC 384 ms
155,792 KB
testcase_16 AC 37 ms
53,460 KB
testcase_17 AC 535 ms
141,528 KB
testcase_18 AC 388 ms
140,016 KB
testcase_19 AC 466 ms
131,844 KB
testcase_20 AC 512 ms
133,244 KB
testcase_21 AC 510 ms
132,604 KB
testcase_22 AC 39 ms
53,460 KB
testcase_23 AC 600 ms
143,136 KB
testcase_24 AC 707 ms
143,692 KB
testcase_25 AC 37 ms
53,460 KB
testcase_26 AC 646 ms
141,824 KB
testcase_27 AC 706 ms
143,196 KB
権限があれば一括ダウンロードができます

ソースコード

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

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

print(ANS)
0