結果
問題 |
No.1333 Squared Sum
|
ユーザー |
👑 ![]() |
提出日時 | 2022-02-25 17:09:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 966 ms / 2,000 ms |
コード長 | 959 bytes |
コンパイル時間 | 145 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 143,564 KB |
最終ジャッジ日時 | 2024-07-03 11:40:37 |
合計ジャッジ時間 | 25,222 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 44 |
ソースコード
N=int(input()) M=10**9+7 ans=0 G=[[(0,0)]*0 for i in range(N)] order=[0] sub_size=[0]*N pare=[(-1,0)]*N lower_sum=[0]*N upper_sum=[0]*N for i in range(N-1): a,b,c=map(int,input().split()) a-=1 b-=1 G[a].append((b,c)) G[b].append((a,c)) for i in range(N): a=order[i] for x in G[a]: if pare[a][0]!=x[0]: order.append(x[0]) pare[x[0]]=(a,x[1]) for i in range(N): a=order[N-1-i] L=pare[a][1] for x in G[a]: if x[0]!=pare[a][0]: sub_size[a]+=sub_size[x[0]] lower_sum[a]+=lower_sum[x[0]] lower_sum[a]%=M sub_size[a]+=1 ans+=(((sub_size[a]*(N-sub_size[a]))%M)*pow(L,2,M))%M ans+=(L*lower_sum[a]*(N-sub_size[a]))%M ans%=M lower_sum[a]+=L*sub_size[a] for i in range(N): a=order[i] L=pare[a][1] lower_sum[a]-=(L*sub_size[a])%M for x in G[a]: if pare[a][0]==x[0]: continue tmp=(upper_sum[a]+lower_sum[a]-lower_sum[x[0]])%M ans+=(x[1]*tmp*sub_size[x[0]])%M upper_sum[x[0]]=(tmp+x[1]*(N-sub_size[x[0]]))%M ans%=M print(ans)