結果
問題 | No.1333 Squared Sum |
ユーザー | maspy |
提出日時 | 2021-01-08 22:01:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 914 ms / 2,000 ms |
コード長 | 1,499 bytes |
コンパイル時間 | 372 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 153,652 KB |
最終ジャッジ日時 | 2024-11-16 11:48:59 |
合計ジャッジ時間 | 25,074 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
52,224 KB |
testcase_01 | AC | 37 ms
52,480 KB |
testcase_02 | AC | 37 ms
52,480 KB |
testcase_03 | AC | 896 ms
153,148 KB |
testcase_04 | AC | 880 ms
152,752 KB |
testcase_05 | AC | 895 ms
152,892 KB |
testcase_06 | AC | 910 ms
152,764 KB |
testcase_07 | AC | 877 ms
153,272 KB |
testcase_08 | AC | 914 ms
152,392 KB |
testcase_09 | AC | 873 ms
152,856 KB |
testcase_10 | AC | 883 ms
152,884 KB |
testcase_11 | AC | 878 ms
150,212 KB |
testcase_12 | AC | 842 ms
152,768 KB |
testcase_13 | AC | 730 ms
153,420 KB |
testcase_14 | AC | 882 ms
153,128 KB |
testcase_15 | AC | 881 ms
153,652 KB |
testcase_16 | AC | 38 ms
51,968 KB |
testcase_17 | AC | 38 ms
52,224 KB |
testcase_18 | AC | 37 ms
52,096 KB |
testcase_19 | AC | 38 ms
51,968 KB |
testcase_20 | AC | 37 ms
52,224 KB |
testcase_21 | AC | 37 ms
52,224 KB |
testcase_22 | AC | 38 ms
52,224 KB |
testcase_23 | AC | 37 ms
51,840 KB |
testcase_24 | AC | 37 ms
52,352 KB |
testcase_25 | AC | 38 ms
52,352 KB |
testcase_26 | AC | 902 ms
152,056 KB |
testcase_27 | AC | 900 ms
152,316 KB |
testcase_28 | AC | 907 ms
152,564 KB |
testcase_29 | AC | 769 ms
152,908 KB |
testcase_30 | AC | 455 ms
106,076 KB |
testcase_31 | AC | 304 ms
93,824 KB |
testcase_32 | AC | 581 ms
123,240 KB |
testcase_33 | AC | 491 ms
108,996 KB |
testcase_34 | AC | 727 ms
138,244 KB |
testcase_35 | AC | 565 ms
122,492 KB |
testcase_36 | AC | 392 ms
102,404 KB |
testcase_37 | AC | 403 ms
103,668 KB |
testcase_38 | AC | 462 ms
107,208 KB |
testcase_39 | AC | 646 ms
128,516 KB |
testcase_40 | AC | 423 ms
148,152 KB |
testcase_41 | AC | 426 ms
149,676 KB |
testcase_42 | AC | 421 ms
148,304 KB |
testcase_43 | AC | 399 ms
148,204 KB |
ソースコード
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 1_000_000_007 def bfs_tree(N, G, root=1): q = [root] parent = [0] * (N + 1) dist = [0] * (N + 1) for v in q: for w, cost in G[v]: if w == parent[v]: continue dist[w] = dist[v] + cost parent[w] = v q.append(w) return parent, q, dist def main(N, G): parent, order, dist = bfs_tree(N, G) ans = 0 # k 乗和 dp_0 = [0] * (N + 1) dp_1 = [0] * (N + 1) dp_2 = [0] * (N + 1) dp = [0] * (N + 1) # lca ごとの答 for v in order[::-1]: dp_0[v] += 1 dp[v] += dp_2[v] * dp_0[v] * 2 dp[v] += 2 * dp_1[v]**2 dp_0[v] %= MOD dp_1[v] %= MOD dp_2[v] %= MOD dp[v] %= MOD p = parent[v] if p == 0: break d = dist[v] - dist[p] dp[p] -= dp_0[v] * dp_2[v] * 2 + 4 * d * d * dp_0[v]**2 dp[p] -= 2 * dp_1[v] * dp_1[v] dp[p] -= 8 * d * dp_0[v] * dp_1[v] dp_0[p] += dp_0[v] dp_1[p] += dp_1[v] + d * dp_0[v] dp_2[p] += dp_2[v] + 2 * d * dp_1[v] + d * d * dp_0[v] ans = sum(dp) % MOD if ans & 1: ans += MOD ans //= 2 return ans N = int(readline()) G = [[] for _ in range(N + 1)] for line in readlines(): a, b, c = map(int, line.split()) G[a].append((b, c)) G[b].append((a, c)) print(main(N, G))