結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
terasa
|
| 提出日時 | 2022-06-01 22:45:07 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,167 ms / 3,000 ms |
| コード長 | 973 bytes |
| コンパイル時間 | 153 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 374,480 KB |
| 最終ジャッジ日時 | 2024-09-21 01:45:40 |
| 合計ジャッジ時間 | 10,029 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')
N = int(input())
adj = [{} for _ in range(N)]
for _ in range(N - 1):
u, v, w = map(int, input().split())
u -= 1
v -= 1
adj[u][v] = w
adj[v][u] = w
child = [None] * N
cost = [None] * N
cost[0] = 0
def dfs(v, p):
if v > 0:
cost[v] = cost[p] + adj[v][p]
acc = 1
for d in adj[v].keys():
if d == p:
continue
acc += dfs(d, v)
child[v] = acc
return child[v]
dfs(0, -1)
S = sum(cost)
ans = 0
def dfs2(v, p):
global ans, S
if v > 0:
S += adj[v][p] * (N - 2 * child[v])
ans += S
for d in adj[v].keys():
if d == p:
continue
dfs2(d, v)
if v > 0:
S -= adj[v][p] * (N - 2 * child[v])
dfs2(0, -1)
print(ans)
terasa