結果
問題 | No.1333 Squared Sum |
ユーザー | chineristAC |
提出日時 | 2021-01-08 21:44:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,041 ms / 2,000 ms |
コード長 | 1,565 bytes |
コンパイル時間 | 286 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 144,956 KB |
最終ジャッジ日時 | 2024-11-16 11:01:10 |
合計ジャッジ時間 | 28,664 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
54,016 KB |
testcase_01 | AC | 47 ms
54,016 KB |
testcase_02 | AC | 45 ms
54,016 KB |
testcase_03 | AC | 953 ms
138,308 KB |
testcase_04 | AC | 967 ms
137,876 KB |
testcase_05 | AC | 881 ms
137,484 KB |
testcase_06 | AC | 934 ms
138,132 KB |
testcase_07 | AC | 947 ms
138,368 KB |
testcase_08 | AC | 934 ms
137,144 KB |
testcase_09 | AC | 918 ms
137,124 KB |
testcase_10 | AC | 927 ms
138,208 KB |
testcase_11 | AC | 950 ms
134,716 KB |
testcase_12 | AC | 1,017 ms
137,864 KB |
testcase_13 | AC | 681 ms
134,628 KB |
testcase_14 | AC | 966 ms
137,568 KB |
testcase_15 | AC | 976 ms
136,416 KB |
testcase_16 | AC | 46 ms
53,760 KB |
testcase_17 | AC | 46 ms
53,632 KB |
testcase_18 | AC | 47 ms
53,888 KB |
testcase_19 | AC | 47 ms
53,760 KB |
testcase_20 | AC | 47 ms
53,504 KB |
testcase_21 | AC | 47 ms
53,888 KB |
testcase_22 | AC | 47 ms
53,888 KB |
testcase_23 | AC | 47 ms
53,760 KB |
testcase_24 | AC | 47 ms
53,504 KB |
testcase_25 | AC | 46 ms
53,760 KB |
testcase_26 | AC | 992 ms
134,800 KB |
testcase_27 | AC | 994 ms
136,436 KB |
testcase_28 | AC | 1,041 ms
135,384 KB |
testcase_29 | AC | 733 ms
135,056 KB |
testcase_30 | AC | 411 ms
103,040 KB |
testcase_31 | AC | 280 ms
93,056 KB |
testcase_32 | AC | 568 ms
112,492 KB |
testcase_33 | AC | 463 ms
104,712 KB |
testcase_34 | AC | 771 ms
123,980 KB |
testcase_35 | AC | 589 ms
113,792 KB |
testcase_36 | AC | 368 ms
102,568 KB |
testcase_37 | AC | 380 ms
101,440 KB |
testcase_38 | AC | 444 ms
103,424 KB |
testcase_39 | AC | 657 ms
119,824 KB |
testcase_40 | AC | 606 ms
144,956 KB |
testcase_41 | AC | 589 ms
140,932 KB |
testcase_42 | AC | 615 ms
144,048 KB |
testcase_43 | AC | 580 ms
141,492 KB |
ソースコード
import sys from collections import deque mod = 10**9+7 input = sys.stdin.readline N = int(input()) edge = [[] for i in range(N)] for i in range(N-1): u,v,w = map(int,input().split()) edge[u-1].append((v-1,w)) edge[v-1].append((u-1,w)) deq = deque([0]) parent = [-1]*N parent[0] = (0,0) res = [] while deq: v = deq.popleft() res.append(v) for nv,w in edge[v]: if parent[nv]==-1: parent[nv] = (v,w) deq.append(nv) res = res[::-1] size = [1 for v in range(N)] dist_sum = [0 for v in range(N)] dist_sum_square = [0 for v in range(N)] for v in res: for nv,w in edge[v]: if nv!=parent[v][0]: size[v] += size[nv] dist_sum[v] += dist_sum[nv] + w * size[nv] dist_sum[v] %= mod dist_sum_square[v] += dist_sum_square[nv] + 2 * w * dist_sum[nv] + size[nv] * w * w dist_sum_square[v] %= mod #4 9 1 3 ans = [0 for v in range(N)] ans[0] = dist_sum_square[0] dist_sum_ans = [0 for v in range(N)] dist_sum_ans[0] = dist_sum[0] res = res[::-1] for v in res: if v==0: continue pv,w = parent[v] tmp = ans[pv] - ( dist_sum_square[v] + 2 * w * dist_sum[v] + size[v] * w * w ) tmp_dist_sum = dist_sum_ans[pv] - (w * size[v] + dist_sum[v]) ans[v] = dist_sum_square[v] + tmp + 2 * w * (tmp_dist_sum) + (N - size[v]) * w * w dist_sum_ans[v] = dist_sum[v] + w * (N - size[v]) + tmp_dist_sum ans[v] %= mod dist_sum_ans[v] %= mod S = 0 for v in range(N): S += ans[v] S %= mod S *= pow(2,mod-2,mod) print(S%mod)