結果

問題 No.1878 union-find の数え上げ
ユーザー cologne
提出日時 2022-03-17 16:43:42
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 494 bytes
コンパイル時間 830 ms
コンパイル使用メモリ 131,680 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2024-10-03 09:23:20
合計ジャッジ時間 2,000 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

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