結果

問題 No.1878 union-find の数え上げ
ユーザー 👑 colognecologne
提出日時 2022-03-17 16:27:28
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 685 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 132,076 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2024-04-14 12:06:03
合計ジャッジ時間 2,010 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

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<pair<Mint, Mint>(int, int)> dfs = [&](int a, int h)
	{
		Mint root = 1, rem = 1;
		// case rem, child should all be rem
		// case root, n/c about child
		for (auto b : adj[a])
		{
			auto [bro, bre] = dfs(b, h + 1);
			rem *= bre;
			root *= bre + bro;
		}
		if (h <= 1)
			rem = 0;
		return make_pair(root, rem);
	};
	cout << dfs(0, 0).first.val() << endl;
}
0