結果
問題 | No.1124 Earthquake Safety |
ユーザー | e869120 |
提出日時 | 2020-07-05 01:11:04 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 373 ms / 3,000 ms |
コード長 | 3,069 bytes |
コンパイル時間 | 1,271 ms |
コンパイル使用メモリ | 73,260 KB |
実行使用メモリ | 87,936 KB |
最終ジャッジ日時 | 2024-09-19 18:34:50 |
合計ジャッジ時間 | 16,853 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 14 ms
28,160 KB |
testcase_01 | AC | 17 ms
28,288 KB |
testcase_02 | AC | 13 ms
28,160 KB |
testcase_03 | AC | 13 ms
28,288 KB |
testcase_04 | AC | 13 ms
28,160 KB |
testcase_05 | AC | 13 ms
28,160 KB |
testcase_06 | AC | 14 ms
28,288 KB |
testcase_07 | AC | 20 ms
29,056 KB |
testcase_08 | AC | 75 ms
37,504 KB |
testcase_09 | AC | 256 ms
55,680 KB |
testcase_10 | AC | 189 ms
87,936 KB |
testcase_11 | AC | 14 ms
28,160 KB |
testcase_12 | AC | 13 ms
28,288 KB |
testcase_13 | AC | 12 ms
28,160 KB |
testcase_14 | AC | 266 ms
55,680 KB |
testcase_15 | AC | 303 ms
55,552 KB |
testcase_16 | AC | 272 ms
55,680 KB |
testcase_17 | AC | 297 ms
55,680 KB |
testcase_18 | AC | 278 ms
55,552 KB |
testcase_19 | AC | 285 ms
55,680 KB |
testcase_20 | AC | 276 ms
55,808 KB |
testcase_21 | AC | 277 ms
55,680 KB |
testcase_22 | AC | 276 ms
55,680 KB |
testcase_23 | AC | 295 ms
56,448 KB |
testcase_24 | AC | 312 ms
56,576 KB |
testcase_25 | AC | 296 ms
56,832 KB |
testcase_26 | AC | 289 ms
56,832 KB |
testcase_27 | AC | 298 ms
58,112 KB |
testcase_28 | AC | 292 ms
58,052 KB |
testcase_29 | AC | 293 ms
63,360 KB |
testcase_30 | AC | 313 ms
63,488 KB |
testcase_31 | AC | 373 ms
74,880 KB |
testcase_32 | AC | 311 ms
72,832 KB |
testcase_33 | AC | 358 ms
74,368 KB |
testcase_34 | AC | 352 ms
76,672 KB |
testcase_35 | AC | 272 ms
55,168 KB |
testcase_36 | AC | 257 ms
53,504 KB |
testcase_37 | AC | 237 ms
54,016 KB |
testcase_38 | AC | 234 ms
54,272 KB |
testcase_39 | AC | 224 ms
53,120 KB |
testcase_40 | AC | 222 ms
54,272 KB |
testcase_41 | AC | 210 ms
52,992 KB |
testcase_42 | AC | 244 ms
53,632 KB |
testcase_43 | AC | 234 ms
54,144 KB |
testcase_44 | AC | 227 ms
53,760 KB |
testcase_45 | AC | 222 ms
53,120 KB |
testcase_46 | AC | 218 ms
53,632 KB |
testcase_47 | AC | 215 ms
52,736 KB |
testcase_48 | AC | 208 ms
52,736 KB |
testcase_49 | AC | 193 ms
52,644 KB |
testcase_50 | AC | 21 ms
28,160 KB |
testcase_51 | AC | 21 ms
28,032 KB |
testcase_52 | AC | 20 ms
28,160 KB |
testcase_53 | AC | 20 ms
28,032 KB |
testcase_54 | AC | 19 ms
28,032 KB |
testcase_55 | AC | 19 ms
28,032 KB |
testcase_56 | AC | 20 ms
28,160 KB |
testcase_57 | AC | 20 ms
28,160 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; }