結果
問題 | No.1124 Earthquake Safety |
ユーザー | e869120 |
提出日時 | 2020-07-22 21:15:59 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 438 ms / 3,000 ms |
コード長 | 3,069 bytes |
コンパイル時間 | 981 ms |
コンパイル使用メモリ | 73,468 KB |
実行使用メモリ | 94,012 KB |
最終ジャッジ日時 | 2024-06-22 12:08:45 |
合計ジャッジ時間 | 18,662 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 13 ms
36,300 KB |
testcase_01 | AC | 12 ms
36,300 KB |
testcase_02 | AC | 13 ms
36,428 KB |
testcase_03 | AC | 12 ms
36,428 KB |
testcase_04 | AC | 12 ms
36,304 KB |
testcase_05 | AC | 12 ms
36,436 KB |
testcase_06 | AC | 13 ms
36,336 KB |
testcase_07 | AC | 20 ms
37,008 KB |
testcase_08 | AC | 94 ms
42,732 KB |
testcase_09 | AC | 355 ms
61,836 KB |
testcase_10 | AC | 180 ms
94,012 KB |
testcase_11 | AC | 13 ms
36,428 KB |
testcase_12 | AC | 13 ms
36,432 KB |
testcase_13 | AC | 13 ms
36,428 KB |
testcase_14 | AC | 351 ms
61,820 KB |
testcase_15 | AC | 361 ms
59,940 KB |
testcase_16 | AC | 376 ms
60,048 KB |
testcase_17 | AC | 381 ms
59,960 KB |
testcase_18 | AC | 389 ms
61,820 KB |
testcase_19 | AC | 386 ms
61,824 KB |
testcase_20 | AC | 383 ms
61,824 KB |
testcase_21 | AC | 392 ms
61,820 KB |
testcase_22 | AC | 391 ms
61,820 KB |
testcase_23 | AC | 379 ms
62,684 KB |
testcase_24 | AC | 362 ms
62,688 KB |
testcase_25 | AC | 383 ms
62,716 KB |
testcase_26 | AC | 390 ms
60,992 KB |
testcase_27 | AC | 373 ms
64,336 KB |
testcase_28 | AC | 377 ms
64,392 KB |
testcase_29 | AC | 400 ms
69,672 KB |
testcase_30 | AC | 390 ms
69,672 KB |
testcase_31 | AC | 416 ms
79,176 KB |
testcase_32 | AC | 404 ms
79,024 KB |
testcase_33 | AC | 434 ms
80,524 KB |
testcase_34 | AC | 438 ms
82,900 KB |
testcase_35 | AC | 359 ms
61,324 KB |
testcase_36 | AC | 295 ms
59,640 KB |
testcase_37 | AC | 284 ms
60,152 KB |
testcase_38 | AC | 275 ms
60,392 KB |
testcase_39 | AC | 254 ms
59,192 KB |
testcase_40 | AC | 248 ms
60,388 KB |
testcase_41 | AC | 225 ms
57,420 KB |
testcase_42 | AC | 209 ms
59,640 KB |
testcase_43 | AC | 204 ms
60,212 KB |
testcase_44 | AC | 189 ms
59,944 KB |
testcase_45 | AC | 193 ms
59,240 KB |
testcase_46 | AC | 199 ms
59,640 KB |
testcase_47 | AC | 200 ms
58,776 KB |
testcase_48 | AC | 193 ms
59,052 KB |
testcase_49 | AC | 196 ms
58,860 KB |
testcase_50 | AC | 12 ms
32,204 KB |
testcase_51 | AC | 13 ms
36,428 KB |
testcase_52 | AC | 13 ms
36,300 KB |
testcase_53 | AC | 13 ms
36,424 KB |
testcase_54 | AC | 13 ms
36,428 KB |
testcase_55 | AC | 12 ms
36,424 KB |
testcase_56 | AC | 12 ms
36,300 KB |
testcase_57 | AC | 13 ms
36,428 KB |
ソースコード
#include <iostream> #include <vector> using namespace std; #pragma warning (disable: 4996) const long long mod = 1000000007; const long long inv2 = 500000004; const long long inv6 = 166666668; // 入出力 long long N, A[1 << 19], B[1 << 19]; long long Answer1, Answer2, Answer3A, Answer3B, Answer3C; vector<int> X[1 << 19]; vector<int> Y[1 << 19]; // DP int par[1 << 19]; long long dp1[1 << 19]; long long dp2[1 << 19]; long long Eval[1 << 19]; void dfs1(int pos) { dp1[pos] += 1LL; for (int i : X[pos]) { if (dp1[i] != 0LL) continue; dfs1(i); par[i] = pos; Y[pos].push_back(i); dp1[pos] += dp1[i] * inv2 % mod; dp1[pos] %= mod; } } void dfs2(int pos) { for (int i : Y[pos]) { dp2[i] += dp2[pos] * inv2 % mod; dp2[i] += dp1[pos] * inv2 % mod; dp2[i] -= dp1[i] * (inv2 * inv2 % mod) % mod; dp2[i] = (dp2[i] + mod * mod) % mod; dfs2(i); } } void getEval() { for (int i = 1; i <= N; i++) { long long r = 0, s1 = 0, s2 = 0; for (int j : Y[i]) s1 += dp1[j] * inv2 % mod; for (int j : Y[i]) s2 += (dp1[j] * inv2 % mod) * (dp1[j] * inv2 % mod) % mod; s1 %= mod; s2 %= mod; r = s1 * s1 - s2; r = (r + mod * mod) % mod; Eval[i] = r * inv2 % mod; } } int main() { // Step #1. 入力 scanf("%lld", &N); for (int i = 1; i <= N - 1; i++) scanf("%lld%lld", &A[i], &B[i]); for (int i = 1; i <= N - 1; i++) { X[A[i]].push_back(B[i]); X[B[i]].push_back(A[i]); } // Step #2. 全方位木DP dfs1(1); dfs2(1); // Step #3. [1], [2] の場合を求める Answer1 = N; getEval(); for (int i = 1; i <= N; i++) Answer2 += (dp1[i] + mod - 1LL) % mod; for (int i = 1; i <= N; i++) Answer2 += Eval[i]; Answer2 %= mod; // Step #4. [3] の場合 - Pattern A for (int i = 1; i <= N; i++) { long long r1 = (dp1[i] - 1LL); long long r2 = dp2[i]; Answer3A += r1 * r2 % mod; Answer3A %= mod; } for (int i = 1; i <= N; i++) { Answer3A += Eval[i]; Answer3A %= mod; } // Step #5. [3] の場合 - Pattern B for (int i = 1; i <= N; i++) { long long r1 = dp2[i]; long long r2 = Eval[i]; Answer3B += r1 * r2 % mod; Answer3B %= mod; } // Step #6. [3] の場合 - Pattern C for (int i = 1; i <= N; i++) { long long s1 = 0, s2 = 0, s3 = 0; for (int j : Y[i]) { s1 += dp1[j]; s2 += dp1[j] * dp1[j] % mod; s3 += (dp1[j] * dp1[j] % mod) * dp1[j] % mod; s1 %= mod; s2 %= mod; s3 %= mod; } long long v1 = (s1 * s1 % mod) * s1 % mod; long long v2 = s1 * s2 % mod; long long v3 = s3; long long res = (v1 - 3LL * v2 + 2LL * v3 + mod * mod) % mod; res = res * inv6 % mod; res = res * inv2 % mod; res = res * inv2 % mod; res = res * inv2 % mod; Answer3C += res; Answer3C %= mod; } // Step #7. 出力 long long Ret1 = Answer1; long long Ret2 = Answer2; long long Ret3 = (Answer3A + Answer3B + Answer3C) % mod; for (int i = 1; i <= N - 1; i++) { Ret1 *= 2LL; Ret1 %= mod; } for (int i = 1; i <= N - 1; i++) { Ret2 *= 2LL; Ret2 %= mod; } for (int i = 1; i <= N - 1; i++) { Ret3 *= 2LL; Ret3 %= mod; } cout << (1LL * Ret1 + 6LL * Ret2 + 6LL * Ret3) % mod << endl; return 0; }