結果
| 問題 |
No.2638 Initial fare
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2024-03-22 01:21:54 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 672 ms / 2,000 ms |
| コード長 | 1,050 bytes |
| コンパイル時間 | 491 ms |
| コンパイル使用メモリ | 81,920 KB |
| 実行使用メモリ | 161,400 KB |
| 最終ジャッジ日時 | 2024-09-30 10:27:00 |
| 合計ジャッジ時間 | 11,290 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
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)
titia