結果

問題 No.1878 union-find の数え上げ
ユーザー ruthen71ruthen71
提出日時 2022-03-18 23:13:08
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,201 bytes
コンパイル時間 1,664 ms
コンパイル使用メモリ 180,276 KB
実行使用メモリ 19,372 KB
最終ジャッジ日時 2024-10-03 09:44:43
合計ジャッジ時間 2,622 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;
#include <atcoder/modint>
using namespace atcoder;
using mint = modint998244353;
ostream &operator<<(ostream &os, const mint &p) { return os << p.val(); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
V<int> p(N);
rep(i, N - 1) {
cin >> p[i + 1];
p[i + 1]--;
}
show(p);
V<V<int>> G(N);
for (int i = 1; i < N; i++) {
G[i].push_back(p[i]);
G[p[i]].push_back(i);
}
V<V<mint>> dp(N, V<mint>(2, 1));
auto rec = [&](auto f, int cur, int par) -> void {
if (G[cur].size() == 1 && par != -1) return;
for (auto &nx : G[cur]) {
if (nx == par) continue;
f(f, nx, cur);
dp[cur][1] *= (dp[nx][0] + dp[nx][1]);
dp[cur][0] *= dp[nx][0];
}
dp[cur][1] -= dp[cur][0];
};
rec(rec, 0, -1);
mint ans = dp[0][0] + dp[0][1];
cout << ans << '\n';
show(dp);
show(G);
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0