結果

問題 No.1878 union-find の数え上げ
ユーザー ruthenruthen
提出日時 2022-03-19 00:14:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,065 bytes
コンパイル時間 1,902 ms
コンパイル使用メモリ 174,556 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-04-14 12:58:52
合計ジャッジ時間 3,042 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
18,816 KB
testcase_01 AC 43 ms
15,232 KB
testcase_02 AC 44 ms
15,016 KB
testcase_03 AC 43 ms
15,232 KB
testcase_04 AC 43 ms
14,816 KB
testcase_05 AC 11 ms
6,944 KB
testcase_06 AC 13 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 26 ms
15,192 KB
権限があれば一括ダウンロードができます

ソースコード

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]--;
    }
    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<int> d(N, 0);
    V<V<mint>> dp(N, V<mint>(2, 1));
    auto rec = [&](auto f, int cur, int par) -> void {
        for (auto &nx : G[cur]) {
            if (nx == par) continue;
            d[nx] = d[cur] + 1;
            f(f, nx, cur);
        }
    };
    rec(rec, 0, -1);
    mint ans = 1;
    for (int i = 1; i < N; i++) ans *= d[i];
    cout << ans << '\n';
    return 0;
}
0