結果

問題 No.2949 Product on Tree
ユーザー V_Melville
提出日時 2024-10-26 18:10:53
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 897 bytes
コンパイル時間 5,775 ms
コンパイル使用メモリ 312,128 KB
実行使用メモリ 22,196 KB
最終ジャッジ日時 2024-10-26 18:11:12
合計ジャッジ時間 16,824 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#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() {
    cin.tie(nullptr) -> sync_with_stdio(false);
    
    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;
}
0