結果

問題 No.1333 Squared Sum
ユーザー toyuzukotoyuzuko
提出日時 2021-04-18 00:17:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,315 ms / 2,000 ms
コード長 2,431 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 86,768 KB
実行使用メモリ 208,460 KB
最終ジャッジ日時 2023-09-17 07:27:44
合計ジャッジ時間 36,227 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
70,864 KB
testcase_01 AC 74 ms
70,692 KB
testcase_02 AC 75 ms
71,084 KB
testcase_03 AC 1,229 ms
194,780 KB
testcase_04 AC 1,284 ms
199,488 KB
testcase_05 AC 1,241 ms
200,272 KB
testcase_06 AC 1,277 ms
199,496 KB
testcase_07 AC 1,261 ms
198,756 KB
testcase_08 AC 1,266 ms
198,236 KB
testcase_09 AC 1,271 ms
199,128 KB
testcase_10 AC 1,312 ms
194,824 KB
testcase_11 AC 1,278 ms
199,016 KB
testcase_12 AC 1,287 ms
198,420 KB
testcase_13 AC 888 ms
190,608 KB
testcase_14 AC 1,295 ms
189,192 KB
testcase_15 AC 1,300 ms
188,528 KB
testcase_16 AC 73 ms
70,884 KB
testcase_17 AC 75 ms
71,040 KB
testcase_18 AC 76 ms
71,096 KB
testcase_19 AC 74 ms
70,712 KB
testcase_20 AC 73 ms
70,852 KB
testcase_21 AC 72 ms
71,008 KB
testcase_22 AC 73 ms
70,880 KB
testcase_23 AC 75 ms
70,864 KB
testcase_24 AC 73 ms
71,012 KB
testcase_25 AC 72 ms
71,196 KB
testcase_26 AC 1,283 ms
187,500 KB
testcase_27 AC 1,310 ms
188,228 KB
testcase_28 AC 1,315 ms
188,744 KB
testcase_29 AC 876 ms
188,024 KB
testcase_30 AC 572 ms
125,900 KB
testcase_31 AC 392 ms
105,044 KB
testcase_32 AC 785 ms
147,884 KB
testcase_33 AC 656 ms
133,336 KB
testcase_34 AC 1,019 ms
170,928 KB
testcase_35 AC 800 ms
148,096 KB
testcase_36 AC 534 ms
118,824 KB
testcase_37 AC 535 ms
120,864 KB
testcase_38 AC 597 ms
127,440 KB
testcase_39 AC 899 ms
158,216 KB
testcase_40 AC 1,045 ms
208,244 KB
testcase_41 AC 1,069 ms
207,096 KB
testcase_42 AC 962 ms
208,460 KB
testcase_43 AC 1,037 ms
206,924 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]
            inv[v] = op(merge(lt[v], rt[v]), inv[p], self.cost[p])
            dp[v] = op(dp[v], inv[v], self.cost[v])
        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)

def op(p, v, d):
    p_size, p_dist, p_square = p
    v_size, v_dist, v_square = v
    size = p_size + v_size
    dist = (p_dist + v_dist + d * v_size) % MOD
    square = (p_square + v_square + d**2 * v_size + 2 * d * v_dist) % MOD
    return size, dist, square

def merge(lt, rt):
    l_size, l_dist, l_square = lt
    r_size, r_dist, r_square = rt
    size = l_size + r_size - 1
    dist = (l_dist + r_dist) % MOD
    square = (l_square + r_square) % MOD
    return size, dist, square

dp = t.rerooting(op, (1, 0, 0), merge, (0, 0, 0))

res = 0

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

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