結果
問題 | No.1124 Earthquake Safety |
ユーザー | QCFium |
提出日時 | 2020-07-03 16:18:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,043 ms / 3,000 ms |
コード長 | 1,576 bytes |
コンパイル時間 | 213 ms |
コンパイル使用メモリ | 82,312 KB |
実行使用メモリ | 173,960 KB |
最終ジャッジ日時 | 2024-09-16 17:15:34 |
合計ジャッジ時間 | 39,924 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
52,352 KB |
testcase_01 | AC | 40 ms
52,096 KB |
testcase_02 | AC | 39 ms
52,224 KB |
testcase_03 | AC | 38 ms
52,608 KB |
testcase_04 | AC | 38 ms
52,608 KB |
testcase_05 | AC | 41 ms
52,992 KB |
testcase_06 | AC | 112 ms
77,056 KB |
testcase_07 | AC | 142 ms
78,648 KB |
testcase_08 | AC | 356 ms
90,368 KB |
testcase_09 | AC | 951 ms
116,992 KB |
testcase_10 | AC | 623 ms
173,960 KB |
testcase_11 | AC | 39 ms
52,352 KB |
testcase_12 | AC | 37 ms
52,096 KB |
testcase_13 | AC | 39 ms
52,608 KB |
testcase_14 | AC | 1,025 ms
117,376 KB |
testcase_15 | AC | 1,031 ms
118,400 KB |
testcase_16 | AC | 1,022 ms
118,400 KB |
testcase_17 | AC | 1,034 ms
117,376 KB |
testcase_18 | AC | 1,043 ms
118,272 KB |
testcase_19 | AC | 1,035 ms
117,504 KB |
testcase_20 | AC | 1,022 ms
117,504 KB |
testcase_21 | AC | 1,039 ms
118,400 KB |
testcase_22 | AC | 1,021 ms
117,248 KB |
testcase_23 | AC | 960 ms
116,352 KB |
testcase_24 | AC | 959 ms
116,476 KB |
testcase_25 | AC | 886 ms
116,992 KB |
testcase_26 | AC | 899 ms
115,968 KB |
testcase_27 | AC | 874 ms
118,956 KB |
testcase_28 | AC | 875 ms
119,552 KB |
testcase_29 | AC | 976 ms
130,444 KB |
testcase_30 | AC | 929 ms
129,916 KB |
testcase_31 | AC | 964 ms
149,592 KB |
testcase_32 | AC | 967 ms
146,600 KB |
testcase_33 | AC | 972 ms
171,448 KB |
testcase_34 | AC | 956 ms
170,248 KB |
testcase_35 | AC | 872 ms
116,608 KB |
testcase_36 | AC | 850 ms
116,992 KB |
testcase_37 | AC | 841 ms
121,344 KB |
testcase_38 | AC | 881 ms
121,088 KB |
testcase_39 | AC | 836 ms
119,424 KB |
testcase_40 | AC | 849 ms
120,064 KB |
testcase_41 | AC | 859 ms
118,912 KB |
testcase_42 | AC | 795 ms
118,656 KB |
testcase_43 | AC | 812 ms
119,040 KB |
testcase_44 | AC | 842 ms
118,784 KB |
testcase_45 | AC | 866 ms
118,144 KB |
testcase_46 | AC | 837 ms
118,144 KB |
testcase_47 | AC | 849 ms
120,320 KB |
testcase_48 | AC | 846 ms
122,880 KB |
testcase_49 | AC | 878 ms
151,696 KB |
testcase_50 | AC | 39 ms
52,352 KB |
testcase_51 | AC | 38 ms
51,968 KB |
testcase_52 | AC | 39 ms
52,864 KB |
testcase_53 | AC | 38 ms
52,480 KB |
testcase_54 | AC | 39 ms
52,224 KB |
testcase_55 | AC | 38 ms
52,480 KB |
testcase_56 | AC | 39 ms
52,480 KB |
testcase_57 | AC | 39 ms
52,864 KB |
ソースコード
import sys sys.setrecursionlimit(400000) MOD = 1000000007 inv2 = 500000004 def dfs0(i) : stack = [[-1, -1], [i, 0]] while len(stack) > 1 : i = stack[-1][0] j = stack[-1][1] if j == len(hen[i]) : stack.pop(-1) if len(stack) > 1 : subtree_dsum[stack[-1][0]] = (subtree_dsum[stack[-1][0]] + subtree_dsum[i] * inv2) % MOD else : stack[-1][1] += 1 if hen[i][j] != stack[-2][0] : stack.append([hen[i][j], 0]) res = 0 def dfs1(i) : global res stack = [[-1, -1, -1], [i, 0, 0]] sum1_global = [0 for i in range(n)] while len(stack) > 1 : i = stack[-1][0] j = stack[-1][1] if not j : all = [] if len(stack) > 2 : all.append(stack[-1][2]) for k in hen[i] : if k != stack[-2][0] : all.append(subtree_dsum[k]) sum1 = sum(all) % MOD sum1_global[i] = sum1 sum2 = sum(map(lambda i : i * i, all)) % MOD sum3 = sum(map(lambda i : i * i % MOD * i, all)) % MOD res = (res \ + sum1 * sum1 % MOD * sum1 - sum2 * sum1 * 3 + sum3 * 2 \ + (sum1 * sum1 - sum2) * 3 \ + sum1 * 3 + 1) % MOD if res < 0 : res += MOD if j == len(hen[i]) : stack.pop(-1) else : stack[-1][1] += 1 if hen[i][j] != stack[-2][0] : next = (sum1_global[i] - subtree_dsum[hen[i][j]] + 1) * inv2 % MOD if next < 0 : next += MOD stack.append([hen[i][j], 0, next]) n = int(input()) hen = [[] for i in range(n)] for i in range(n - 1) : a, b = map(int, input().split()) a -= 1 b -= 1 hen[a].append(b) hen[b].append(a) subtree_dsum = [inv2 for i in range(n)] dfs0(0) dfs1(0) res = res * pow(2, n - 1, MOD) % MOD print(res)