結果

問題 No.1333 Squared Sum
ユーザー toyuzukotoyuzuko
提出日時 2021-04-17 23:48:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,198 ms / 2,000 ms
コード長 2,361 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 82,368 KB
実行使用メモリ 205,004 KB
最終ジャッジ日時 2024-07-04 04:20:46
合計ジャッジ時間 30,772 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,952 KB
testcase_01 AC 37 ms
52,472 KB
testcase_02 AC 36 ms
53,340 KB
testcase_03 AC 1,146 ms
197,268 KB
testcase_04 AC 1,155 ms
198,584 KB
testcase_05 AC 1,132 ms
193,676 KB
testcase_06 AC 1,154 ms
199,184 KB
testcase_07 AC 1,169 ms
198,640 KB
testcase_08 AC 1,145 ms
196,492 KB
testcase_09 AC 1,145 ms
197,840 KB
testcase_10 AC 1,120 ms
194,336 KB
testcase_11 AC 1,096 ms
199,360 KB
testcase_12 AC 1,100 ms
197,456 KB
testcase_13 AC 763 ms
186,000 KB
testcase_14 AC 1,147 ms
190,556 KB
testcase_15 AC 1,121 ms
189,676 KB
testcase_16 AC 35 ms
53,284 KB
testcase_17 AC 37 ms
52,744 KB
testcase_18 AC 36 ms
52,748 KB
testcase_19 AC 36 ms
52,468 KB
testcase_20 AC 36 ms
53,840 KB
testcase_21 AC 37 ms
53,344 KB
testcase_22 AC 35 ms
53,408 KB
testcase_23 AC 35 ms
54,156 KB
testcase_24 AC 36 ms
54,112 KB
testcase_25 AC 33 ms
53,848 KB
testcase_26 AC 1,148 ms
188,996 KB
testcase_27 AC 1,163 ms
188,320 KB
testcase_28 AC 1,198 ms
189,540 KB
testcase_29 AC 772 ms
189,184 KB
testcase_30 AC 486 ms
124,176 KB
testcase_31 AC 303 ms
101,748 KB
testcase_32 AC 687 ms
149,356 KB
testcase_33 AC 552 ms
133,744 KB
testcase_34 AC 906 ms
172,996 KB
testcase_35 AC 724 ms
148,772 KB
testcase_36 AC 455 ms
116,724 KB
testcase_37 AC 454 ms
118,688 KB
testcase_38 AC 502 ms
125,708 KB
testcase_39 AC 791 ms
157,604 KB
testcase_40 AC 884 ms
204,872 KB
testcase_41 AC 834 ms
205,004 KB
testcase_42 AC 868 ms
204,968 KB
testcase_43 AC 847 ms
204,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Tree():
    def __init__(self, n):
        self.n = n
        self.tree = [[] for _ in range(n)]
        self.root = None

    def add_edge(self, u, v, c):
        if u > v: u, v = v, u
        self.tree[u].append((v, c))
        self.tree[v].append((u, c))

    def set_root(self, r=0):
        self.root = r
        self.par = [None] * self.n
        self.ord = [r]
        self.cost = [0] * N
        stack = [r]
        while stack:
            v = stack.pop()
            for adj, cost in self.tree[v]:
                if self.par[v] == adj: continue
                self.cost[adj] = cost
                self.par[adj] = v
                self.ord.append(adj)
                stack.append(adj)

    def rerooting(self, op, e, merge, id):
        if self.root is None: self.set_root()
        dp = [e] * self.n
        lt = [id] * self.n
        rt = [id] * self.n
        inv = [id] * self.n
        for v in self.ord[::-1]:
            tl = tr = e
            for adj, cost in self.tree[v]:
                if self.par[v] == adj: continue
                lt[adj] = tl
                tl = op(tl, dp[adj], cost)
            for adj, cost in self.tree[v][::-1]:
                if self.par[v] == adj: continue
                rt[adj] = tr
                tr = op(tr, dp[adj], cost)
            dp[v] = tr
        for v in self.ord:
            if v == self.root: continue
            p = self.par[v]
            cost = self.cost[p]
            inv[v] = op(merge(lt[v], rt[v]), inv[p], cost)
            cost = self.cost[v]
            dp[v] = op(dp[v], inv[v], cost)
        return dp

import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

MOD = 1000000007

N = int(input())

t = Tree(N)

for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1; v -= 1
    t.add_edge(u, v, w)

e = (1, 0, 0)

def op(p, c, w):
    ps, pd, pds = p
    cs, cd, cds = c
    size = ps + cs
    dist = (pd + cd + w * cs) % MOD
    dsq = (pds + cds + w**2 * cs + 2 * w * cd) % MOD
    return size, dist, dsq

id = (0, 0, 0)

def merge(lt, rt):
    ls, ld, lds = lt
    rs, rd, rds = rt
    size = ls + rs - 1
    dist = (ld + rd) % MOD
    dsq = (lds + rds) % MOD
    return size, dist, dsq

dp = t.rerooting(op, e, merge, id)

res = 0

for size, dist, dsq in dp:
    res += dsq
    res %= MOD

print(res * pow(2, MOD - 2, MOD) % MOD)
0