結果
問題 | No.2115 Making Forest Easy |
ユーザー | kwm_t |
提出日時 | 2022-10-29 18:01:12 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 650 ms / 2,000 ms |
コード長 | 3,196 bytes |
コンパイル時間 | 4,051 ms |
コンパイル使用メモリ | 273,088 KB |
実行使用メモリ | 396,540 KB |
最終ジャッジ日時 | 2024-07-06 15:37:43 |
合計ジャッジ時間 | 21,825 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 650 ms
395,776 KB |
testcase_03 | AC | 358 ms
101,592 KB |
testcase_04 | AC | 603 ms
313,600 KB |
testcase_05 | AC | 444 ms
235,584 KB |
testcase_06 | AC | 614 ms
395,920 KB |
testcase_07 | AC | 349 ms
51,244 KB |
testcase_08 | AC | 613 ms
396,032 KB |
testcase_09 | AC | 316 ms
43,648 KB |
testcase_10 | AC | 622 ms
396,416 KB |
testcase_11 | AC | 365 ms
101,268 KB |
testcase_12 | AC | 317 ms
43,648 KB |
testcase_13 | AC | 321 ms
91,040 KB |
testcase_14 | AC | 354 ms
43,644 KB |
testcase_15 | AC | 370 ms
49,276 KB |
testcase_16 | AC | 351 ms
101,584 KB |
testcase_17 | AC | 32 ms
12,460 KB |
testcase_18 | AC | 100 ms
68,576 KB |
testcase_19 | AC | 327 ms
207,356 KB |
testcase_20 | AC | 289 ms
43,844 KB |
testcase_21 | AC | 39 ms
25,472 KB |
testcase_22 | AC | 68 ms
23,468 KB |
testcase_23 | AC | 442 ms
270,592 KB |
testcase_24 | AC | 354 ms
101,568 KB |
testcase_25 | AC | 211 ms
112,256 KB |
testcase_26 | AC | 311 ms
43,776 KB |
testcase_27 | AC | 181 ms
55,464 KB |
testcase_28 | AC | 347 ms
51,540 KB |
testcase_29 | AC | 67 ms
46,080 KB |
testcase_30 | AC | 350 ms
49,460 KB |
testcase_31 | AC | 547 ms
309,988 KB |
testcase_32 | AC | 626 ms
396,540 KB |
testcase_33 | AC | 617 ms
395,716 KB |
testcase_34 | AC | 41 ms
14,696 KB |
testcase_35 | AC | 135 ms
91,392 KB |
testcase_36 | AC | 630 ms
395,904 KB |
testcase_37 | AC | 173 ms
52,496 KB |
testcase_38 | AC | 91 ms
29,900 KB |
testcase_39 | AC | 252 ms
162,308 KB |
testcase_40 | AC | 346 ms
49,160 KB |
testcase_41 | AC | 312 ms
43,600 KB |
testcase_42 | AC | 357 ms
101,352 KB |
testcase_43 | AC | 158 ms
28,848 KB |
testcase_44 | AC | 91 ms
29,548 KB |
testcase_45 | AC | 265 ms
39,024 KB |
testcase_46 | AC | 352 ms
101,352 KB |
testcase_47 | AC | 141 ms
24,912 KB |
testcase_48 | AC | 542 ms
299,588 KB |
testcase_49 | AC | 562 ms
321,832 KB |
testcase_50 | AC | 216 ms
63,888 KB |
testcase_51 | AC | 145 ms
42,548 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair<long long,long long> template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } struct ReRooting { struct Edge { int to; Edge(int _to) :to(_to) {} }; using arr = array<mint, 1001>; vector<int>a; vector<vector<Edge>>g; vector<arr>dp, ans; arr e(int x = -1) { arr ret; rep(i, 1001)ret[i] = 0; if (x >= 0)ret[x] = 1; return ret; } int sz; ReRooting(int _sz) :sz(_sz) { a.resize(sz); g.resize(sz); dp.resize(sz); ans.resize(sz); } void ReadGraph() { rep(i, sz - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back({ b }); g[b].push_back({ a }); } } arr mergeSub(const arr &lh, const arr &rh) { arr ret = e(); arr lsub = lh, rsub = rh; rep2(i, 1, 1001)rsub[0] += rsub[i]; rep2(i, 1, lsub.size())lsub[i] = lsub[i - 1] + lh[i]; rep2(i, 1, rsub.size())rsub[i] = rsub[i - 1] + rh[i]; rep(i, ret.size()) ret[i] = lsub[i] * rsub[i]; rrep2(i, 1, ret.size())ret[i] -= ret[i - 1]; return ret; } arr merge(const arr &lh, const arr &rh) { arr ret = e(); arr lsub = lh, rsub = rh; rep2(i, 1, lsub.size())lsub[i] = lsub[i - 1] + lh[i]; rep2(i, 1, rsub.size())rsub[i] = rsub[i - 1] + rh[i]; rep(i, ret.size()) ret[i] = lsub[i] * rsub[i]; rrep2(i, 1, ret.size())ret[i] -= ret[i - 1]; return ret; } void dfs(int v, int p = -1) { auto sub = e(0); for (auto e : g[v]) { if (p == e.to)continue; dfs(e.to, v); sub = mergeSub(sub, dp[e.to]); } dp[v] = e(a[v]); dp[v] = merge(dp[v], sub); } void dfsR(int v, int p = -1) { ans[v] = dp[v]; if (-1 != p) ans[v] = mergeSub(ans[v], dp[p]); vector<arr>vdata; for (auto e : g[v]) vdata.push_back(dp[e.to]); int sz = vdata.size(); vector<arr>sumL(sz + 1), sumR(sz + 1); sumL[0] = e(0), sumR[0] = e(0); rep(i, sz)sumL[i + 1] = mergeSub(sumL[i], vdata[i]); rep(i, sz)sumR[i + 1] = mergeSub(sumR[i], vdata[sz - i - 1]); for (int i = 0; i < g[v].size(); ++i) { auto nv = g[v][i].to; if (p == nv)continue; auto chi = merge(sumL[i], sumR[sz - (i + 1)]); arr sub = e(a[v]); dp[v] = merge(sub, chi); dfsR(nv, v); } } void output() { mint ret = 0; rep(i, sz) rep(j, 1001) ret += ans[i][j] * j; cout << ret.val() << endl; } void solve() { rep(i, sz)cin >> a[i]; ReadGraph(); dfs(0); dfsR(0); output(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; ReRooting reroot(n); reroot.solve(); return 0; }