結果

問題 No.1878 union-find の数え上げ
ユーザー rogi52rogi52
提出日時 2022-10-22 13:30:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 628 bytes
コンパイル時間 2,030 ms
コンパイル使用メモリ 203,500 KB
実行使用メモリ 15,124 KB
最終ジャッジ日時 2023-09-14 08:14:03
合計ジャッジ時間 3,683 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
15,124 KB
testcase_01 AC 24 ms
7,572 KB
testcase_02 AC 27 ms
7,208 KB
testcase_03 AC 28 ms
7,468 KB
testcase_04 AC 29 ms
7,252 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 11 ms
6,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N; cin >> N;
    vector<vector<int>> G(N);
    rep(i,N-1) {
        int p; cin >> p; p--;
        G[p].push_back(i + 1);
    }

    vector<int> depth(N, 0);
    function<void(int)> dfs = [&](int v) -> void {
        for(int to : G[v]) {
            depth[to] = depth[v] + 1;
            dfs(to);
        }
    }; dfs(0);

    ll ans = 1;
    rep(i,N) if(i) ans = (ans * depth[i]) % 998244353;
    cout << ans << endl;
}
0