結果
問題 | No.2949 Product on Tree |
ユーザー | miiitomi |
提出日時 | 2024-10-25 22:07:06 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 197 ms / 2,000 ms |
コード長 | 879 bytes |
コンパイル時間 | 3,133 ms |
コンパイル使用メモリ | 253,908 KB |
実行使用メモリ | 27,500 KB |
最終ジャッジ日時 | 2024-10-25 22:07:29 |
合計ジャッジ時間 | 12,941 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 2e+18; const ll MOD = 998244353; ll N; vector<ll> A; vector<vector<int>> G; ll ans = 0; vector<ll> dp; void dfs(int u, int p) { dp[u] = A[u]; for (int v : G[u]) { if (v == p) continue; dfs(v, u); ans = (ans + dp[u]*dp[v]%MOD)%MOD; dp[u] = (dp[u] + dp[v]*A[u]%MOD)%MOD; } } void solve() { cin >> N; A.resize(N); for (ll &a : A) cin >> a; G.resize(N); for (int i = 0; i < N-1; i++) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } dp.resize(N); dfs(0, -1); cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int T = 1; // cin >> T; while (T--) solve(); }