QOJ.ac

QOJ

時間限制: 2.0 s 記憶體限制: 512 MB 總分: 100 可 Hack ✓

#9367. 卡林图

统计

小乔治·卡林(George Khalin)设计了一种新型图。他不喜欢字母 K,所以他把它去掉了。

树的先序遍历(有时称为 time-in 序)通过以下过程获得: 固定树的根,并将所有边指向远离根的方向。顶点 $v$ 子树的先序遍历是 $v$ 后面跟着其所有子节点(如果有)子树的先序遍历(按某种顺序)。固定根的树的先序遍历是根子树的任意一种先序遍历。

注意,同一棵树可能有多种先序遍历,因为先序遍历取决于根的选择,以及在每个顶点处考虑子树的顺序。

Halin 图是通过以下过程获得的图: 存在一棵我们称之为图的基树(base tree)的树,它至少有 4 个顶点,且没有度数为 2 的顶点。指定了它的一种先序遍历。相对于此先序遍历,树的根不是叶子节点。

设 $v_1, v_2, \dots, v_m$ 为树中按先序遍历顺序出现的叶子节点。对于每个 $i$ 从 1 到 $m$,在顶点 $v_i$ 和 $v_{(i \bmod m)+1}$ 之间添加一条边。这些边被称为附加边。所得的图即为相对于该基树和指定先序遍历的 Halin 图。

图 $G$ 的 3-匹配(3-matching)是一个边集 $S$,使得从 $G$ 中移除所有不在 $S$ 中的边后,剩余图的连通分量均为大小为 3 或 1 的树。

给定一个 Halin 图,求其 3-匹配的数量,结果对 $998\,244\,353$ 取模。

输入格式

第一行包含一个整数 $n$ ($4 \le n \le 10^5$),表示基树的顶点数。顶点根据先序遍历进行编号。

第二行包含 $n - 1$ 个整数。其中第 $i$ 个整数为 $p_i$ ($1 \le p_i \le i$),描述了基树中第 $p_i$ 个顶点与第 $i + 1$ 个顶点之间的一条边。

保证基树是一棵树,没有度数为 2 的顶点,且顶点 1 不是叶子节点。

输出格式

输出一个整数,表示给定图的 3-匹配数量,结果对 $998\,244\,353$ 取模。

样例

样例输入 1

4
1 1 1

样例输出 1

13

样例输入 2

6
1 1 3 3 1

样例输出 2

34

样例输入 3

11
1 1 3 4 4 3 3 1 9 9

样例输出 3

737

说明

在第一个样例中,实际的 Halin 图是四个顶点的完全图。

在第二个样例中,叶子节点为 $[2, 4, 5, 6]$,因此有四条附加边 —— $(2, 4), (4, 5), (5, 6), (6, 2)$。

注意,题目描述的有意义部分中没有字母 K。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.