結果

問題 No.1878 union-find の数え上げ
ユーザー hiro71687khiro71687k
提出日時 2023-04-12 17:56:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,194 bytes
コンパイル時間 4,435 ms
コンパイル使用メモリ 260,056 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2024-04-17 05:28:12
合計ジャッジ時間 5,798 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 WA -
testcase_14 AC 23 ms
7,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=144494;
ll mod=998244353;
int main(){
    ll  n;
    cin >> n;
    if (n==1)
    {
        cout << 1 << endl;
        return 0;
    }
    
    vector<vector<ll>>g(n);
    for (ll i = 0; i < n-1; i++)
    {
        ll a;
        cin >> a;
        a--;
        g[a].push_back(i+1);
    }
    vector<ll>memo(n,inf);
    memo[0]=0;
    queue<ll>que;
    que.push(0);
    while (!que.empty())
    {
        ll v=que.front();
        que.pop();
        for (ll i = 0; i < g[v].size(); i++)
        {
            if (memo[g[v][i]]>memo[v]+1)
            {
                memo[g[v][i]]=memo[v]+1;
                que.push(g[v][i]);
            }
        }
    }
    ll ans=1;
    for (ll i = 0; i < n; i++)
    {
        bool ok=true;
        for (ll j = 0; j < g[i].size(); j++)
        {
            if (memo[g[i][j]]>memo[i])
            {
                ok=false;
                break;
            }
        }
        if (ok)
        {
            ans*=memo[i];
            ans%=mod;
        }
    }
    cout << ans << endl;
}
0