結果
問題 | No.2949 Product on Tree |
ユーザー |
|
提出日時 | 2024-10-27 10:33:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 200 ms / 2,000 ms |
コード長 | 823 bytes |
コンパイル時間 | 4,382 ms |
コンパイル使用メモリ | 256,580 KB |
最終ジャッジ日時 | 2025-02-25 00:47:16 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/all>using namespace std;using mint = atcoder::modint998244353;int main(){ios::sync_with_stdio(false);cin.tie(0);int n, u, v;cin >> n;vector<mint> a(n);for(int i = 0; i < n; i++){cin >> v;a[i] = v;}vector<vector<int>> g(n);for(int i = 1; i < n; i++){cin >> u >> v;g[--u].emplace_back(--v);g[v].emplace_back(u);}mint ans;auto dfs = [&](auto dfs, int v, int p = -1) -> mint {mint s0, s1;for(auto &&u : g[v]){if(u == p) continue;auto rc = dfs(dfs, u, v);s1 += s0 * rc;s0 += rc;}ans += (s0 + s1) * a[v];return ++s0 * a[v];};dfs(dfs, 0);cout << ans.val() << '\n';}