結果
問題 | No.2949 Product on Tree |
ユーザー |
|
提出日時 | 2024-10-26 18:09:17 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 342 ms / 2,000 ms |
コード長 | 844 bytes |
コンパイル時間 | 5,913 ms |
コンパイル使用メモリ | 312,228 KB |
実行使用メモリ | 22,080 KB |
最終ジャッジ日時 | 2024-10-26 18:09:41 |
合計ジャッジ時間 | 23,270 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#include <bits/stdc++.h>#if __has_include(<atcoder/all>)#include <atcoder/all>using namespace atcoder;#endif#define rep(i, n) for (int i = 0; i < (n); ++i)using namespace std;using mint = modint998244353;int main() {int n;cin >> n;vector<int> a(n);rep(i, n) cin >> a[i];vector<vector<int>> to(n);rep(i, n-1) {int u, v;cin >> u >> v;--u; --v;to[u].push_back(v);to[v].push_back(u);}mint ans;vector<mint> dp(n);auto dfs = [&](auto& f, int v, int p=-1) -> void {dp[v] = a[v];for (int u : to[v]) {if (u == p) continue;f(f, u, v);ans += dp[v]*dp[u];dp[v] += dp[u]*a[v];}};dfs(dfs, 0);cout << ans.val() << '\n';return 0;}