結果

問題 No.1878 union-find の数え上げ
ユーザー 👑 colognecologne
提出日時 2022-03-17 16:43:42
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 494 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 130,912 KB
実行使用メモリ 14,796 KB
最終ジャッジ日時 2024-04-14 12:06:17
合計ジャッジ時間 2,278 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
14,796 KB
testcase_01 AC 44 ms
6,988 KB
testcase_02 AC 42 ms
7,040 KB
testcase_03 AC 43 ms
7,040 KB
testcase_04 AC 42 ms
7,168 KB
testcase_05 AC 11 ms
6,940 KB
testcase_06 AC 12 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 19 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <iostream>
#include <optional>
#include <vector>
#include <atcoder/modint>

using namespace std;
using Mint = atcoder::modint998244353;

int main()
{
	int N;
	cin >> N;
	vector<vector<int>> adj(N);
	for (int i = 1; i < N; i++)
	{
		int v;
		cin >> v;
		--v;
		adj[v].push_back(i);
	}
	function<Mint(int, int)> dfs = [&](int a, int h)
	{
		Mint ans = max(h, 1);
		for (auto b : adj[a])
			ans *= dfs(b, h + 1);
		return ans;
	};
	cout << dfs(0, 0).val() << endl;
}
0