跳到正文

题解

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

BpbjsGreen约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月16日
篇幅
约 1 分钟
获赞
1
题目
P11863

版本

  1. v1当前

只选 11 是一种方案。

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

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

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

C++
// 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;
}
原文