結果
問題 | No.2949 Product on Tree |
ユーザー | Tatsu_mr |
提出日時 | 2024-10-25 22:20:30 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 606 ms / 2,000 ms |
コード長 | 3,786 bytes |
コンパイル時間 | 5,933 ms |
コンパイル使用メモリ | 321,312 KB |
実行使用メモリ | 92,172 KB |
最終ジャッジ日時 | 2024-10-25 22:21:03 |
合計ジャッジ時間 | 32,764 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
6,812 KB |
testcase_01 | AC | 3 ms
6,816 KB |
testcase_02 | AC | 3 ms
6,820 KB |
testcase_03 | AC | 513 ms
78,352 KB |
testcase_04 | AC | 508 ms
76,672 KB |
testcase_05 | AC | 514 ms
78,468 KB |
testcase_06 | AC | 518 ms
78,676 KB |
testcase_07 | AC | 505 ms
76,936 KB |
testcase_08 | AC | 522 ms
79,220 KB |
testcase_09 | AC | 526 ms
79,092 KB |
testcase_10 | AC | 522 ms
79,232 KB |
testcase_11 | AC | 564 ms
80,044 KB |
testcase_12 | AC | 529 ms
81,532 KB |
testcase_13 | AC | 543 ms
81,428 KB |
testcase_14 | AC | 529 ms
81,872 KB |
testcase_15 | AC | 543 ms
83,812 KB |
testcase_16 | AC | 564 ms
82,612 KB |
testcase_17 | AC | 553 ms
83,672 KB |
testcase_18 | AC | 547 ms
83,940 KB |
testcase_19 | AC | 567 ms
85,860 KB |
testcase_20 | AC | 579 ms
85,888 KB |
testcase_21 | AC | 553 ms
86,656 KB |
testcase_22 | AC | 543 ms
85,300 KB |
testcase_23 | AC | 554 ms
79,904 KB |
testcase_24 | AC | 534 ms
79,992 KB |
testcase_25 | AC | 534 ms
79,984 KB |
testcase_26 | AC | 547 ms
80,012 KB |
testcase_27 | AC | 531 ms
79,984 KB |
testcase_28 | AC | 537 ms
80,124 KB |
testcase_29 | AC | 533 ms
80,348 KB |
testcase_30 | AC | 568 ms
80,676 KB |
testcase_31 | AC | 541 ms
81,780 KB |
testcase_32 | AC | 548 ms
82,028 KB |
testcase_33 | AC | 572 ms
84,944 KB |
testcase_34 | AC | 574 ms
88,824 KB |
testcase_35 | AC | 570 ms
86,484 KB |
testcase_36 | AC | 571 ms
90,192 KB |
testcase_37 | AC | 606 ms
90,596 KB |
testcase_38 | AC | 572 ms
87,952 KB |
testcase_39 | AC | 575 ms
88,396 KB |
testcase_40 | AC | 599 ms
91,984 KB |
testcase_41 | AC | 579 ms
89,736 KB |
testcase_42 | AC | 575 ms
92,172 KB |
testcase_43 | AC | 233 ms
62,812 KB |
testcase_44 | AC | 234 ms
63,376 KB |
testcase_45 | AC | 308 ms
79,832 KB |
testcase_46 | AC | 270 ms
71,764 KB |
testcase_47 | AC | 196 ms
53,588 KB |
testcase_48 | AC | 276 ms
74,500 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> #define For(i, a, b) for(long long i = a; i < b; i++) #define rep(i, n) For(i, 0, n) #define rFor(i, a, b) for(long long i = a; i >= b; i--) #define ALL(v) (v).begin(), (v).end() #define rALL(v) (v).rbegin(), (v).rend() using namespace std; using lint = long long; using ld = long double; using mint = atcoder::modint998244353; int INF = 2000000000; lint LINF = 1000000000000000000; template <class T> struct Edge { int from, to; T cost; int idx; Edge() {} Edge(int to_) : to(to_) {} Edge(int to_, T cost_) : to(to_), cost(cost_) {} Edge(int from_, int to_, int idx_) : from(from_), to(to_), idx(idx_) {} Edge(int from_, int to_, T cost_, int idx_) : from(from_), to(to_), cost(cost_), idx(idx_) {} }; template <class T> using Graph = vector<vector<Edge<T>>>; using graph = Graph<long long>; using edge = Edge<long long>; #define add emplace_back template <class T, class E, T (*merge)(T, T), T (*add_edge)(Edge<E>, T), T (*add_node)(int, T), T (*id)()> struct RerootingDP { private: int n; vector<vector<Edge<E>>> g; vector<vector<T>> dp, l, r; T dfs1(int v, int pv) { T res = id(); for (int i = 0; i < (int)g[v].size(); i++) { int nv = g[v][i].to; if (nv == pv) { continue; } dp[v][i] = dfs1(nv, v); res = merge(add_edge(g[v][i], dp[v][i]), res); } return add_node(v, res); } void dfs2(int v, int pv, T num) { int sz = g[v].size(); for (int i = 0; i < sz; i++) { int nv = g[v][i].to; if (nv == pv) { dp[v][i] = add_node(pv, num); } } l[v][0] = r[v][sz] = id(); for (int i = 0; i < sz; i++) { l[v][i + 1] = merge(add_edge(g[v][i], dp[v][i]), l[v][i]); r[v][sz - i - 1] = merge(add_edge(g[v][sz - i - 1], dp[v][sz - i - 1]), r[v][sz - i]); } for (int i = 0; i < sz; i++) { int nv = g[v][i].to; if (nv == pv) { continue; } T nnum = merge(l[v][i], r[v][i + 1]); dfs2(nv, v, nnum); } } public: RerootingDP(vector<vector<Edge<E>>> g_) : n(g_.size()), g(g_), dp(n), l(n), r(n) { for (int i = 0; i < n; i++) { dp[i].resize(g[i].size()); l[i].resize(g[i].size() + 1); r[i].resize(g[i].size() + 1); } dfs1(0, -1); dfs2(0, -1, id()); } T get(int v) { T res = id(); for (int i = 0; i < (int)g[v].size(); i++) { res = merge(add_edge(g[v][i], dp[v][i]), res); } return add_node(v, res); } void dump() { for (int v = 0; v < n; v++) { for (int i = 0; i < (int)g[v].size(); i++) { cout << v + 1 << "->" << g[v][i].to + 1 << " : "; cout << dp[v][i]; cout << endl; } } } }; vector<mint> A(200010); mint merge(mint a, mint b) { return a + b; } mint add_edge(Edge<int> e, mint a) { return a; } mint add_node(int v, mint a) { return A[v] * a + A[v]; } mint id() { return mint(0); } int main() { int n; cin >> n; rep(i, n) { int x; cin >> x; A[i] = mint(x); } Graph<int> g(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--; v--; g[u].add(v); g[v].add(u); } RerootingDP<mint, int, merge, add_edge, add_node, id> rd(g); mint ans = mint(0); rep(i, n) { ans += rd.get(i); } ans -= accumulate(ALL(A), mint(0)); ans /= mint(2); cout << ans.val() << endl; }