【题解】P11863 「o.OI R1」CX

· · 题解

只选 1 是一种方案。

不选 1,则对于 1 的每一个邻点,以它为根的子树的方案是独立的。

假设这棵子树的根节点为 u。在这棵子树中我们只能选一个点,否则边 (1,u) 会被覆盖两次。选点时,只能选取 u 延伸出的最长不分叉链中的某个节点,否则会有边没覆盖到。

利用乘法原理即可得到答案。

// ID: @BpbjsGreen

#include <bits/stdc++.h>
#define int long long
// #define double long double
using namespace std;

const int N = 5e5 + 5, mod = 998244353;
int n, res = 0, ans = 1, tmp;
vector<int> G[N];

signed main()
{
    scanf("%lld", &n);
    for (int i = 2, fa; i <= n; i++)
        scanf("%lld", &fa), G[fa].push_back(i);
    for (int u : G[1])
    {
        res = 1, tmp = u;
        while (G[tmp].size() == 1)
            res++, tmp = G[tmp][0];
        ans = ans * res % mod;
    }
    printf("%lld\n", (ans + 1) % mod);

    return 0;
}