A12521 | Leaf Partition
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a rooted tree with $n$ nodes, labeled from $1$ to $n$ . The tree is rooted at node $1$ . The parent of the $i$ -th node is $p_i$ . A leaf is node with no children. For a given set of leaves $L$ , let $f(L)$ denote the smallest connected subgraph that contains all leaves $L$ .
You would like to partition the leaves such that for any two different sets $x, y$ of the partition, $f(x)$ and $f(y)$ are disjoint.
Count the number of ways to partition the leaves, modulo $998244353$ . Two ways are different if there are two leaves such that they are in the same set in one way but in different sets in the other.
You would like to partition the leaves such that for any two different sets $x, y$ of the partition, $f(x)$ and $f(y)$ are disjoint.
Count the number of ways to partition the leaves, modulo $998244353$ . Two ways are different if there are two leaves such that they are in the same set in one way but in different sets in the other.
输入格式
The first line contains an integer $n$ ( $2 \leq n \leq 200\,000$ ) — the number of nodes in the tree.
The next line contains $n-1$ integers $p_2, p_3, \ldots, p_n$ ( $1 \leq p_i < i$ ).
The next line contains $n-1$ integers $p_2, p_3, \ldots, p_n$ ( $1 \leq p_i < i$ ).
输出格式
Print a single integer, the number of ways to partition the leaves, modulo $998244353$ .
输入输出样例
输入 #1
5 1 1 1 1
输出 #1
12
输入 #2
10 1 2 3 4 5 6 7 8 9
输出 #2
1
In the first example, the leaf nodes are $2,3,4,5$ . The ways to partition the leaves are in the following image 
In the second example, the only leaf is node $10$ so there is only one partition. Note that node $1$ is not a leaf.
In the second example, the only leaf is node $10$ so there is only one partition. Note that node $1$ is not a leaf.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted