結果
問題 | No.2115 Making Forest Easy |
ユーザー | SSRS |
提出日時 | 2022-10-29 00:08:07 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,025 bytes |
コンパイル時間 | 1,858 ms |
コンパイル使用メモリ | 189,836 KB |
実行使用メモリ | 160,896 KB |
最終ジャッジ日時 | 2024-07-06 03:23:58 |
合計ジャッジ時間 | 19,566 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 429 ms
82,432 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | AC | 426 ms
82,560 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | AC | 69 ms
16,512 KB |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 478 ms
122,496 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 427 ms
82,688 KB |
testcase_33 | AC | 423 ms
82,560 KB |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 123 ms
35,200 KB |
testcase_39 | WA | - |
testcase_40 | AC | 432 ms
82,688 KB |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | AC | 324 ms
62,976 KB |
testcase_46 | AC | 498 ms
160,512 KB |
testcase_47 | WA | - |
testcase_48 | WA | - |
testcase_49 | AC | 428 ms
82,688 KB |
testcase_50 | WA | - |
testcase_51 | AC | 185 ms
50,944 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; vector<long long> make(int x){ vector<long long> A(1001, 0); A[x] = 1; return A; } vector<long long> operator +(vector<long long> A, vector<long long> B){ vector<long long> C(1001); for (int i = 0; i <= 1000; i++){ C[i] = A[i] + B[i] % MOD; } return C; } vector<long long> operator *(vector<long long> A, long long x){ for (int i = 0; i <= 1000; i++){ A[i] *= x; A[i] %= MOD; } return A; } vector<long long> max_convolution(vector<long long> A, vector<long long> B){ for (int i = 0; i < 1000; i++){ A[i + 1] += A[i]; A[i + 1] %= MOD; B[i + 1] += B[i]; B[i + 1] %= MOD; } vector<long long> C(1001); for (int i = 0; i <= 1000; i++){ C[i] = A[i] * B[i] % MOD; } for (int i = 999; i >= 0; i--){ C[i + 1] += MOD - C[i]; C[i + 1] %= MOD; } return C; } int main(){ int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++){ cin >> A[i]; } vector<vector<int>> E(N); for (int i = 0; i < N - 1; i++){ int u, v; cin >> u >> v; u--; v--; E[u].push_back(v); E[v].push_back(u); } vector<long long> POW(N); POW[0] = 1; for (int i = 0; i < N - 1; i++){ POW[i + 1] = POW[i] * 2 % MOD; } vector<int> p(N, -1); vector<vector<int>> c(N); queue<int> Q; Q.push(0); vector<int> bfs; while (!Q.empty()){ int v = Q.front(); Q.pop(); bfs.push_back(v); for (int w : E[v]){ if (w != p[v]){ p[w] = v; c[v].push_back(w); Q.push(w); } } } reverse(bfs.begin(), bfs.end()); vector<int> sz(N, 1); for (int v : bfs){ for (int w : c[v]){ sz[v] += sz[w]; } } vector<vector<long long>> dp1(N, vector<long long>(1001, 0)); for (int v : bfs){ dp1[v] = make(A[v]); for (int w : c[v]){ dp1[v] = max_convolution(dp1[v], dp1[w]) + dp1[v] * POW[sz[w] - 1]; } } reverse(bfs.begin(), bfs.end()); vector<vector<long long>> dp2(N, vector<long long>(1001, 0)); dp2[0][0] = 1; for (int v : bfs){ int cnt = c[v].size(); vector<vector<long long>> L(cnt + 1, vector<long long>(1001, 0)); L[0][0] = 1; for (int i = 0; i < cnt; i++){ int w = c[v][i]; L[i + 1] = max_convolution(L[i], dp1[w]) + L[i] * POW[sz[w] - 1]; } vector<vector<long long>> R(cnt + 1, vector<long long>(1001, 0)); R[cnt][0] = 1; for (int i = cnt - 1; i >= 0; i--){ int w = c[v][i]; R[i] = max_convolution(R[i + 1], dp1[w]) + R[i + 1] * POW[sz[w] - 1]; } for (int i = 0; i < cnt; i++){ int w = c[v][i]; dp2[w] = max_convolution(dp2[v], max_convolution(L[i], R[i + 1])); dp2[w] = max_convolution(dp2[w], make(A[w])) + make(A[w]) * POW[N - 1 - sz[w]]; } } long long ans = 0; for (int i = 0; i < N; i++){ vector<long long> dp = max_convolution(dp1[i], dp2[i]); for (int j = 0; j <= 1000; j++){ ans += dp[j] * j % MOD; } ans %= MOD; } cout << ans << endl; }