結果
問題 | No.1124 Earthquake Safety |
ユーザー | e869120 |
提出日時 | 2020-07-03 14:11:42 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 364 ms / 3,000 ms |
コード長 | 3,191 bytes |
コンパイル時間 | 814 ms |
コンパイル使用メモリ | 73,728 KB |
実行使用メモリ | 87,936 KB |
最終ジャッジ日時 | 2024-09-16 17:09:12 |
合計ジャッジ時間 | 14,876 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 12 ms
28,032 KB |
testcase_01 | AC | 13 ms
28,288 KB |
testcase_02 | AC | 12 ms
28,032 KB |
testcase_03 | AC | 13 ms
28,288 KB |
testcase_04 | AC | 13 ms
28,288 KB |
testcase_05 | AC | 13 ms
27,904 KB |
testcase_06 | AC | 14 ms
28,416 KB |
testcase_07 | AC | 18 ms
28,928 KB |
testcase_08 | AC | 84 ms
37,248 KB |
testcase_09 | AC | 321 ms
55,424 KB |
testcase_10 | AC | 180 ms
87,936 KB |
testcase_11 | AC | 12 ms
28,032 KB |
testcase_12 | AC | 13 ms
28,288 KB |
testcase_13 | AC | 13 ms
28,160 KB |
testcase_14 | AC | 337 ms
55,424 KB |
testcase_15 | AC | 332 ms
55,808 KB |
testcase_16 | AC | 326 ms
55,680 KB |
testcase_17 | AC | 332 ms
55,680 KB |
testcase_18 | AC | 316 ms
55,680 KB |
testcase_19 | AC | 316 ms
55,680 KB |
testcase_20 | AC | 332 ms
55,808 KB |
testcase_21 | AC | 330 ms
55,552 KB |
testcase_22 | AC | 342 ms
55,808 KB |
testcase_23 | AC | 342 ms
56,448 KB |
testcase_24 | AC | 353 ms
56,448 KB |
testcase_25 | AC | 328 ms
56,704 KB |
testcase_26 | AC | 330 ms
56,448 KB |
testcase_27 | AC | 304 ms
57,984 KB |
testcase_28 | AC | 313 ms
58,112 KB |
testcase_29 | AC | 324 ms
63,488 KB |
testcase_30 | AC | 330 ms
63,488 KB |
testcase_31 | AC | 364 ms
74,880 KB |
testcase_32 | AC | 343 ms
72,960 KB |
testcase_33 | AC | 358 ms
74,112 KB |
testcase_34 | AC | 344 ms
76,544 KB |
testcase_35 | AC | 273 ms
55,296 KB |
testcase_36 | AC | 250 ms
53,376 KB |
testcase_37 | AC | 258 ms
53,888 KB |
testcase_38 | AC | 235 ms
54,272 KB |
testcase_39 | AC | 219 ms
53,120 KB |
testcase_40 | AC | 219 ms
54,272 KB |
testcase_41 | AC | 209 ms
52,992 KB |
testcase_42 | AC | 211 ms
53,632 KB |
testcase_43 | AC | 203 ms
54,016 KB |
testcase_44 | AC | 197 ms
53,760 KB |
testcase_45 | AC | 189 ms
53,120 KB |
testcase_46 | AC | 179 ms
53,760 KB |
testcase_47 | AC | 178 ms
52,864 KB |
testcase_48 | AC | 186 ms
53,760 KB |
testcase_49 | AC | 189 ms
62,188 KB |
testcase_50 | AC | 12 ms
28,160 KB |
testcase_51 | AC | 13 ms
28,032 KB |
testcase_52 | AC | 12 ms
28,160 KB |
testcase_53 | AC | 13 ms
28,032 KB |
testcase_54 | AC | 13 ms
28,032 KB |
testcase_55 | AC | 13 ms
28,032 KB |
testcase_56 | AC | 12 ms
28,160 KB |
testcase_57 | AC | 12 ms
28,032 KB |
ソースコード
#include <iostream> #include <vector> #include <cassert> using namespace std; #pragma warning (disable: 4996) const long long mod = 1000000007; const long long inv2 = 500000004; // 入出力 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 dpr[1 << 19][4]; 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); // 制約チェック for (int i = 2; i <= N; i++) assert(par[i] != 0); for (int i = 1; i <= N - 1; i++) assert(1 <= A[i] && A[i] < B[i] && B[i] <= N); assert(1 <= N && N <= 300000); // 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++) { for (int j = 0; j <= Y[i].size(); j++) { dpr[j][0] = 0; dpr[j][1] = 0; dpr[j][2] = 0; dpr[j][3] = 0; } dpr[0][0] = 1; for (int j = 0; j < Y[i].size(); j++) { for (int k = 0; k <= 3; k++) dpr[j + 1][k] = dpr[j][k]; for (int k = 0; k <= 2; k++) { dpr[j + 1][k + 1] += dpr[j][k] * (dp1[Y[i][j]] * inv2 % mod) % mod; dpr[j + 1][k + 1] %= mod; } } Answer3C += dpr[Y[i].size()][3]; 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; }