結果
| 問題 | No.872 All Tree Path |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-12 22:02:04 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 446 ms / 3,000 ms |
| コード長 | 601 bytes |
| 記録 | |
| コンパイル時間 | 218 ms |
| コンパイル使用メモリ | 85,620 KB |
| 実行使用メモリ | 137,140 KB |
| 最終ジャッジ日時 | 2026-05-12 22:02:12 |
| 合計ジャッジ時間 | 6,223 ms |
|
ジャッジサーバーID (参考情報) |
judge3_1 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
from collections import deque
n=int(input())
v=[[] for i in range(n)]
for i in range(n-1):
a,b,c=map(int,input().split())
a-=1;b-=1
v[a].append((b,c));v[b].append((a,c))
f=deque([(0,0,0)])
dp=[(0,0)]*n;ans=0
while f:
q,w,e=f.pop()
if len(v[q])>e:
f.append((q,w,e+1))
if w!=v[q][e][0]:
f.append((v[q][e][0],q,0))
else:
a,b=0,1
for i,j in v[q]:
if i==w:
continue
ans+=a*dp[i][1]+dp[i][0]*b+j*dp[i][1]*b
a+=dp[i][0]+j*dp[i][1]
b+=dp[i][1]
dp[q]=(a,b)
print(ans*2)