結果

問題 No.1878 union-find の数え上げ
ユーザー cureskolcureskol
提出日時 2022-05-30 14:27:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,368 bytes
コンパイル時間 2,376 ms
コンパイル使用メモリ 216,596 KB
実行使用メモリ 11,856 KB
最終ジャッジ日時 2023-10-21 00:20:29
合計ジャッジ時間 3,199 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
11,856 KB
testcase_01 AC 21 ms
7,684 KB
testcase_02 AC 23 ms
7,684 KB
testcase_03 AC 23 ms
7,684 KB
testcase_04 AC 23 ms
7,684 KB
testcase_05 AC 9 ms
6,628 KB
testcase_06 AC 9 ms
6,628 KB
testcase_07 AC 4 ms
5,928 KB
testcase_08 AC 4 ms
5,928 KB
testcase_09 AC 4 ms
5,928 KB
testcase_10 AC 4 ms
5,928 KB
testcase_11 AC 4 ms
5,928 KB
testcase_12 AC 5 ms
5,928 KB
testcase_13 AC 4 ms
5,928 KB
testcase_14 AC 10 ms
6,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL
 #include <debug>
#else
 #define debug(...) void(0)
#endif

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

template<typename T>
istream& operator>>(istream&is,vector<T>&v){
  for(T&p:v)is>>p;
  return is;
}
template<typename T>
ostream& operator<<(ostream&os,const vector<T>&v){
  if(&os==&cerr)os<<"[";
  for(int i=0;i<v.size();i++){
    os<<v[i];
    if(i+1<v.size())os<<(&os==&cerr?",":" ");
  }
  if(&os==&cerr)os<<"]";
  return os;
}

template<typename T,T MOD=998244353>
struct Mint{
  inline static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0)v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1)res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator+() const{return *this;}
  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }

  friend ostream& operator<<(ostream&os,const Mint &m){os<<m.v;return os;}
  friend istream& operator>>(istream&is,Mint &m){is>>m.v;m.v%=MOD;if(m.v<0)m.v+=MOD;return is;}
};

using ll=long long;
using mint=Mint<ll>;

mint ans=1;
vector<vector<int>> g(123456);

void dfs(int idx,int pre,int now){
  ans*=max(1,now);
  for(int p:g[idx])
    dfs(p,idx,now+1);
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;cin>>n;
  REP(i,n-1){
    int p;cin>>p;p--;
    g[p].push_back(i+1);
  }
  dfs(0,-1,0);
  cout<<ans<<endl;
}
0