結果

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

#include <atcoder/modint>
using mint = atcoder::modint998244353;

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto &a : A) cin >> a;

    vector<vector<int>> to(N);

    for (int e = 0; e < N - 1; ++e) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        to.at(u).push_back(v);
        to.at(v).push_back(u);
    }

    mint ret = 0;

    auto rec = [&](auto &&self, int now, int prv) -> mint {
        mint w = A.at(now);
        for (int nxt : to.at(now)) {
            if (nxt == prv) continue;
            mint v = self(self, nxt, now);
            ret += v * w;
            w += v * A.at(now);
        }
        return w;
    };

    rec(rec, 0, -1);
    cout << ret.val() << '\n';
}
0