結果
問題 | No.1333 Squared Sum |
ユーザー | tpyneriver |
提出日時 | 2020-10-31 08:20:49 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 639 ms / 2,000 ms |
コード長 | 1,318 bytes |
コンパイル時間 | 340 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 182,488 KB |
最終ジャッジ日時 | 2024-07-22 04:42:23 |
合計ジャッジ時間 | 18,748 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,096 KB |
testcase_01 | AC | 43 ms
52,352 KB |
testcase_02 | AC | 42 ms
52,352 KB |
testcase_03 | AC | 639 ms
151,384 KB |
testcase_04 | AC | 589 ms
151,288 KB |
testcase_05 | AC | 624 ms
150,932 KB |
testcase_06 | AC | 613 ms
151,264 KB |
testcase_07 | AC | 582 ms
151,056 KB |
testcase_08 | AC | 599 ms
153,040 KB |
testcase_09 | AC | 610 ms
150,988 KB |
testcase_10 | AC | 612 ms
150,860 KB |
testcase_11 | AC | 601 ms
139,896 KB |
testcase_12 | AC | 617 ms
150,988 KB |
testcase_13 | AC | 495 ms
152,212 KB |
testcase_14 | AC | 617 ms
154,548 KB |
testcase_15 | AC | 628 ms
153,292 KB |
testcase_16 | AC | 42 ms
52,480 KB |
testcase_17 | AC | 42 ms
52,096 KB |
testcase_18 | AC | 42 ms
52,096 KB |
testcase_19 | AC | 42 ms
52,352 KB |
testcase_20 | AC | 42 ms
52,096 KB |
testcase_21 | AC | 42 ms
52,224 KB |
testcase_22 | AC | 42 ms
52,480 KB |
testcase_23 | AC | 42 ms
52,352 KB |
testcase_24 | AC | 42 ms
51,968 KB |
testcase_25 | AC | 42 ms
52,352 KB |
testcase_26 | AC | 595 ms
150,808 KB |
testcase_27 | AC | 590 ms
150,548 KB |
testcase_28 | AC | 610 ms
150,364 KB |
testcase_29 | AC | 507 ms
150,680 KB |
testcase_30 | AC | 267 ms
111,232 KB |
testcase_31 | AC | 178 ms
95,872 KB |
testcase_32 | AC | 363 ms
127,232 KB |
testcase_33 | AC | 298 ms
116,608 KB |
testcase_34 | AC | 482 ms
133,888 KB |
testcase_35 | AC | 378 ms
127,104 KB |
testcase_36 | AC | 240 ms
102,644 KB |
testcase_37 | AC | 256 ms
107,648 KB |
testcase_38 | AC | 290 ms
112,000 KB |
testcase_39 | AC | 421 ms
131,584 KB |
testcase_40 | AC | 463 ms
174,336 KB |
testcase_41 | AC | 454 ms
175,736 KB |
testcase_42 | AC | 464 ms
174,500 KB |
testcase_43 | AC | 450 ms
182,488 KB |
ソースコード
import sys readline = sys.stdin.readline def parorder(Edge, p): N = len(Edge) par = [0]*N dist = [0]*N par[p] = -1 stack = [p] order = [] visited = set([p]) ast = stack.append apo = order.append while stack: vn = stack.pop() apo(vn) for w, vf in Edge[vn]: if vf in visited: continue visited.add(vf) par[vf] = vn dist[vf] = w ast(vf) return par, order, dist def getcld(p): res = [[] for _ in range(len(p))] for i, v in enumerate(p[1:], 1): res[v].append(i) return res MOD = 10**9+7 N = int(readline()) Edge = [[] for _ in range(N)] for _ in range(N-1): a, b, w = map(int, readline().split()) a -= 1 b -= 1 Edge[a].append((w, b)) Edge[b].append((w, a)) P, L, dist = parorder(Edge, 0) dsum = [0]*N dsum2 = [0]*N size = [1]*N ans = 0 for l in L[:0:-1]: p = P[l] w = dist[l] dsum2[l] = (dsum2[l] + w*w%MOD*size[l] + 2*w*dsum[l])%MOD dsum[l] = (dsum[l] + w*size[l])%MOD ans = (ans + (dsum[l] + dsum[p])**2 - dsum[l]*dsum[l] - dsum[p]*dsum[p] + size[p]*dsum2[l] + size[l]*dsum2[p])%MOD dsum[p] = (dsum[p] + dsum[l])%MOD dsum2[p] = (dsum2[p] + dsum2[l])%MOD size[p] += size[l] print(ans)