結果

問題 No.1333 Squared Sum
ユーザー とりゐとりゐ
提出日時 2022-11-07 16:37:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,479 ms / 2,000 ms
コード長 1,873 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 252,404 KB
最終ジャッジ日時 2023-09-28 05:45:09
合計ジャッジ時間 41,642 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
71,776 KB
testcase_01 AC 84 ms
71,512 KB
testcase_02 AC 84 ms
71,812 KB
testcase_03 AC 1,479 ms
219,640 KB
testcase_04 AC 1,452 ms
221,772 KB
testcase_05 AC 1,335 ms
217,260 KB
testcase_06 AC 1,421 ms
228,988 KB
testcase_07 AC 1,430 ms
224,584 KB
testcase_08 AC 1,402 ms
229,388 KB
testcase_09 AC 1,410 ms
220,104 KB
testcase_10 AC 1,427 ms
220,436 KB
testcase_11 AC 1,396 ms
224,556 KB
testcase_12 AC 1,450 ms
217,868 KB
testcase_13 AC 930 ms
218,372 KB
testcase_14 AC 1,319 ms
221,048 KB
testcase_15 AC 1,304 ms
217,360 KB
testcase_16 AC 80 ms
71,784 KB
testcase_17 AC 83 ms
71,876 KB
testcase_18 AC 82 ms
71,564 KB
testcase_19 AC 84 ms
71,464 KB
testcase_20 AC 86 ms
71,572 KB
testcase_21 AC 85 ms
71,520 KB
testcase_22 AC 84 ms
71,740 KB
testcase_23 AC 83 ms
71,512 KB
testcase_24 AC 80 ms
71,388 KB
testcase_25 AC 81 ms
71,684 KB
testcase_26 AC 1,423 ms
230,700 KB
testcase_27 AC 1,421 ms
229,604 KB
testcase_28 AC 1,360 ms
226,052 KB
testcase_29 AC 999 ms
219,932 KB
testcase_30 AC 620 ms
136,284 KB
testcase_31 AC 390 ms
106,340 KB
testcase_32 AC 867 ms
156,464 KB
testcase_33 AC 674 ms
136,380 KB
testcase_34 AC 1,147 ms
191,324 KB
testcase_35 AC 855 ms
157,524 KB
testcase_36 AC 540 ms
121,044 KB
testcase_37 AC 577 ms
127,188 KB
testcase_38 AC 638 ms
136,912 KB
testcase_39 AC 952 ms
174,616 KB
testcase_40 AC 1,261 ms
252,404 KB
testcase_41 AC 1,247 ms
250,868 KB
testcase_42 AC 1,247 ms
250,364 KB
testcase_43 AC 1,175 ms
243,560 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],v))
    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,edge[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],v))
  
  
  ans=[E for i in range(n)]

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

  return ans



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

def f(res,v,u):
  a,b,c=res
  w=weight[v+(u<<20)]
  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]


weight={}
n=int(input())
edge=[[] 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+(y<<20)]=w
  weight[y+(x<<20)]=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()
res=0
for i in ans:
  res+=i[0]

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