#P10121. 『STA - R4』保险丝

    ID: 11448 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>暴力数据结构线段树O2优化树论虚树其它技巧

『STA - R4』保险丝

Background

APJ: "? Why is the fuse in my house gone again?"

Problem Description

You are given a rooted tree with nn nodes, where the root is node 11.

Define the distance between two node sets S1,S2S_1, S_2 as the minimum distance between a node chosen from each set, i.e., $\displaystyle\operatorname{dist}(S_1,S_2)=\min_{\substack{u\in S_1\\v\in S_2}}\operatorname{dist}(u,v)$, where dist(u,v)\operatorname{dist}(u,v) is the distance between nodes uu and vv.

Define path(u,v)\operatorname{path}(u,v) as the set of all nodes on the simple path from uu to vv. Let L\mathcal L be the set of all leaves.

For a fixed positive integer uu, define the set of nodes vv that satisfy the following conditions to be the semi-neighborhood of uu, denoted by U˚(u)\mathring U(u):

  • vv is in the subtree of uu.
  • $\operatorname{dist}(u,v)\le\operatorname{dist}(\operatorname{path}(1,v),\mathcal L)$.

That is, the semi-neighborhood U˚(u)\mathring U(u) contains all nodes vv in the subtree of uu such that the distance from vv to uu is no more than the distance from any node on the path from vv to the root to its nearest leaf node.

Then define:

$$f(x)=\sum_{u\in\mathring U(x)}\prod_{\substack{v\in\operatorname{subtree}(u)\\v\in\mathring U(x)}}F_{\deg v}$$

where subtree(u)\operatorname{subtree}(u) is the set of all nodes in the subtree of uu, degu\deg u is the degree of uu (the number of nodes adjacent to uu), and FF is the Fibonacci sequence:

$$F_n=\begin{cases}1&n\le 2\\F_{n-1}+F_{n-2}&n\ge 3\end{cases}$$

In other words, f(x)f(x) is the sum of contributions of nodes in the semi-neighborhood of xx. The contribution of a node uu to xx is computed as follows: take every node vv in the subtree of uu that is also in the semi-neighborhood of xx; if the degree of vv is dd, then multiply the contribution of uu by FdF_d. The final result is the sum of contributions of all such uu.

You need to compute f(1),f(2),,f(n)f(1), f(2), \cdots, f(n). To reduce the output size, you only need to output the XOR of these values modulo 994007158994007158, i.e., x=1n(f(x)mod994007158)\bigoplus_{x=1}^n(f(x)\bmod 994007158).

Input Format

The first line contains a positive integer nn, the number of nodes.

The second line contains n1n - 1 positive integers. The ii-th integer represents the parent of node i+1i + 1.

Output Format

One line, the value of x=1n(f(x)mod994007158)\bigoplus_{x=1}^n(f(x)\bmod 994007158).

7
1 1 2 2 3 3

8
14
1 2 3 3 2 6 6 6 9 9 10 11 12
25

Hint

Sample Explanation

In the first sample, the values of ff at 171\dots 7 are: 8,2,2,1,1,1,18,2,2,1,1,1,1.

In the second sample, the values of ff at 1141\dots14 are: 4,17,2,1,1,8,1,1,4,2,1,1,1,14,17,2,1,1,8,1,1,4,2,1,1,1,1.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (10 pts): n5000n\le 5000.
  • Subtask 2 (20 pts): the number of leaves in the tree is at most 3030.
  • Subtask 3 (20 pts): there is no node in the tree that has exactly one child.
  • Subtask 4 (50 pts): no special restrictions.

For all testdata, 2n,q1062\le n,q\le 10^6, and for each non-root node, its parent index is smaller than its own index.

Translated by ChatGPT 5