結果

問題 No.1333 Squared Sum
ユーザー とりゐとりゐ
提出日時 2022-11-07 16:42:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,311 ms / 2,000 ms
コード長 1,845 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 217,536 KB
最終ジャッジ日時 2023-09-28 05:49:26
合計ジャッジ時間 35,173 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
71,760 KB
testcase_01 AC 82 ms
71,752 KB
testcase_02 AC 85 ms
71,624 KB
testcase_03 AC 1,272 ms
191,240 KB
testcase_04 AC 1,266 ms
191,620 KB
testcase_05 AC 1,241 ms
191,776 KB
testcase_06 AC 1,276 ms
191,536 KB
testcase_07 AC 1,278 ms
191,844 KB
testcase_08 AC 1,224 ms
191,356 KB
testcase_09 AC 1,229 ms
191,148 KB
testcase_10 AC 1,271 ms
191,400 KB
testcase_11 AC 1,217 ms
188,668 KB
testcase_12 AC 1,282 ms
191,380 KB
testcase_13 AC 875 ms
189,456 KB
testcase_14 AC 1,275 ms
191,172 KB
testcase_15 AC 1,262 ms
189,932 KB
testcase_16 AC 85 ms
71,784 KB
testcase_17 AC 85 ms
71,904 KB
testcase_18 AC 84 ms
71,644 KB
testcase_19 AC 85 ms
71,768 KB
testcase_20 AC 85 ms
71,628 KB
testcase_21 AC 86 ms
71,916 KB
testcase_22 AC 84 ms
71,840 KB
testcase_23 AC 83 ms
71,636 KB
testcase_24 AC 83 ms
71,532 KB
testcase_25 AC 83 ms
71,760 KB
testcase_26 AC 1,249 ms
188,728 KB
testcase_27 AC 1,311 ms
189,188 KB
testcase_28 AC 1,261 ms
188,760 KB
testcase_29 AC 871 ms
189,144 KB
testcase_30 AC 565 ms
124,300 KB
testcase_31 AC 359 ms
104,756 KB
testcase_32 AC 745 ms
148,208 KB
testcase_33 AC 610 ms
131,588 KB
testcase_34 AC 998 ms
166,576 KB
testcase_35 AC 748 ms
147,992 KB
testcase_36 AC 473 ms
118,176 KB
testcase_37 AC 498 ms
121,132 KB
testcase_38 AC 551 ms
128,752 KB
testcase_39 AC 852 ms
154,176 KB
testcase_40 AC 1,013 ms
215,392 KB
testcase_41 AC 1,012 ms
215,668 KB
testcase_42 AC 1,015 ms
215,500 KB
testcase_43 AC 995 ms
217,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def rerooting():  
  dp=[[E]*len(edge[v]) for v in range(n)]
  
  # dfs1
  memo=[E]*n
  for v in order[::-1]:
    res=E
    for i in range(len(edge[v])):
      if edge[v][i]==par[v]:
        continue
      dp[v][i]=memo[edge[v][i]]
      res=merge(res,f(dp[v][i],edge[v][i],weight[v][i]))
    memo[v]=g(res,v)
  
  # dfs2
  memo2=[E]*n
  for v in order:
    for i in range(len(edge[v])):
      if edge[v][i]==par[v]:
        dp[v][i]=memo2[v]
    
    s=len(edge[v])
    cumR=[E]*(s+1)
    cumR[s]=E
    for i in range(s,0,-1):
      cumR[i-1]=merge(cumR[i],f(dp[v][i-1],v,weight[v][i-1]))
      
    cumL=E
    for i in range(s):
      if edge[v][i]!=par[v]:
        val=val=merge(cumL,cumR[i+1])
        memo2[edge[v][i]]=g(val,v)
      cumL=merge(cumL,f(dp[v][i],edge[v][i],weight[v][i]))
  
  
  ans=0

  for v in range(n):
    res=E
    for i in range(len(edge[v])):
      res=merge(res,f(dp[v][i],edge[v][i],weight[v][i]))
    ans+=g(res,v)[0]
    #ans[v]=calc_ans(res,v)

  return ans



E=(0,0,0)
mod=10**9+7

def f(res,v,w):
  a,b,c=res
  a+=2*w*b+c*w%mod*w
  a%=mod
  b+=c*w
  b%=mod 
  return (a,b,c)
 
def g(res,v):
  a,b,c=res
  return (a,b,c+1)
 
def merge(a,b):
  xa,ya,za=a
  xb,yb,zb=b
  return ((xa+xb)%mod,(ya+yb)%mod,za+zb)

'''
def calc_ans(res,v):
  return
'''


from sys import stdin
input=lambda :stdin.readline()[:-1]


n=int(input())
edge=[[] for i in range(n)]
weight=[[] for i in range(n)]
for i in range(n-1):
  x,y,w=map(int,input().split())
  x,y=x-1,y-1
  edge[x].append(y)
  edge[y].append(x)
  weight[x].append(w)
  weight[y].append(w)


# make order table
# root = 0

from collections import deque
order=[]
par=[-1]*n
todo=deque([0])
while todo:
  v=todo.popleft()
  order.append(v)
  for u in edge[v]:
    if u!=par[v]:
      par[u]=v
      todo.append(u)

ans=rerooting()
ans*=pow(2,mod-2,mod)
print(ans%mod)
0