結果
問題 | No.2214 Products on Tree |
ユーザー |
|
提出日時 | 2023-02-11 05:32:17 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 223 ms / 3,000 ms |
コード長 | 1,404 bytes |
コンパイル時間 | 1,871 ms |
コンパイル使用メモリ | 177,976 KB |
実行使用メモリ | 20,764 KB |
最終ジャッジ日時 | 2024-07-07 21:29:44 |
合計ジャッジ時間 | 7,100 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;using Vi = std::vector<int> ;using VVi = vector<Vi>;using Vl = vector<ll>;ll const m = 998244353;ll mpow(ll a, ll n) {ll ret = 1;while (n) {if (n & 1) {ret = (ret * a) % m;}a = (a * a) % m;n >>= 1;}return ret;}int main () {int N;cin >> N;VVi tr(N);for (int i = 1; i < N; i ++) {int a, b;cin >> a >> b;tr[--a].push_back(--b);tr[b].push_back(a);}queue<int> que;Vi tpr;Vi par(N, -1);par[0] = -2;que.push(0);tpr.push_back(0);while (!que.empty()) {int u = que.front();que.pop();for (auto v : tr[u]) {if (par[v] == -1) {par[v] = u;tpr.push_back(v);que.push(v);}}}reverse(tpr.begin(), tpr.end());Vl dp0(N, 0), dp1(N, 0);for (int i : tpr) {dp0[i] = dp1[i] = 1;for (auto v : tr[i]) {if (par[v] == i) {ll x = dp0[v] + dp1[v];x %= m;dp0[i] *= x;dp0[i] %= m;dp1[i] += (dp1[v] * mpow(x, m - 2)) % m;dp1[i] %= m;}}dp1[i] = (dp1[i] * dp0[i]) % m;}cout << dp1[0] << endl;}