結果
問題 | No.1878 union-find の数え上げ |
ユーザー | tnakao0123 |
提出日時 | 2022-03-20 00:04:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 27 ms / 2,000 ms |
コード長 | 875 bytes |
コンパイル時間 | 338 ms |
コンパイル使用メモリ | 52,176 KB |
実行使用メモリ | 9,712 KB |
最終ジャッジ日時 | 2024-10-05 12:09:09 |
合計ジャッジ時間 | 1,485 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 13 |
ソースコード
/* -*- coding: utf-8 -*- * * 1878.cc: No.1878 union-find の数え上げ - yukicoder */ #include<cstdio> #include<vector> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 100000; const int MOD = 998244353; /* typedef */ typedef long long ll; typedef vector<int> vi; /* global variables */ vi nbrs[MAX_N]; int ps[MAX_N], cis[MAX_N], ds[MAX_N]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); ps[0] = -1; for (int i = 1; i < n; i++) { scanf("%d", ps + i), ps[i]--; nbrs[ps[i]].push_back(i); } int x = 1; ds[0] = 0; for (int u = 0; u >= 0;) { vi &nbru = nbrs[u]; if (cis[u] < nbru.size()) { int v = nbru[cis[u]++]; ds[v] = ds[u] + 1; u = v; } else { if (ds[u] > 1) x = (ll)x * ds[u] % MOD; u = ps[u]; } } printf("%d\n", x); return 0; }