結果

問題 No.3449 Mex of Subtree
コンテスト
ユーザー warner1129
提出日時 2026-02-23 23:01:40
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 1,713 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,556 ms
コンパイル使用メモリ 341,584 KB
実行使用メモリ 100,608 KB
最終ジャッジ日時 2026-02-23 23:01:49
合計ジャッジ時間 6,507 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;
using u64 = unsigned long long;

template<ranges::range T> requires (!is_convertible_v<T, string_view>)
ostream &operator<<(ostream &s, T &&v) { 
    for (auto &&x : v) s << x << ' '; 
    return s; 
}

template<class... T> 
void dbg(T... x) {
#ifdef LOCAL
    char e{};
    ((cerr << e << x, e = ' '), ...);
    cerr << '\n';
#endif
}
#define debug(x...) dbg(#x, '=', x)

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second

bool chmin(auto &&a, auto b) { return (b < a) and (a = b, true); }
bool chmax(auto &&a, auto b) { return (a < b) and (a = b, true); }

constexpr i64 mod = 998244353;
constexpr i64 inf = 1E10;

vector<i64> operator*(const vector<i64> &a, const vector<i64> &b) {
    int n = a.size();
    int m = b.size();
    vector<i64> c(n + m - 1);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            (c[i + j] += a[i] * b[j]) %= mod;
        }
    }
    return c;
}

void solve() {
    int N;
    cin >> N;

    vector<int> pa(N, -1);
    for (int i = 1; i < N; i++) {
        cin >> pa[i];
        pa[i]--;
    }

    vector dp(N, vector<i64>{1, 0});
    for (int i = N - 1; i >= 0; i--) {
        for (int j = 1; j < dp[i].size(); j++) {
            (dp[i][j] += dp[i][j - 1]) %= mod;
        }
        if (pa[i] != -1) {
            dp[pa[i]] = dp[pa[i]] * dp[i];
        }
    }

    i64 ans = reduce(all(dp[0]), 0LL) % mod;
    cout << ans << '\n';

}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}
0