結果

問題 No.1878 union-find の数え上げ
ユーザー HIcoderHIcoder
提出日時 2023-07-23 13:49:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 871 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 114,352 KB
実行使用メモリ 12,552 KB
最終ジャッジ日時 2023-10-24 20:07:13
合計ジャッジ時間 3,172 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
12,552 KB
testcase_01 AC 46 ms
8,064 KB
testcase_02 AC 45 ms
8,064 KB
testcase_03 AC 45 ms
8,064 KB
testcase_04 AC 44 ms
8,064 KB
testcase_05 AC 11 ms
4,632 KB
testcase_06 AC 12 ms
4,632 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 1 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 20 ms
6,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set> 
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
#include <unordered_set>
#include<cassert>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;


int main(){
    int N;
    cin>>N;
    vector<vector<int>> G(N);
    vector<int> p(N);
    for(int i=1;i<N;i++){
        cin>>p[i];
        p[i]--;
        //G[i].push_back(p[i]);
        G[p[i]].push_back(i);
    }

    vector<int> depth(N,0);

    auto dfs=[&](auto f,int now,int d)->void{
        depth[now]=d;
        for(int to:G[now]){
            f(f,to,d+1);
        }
    };

    dfs(dfs,0,0);

    ll ans=1;
    for(int i=0;i<N;i++){
        ans*=max(1,depth[i]);
        ans%=MOD;
    }

    cout<<ans<<endl;

}
0