結果

問題 No.1333 Squared Sum
ユーザー toyuzukotoyuzuko
提出日時 2021-04-17 23:27:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,512 ms / 2,000 ms
コード長 2,688 bytes
コンパイル時間 842 ms
コンパイル使用メモリ 86,516 KB
実行使用メモリ 214,976 KB
最終ジャッジ日時 2023-09-17 07:22:03
合計ジャッジ時間 42,429 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
70,856 KB
testcase_01 AC 72 ms
70,864 KB
testcase_02 AC 74 ms
70,928 KB
testcase_03 AC 1,440 ms
198,296 KB
testcase_04 AC 1,388 ms
199,088 KB
testcase_05 AC 1,434 ms
199,296 KB
testcase_06 AC 1,512 ms
199,572 KB
testcase_07 AC 1,426 ms
200,016 KB
testcase_08 AC 1,469 ms
198,996 KB
testcase_09 AC 1,461 ms
198,408 KB
testcase_10 AC 1,426 ms
199,764 KB
testcase_11 AC 1,452 ms
198,476 KB
testcase_12 AC 1,462 ms
198,896 KB
testcase_13 AC 893 ms
182,664 KB
testcase_14 AC 1,314 ms
183,820 KB
testcase_15 AC 1,304 ms
184,212 KB
testcase_16 AC 72 ms
70,808 KB
testcase_17 AC 75 ms
70,988 KB
testcase_18 AC 75 ms
70,840 KB
testcase_19 AC 74 ms
71,048 KB
testcase_20 AC 74 ms
70,844 KB
testcase_21 AC 73 ms
70,820 KB
testcase_22 AC 73 ms
71,016 KB
testcase_23 AC 73 ms
70,972 KB
testcase_24 AC 73 ms
70,720 KB
testcase_25 AC 74 ms
70,836 KB
testcase_26 AC 1,443 ms
188,612 KB
testcase_27 AC 1,430 ms
190,444 KB
testcase_28 AC 1,418 ms
189,712 KB
testcase_29 AC 972 ms
186,380 KB
testcase_30 AC 657 ms
126,492 KB
testcase_31 AC 450 ms
104,312 KB
testcase_32 AC 894 ms
145,996 KB
testcase_33 AC 767 ms
132,188 KB
testcase_34 AC 1,210 ms
173,376 KB
testcase_35 AC 915 ms
146,340 KB
testcase_36 AC 628 ms
122,308 KB
testcase_37 AC 614 ms
120,908 KB
testcase_38 AC 688 ms
126,460 KB
testcase_39 AC 1,030 ms
160,856 KB
testcase_40 AC 1,071 ms
214,976 KB
testcase_41 AC 1,043 ms
204,584 KB
testcase_42 AC 1,064 ms
214,452 KB
testcase_43 AC 980 ms
208,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

    def get_cost(self, u, v):
        if u < v:
            return self.cost[u * self.n + v]
        else:
            return self.cost[v * self.n + u]

    def set_root(self, r=0):
        self.root = r
        self.par = [None] * self.n
        self.ord = [r]
        stack = [r]
        while stack:
            v = stack.pop()
            for adj in self.tree[v]:
                if self.par[v] == adj: continue
                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 in self.tree[v]:
                if self.par[v] == adj: continue
                lt[adj] = tl
                self.w = self.get_cost(v, adj)
                tl = op(tl, dp[adj])
            for adj in self.tree[v][::-1]:
                if self.par[v] == adj: continue
                rt[adj] = tr
                self.w = self.get_cost(v, adj)
                tr = op(tr, dp[adj])
            dp[v] = tr
        for v in self.ord:
            if v == self.root: continue
            p = self.par[v]
            if p != self.root:
                pp = self.par[p]
                self.w = self.get_cost(p, pp)
                inv[v] = op(merge(lt[v], rt[v]), inv[p])
            else:
                inv[v] = merge(lt[v], rt[v])
            self.w = self.get_cost(v, p)
            dp[v] = op(dp[v], inv[v])
        return dp


import sys
input = sys.stdin.buffer.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):
    ps, pd, pds = p
    cs, cd, cds = c
    size = ps + cs
    w = t.w
    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 i in range(N):
    res += dp[i][2]
    res %= MOD

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