Codeforces Round 417 (Div. 2)
E
Sagheer and Apple Tree
Codeforces
2300
原题
← D→
翻译中…

Sagheer is playing a game with his best friend Soliman. He brought a tree with $*n*$ nodes numbered from $1$ to $*n*$ and rooted at node $1$. The $*i*$-th node has $*a*_{*i*}$ apples. This tree has a special property: the lengths of all paths from the root to any leaf have the same parity (i.e. all paths have even length or all paths have odd length).

翻译中…

Sagheer and Soliman will take turns to play. Soliman will make the first move. The player who can't make a move loses.

翻译中…

In each move, the current player will pick a single node, take a non-empty subset of apples from it and do one of the following two things:

翻译中…
  1. eat the apples, if the node is a leaf.
  2. move the apples to one of the children, if the node is non-leaf.
翻译中…

Before Soliman comes to start playing, Sagheer will make exactly one change to the tree. He will pick two different nodes $*u*$ and $*v*$ and swap the apples of $*u*$ with the apples of $*v*$.

翻译中…

Can you help Sagheer count the number of ways to make the swap (i.e. to choose $*u*$ and $*v*$) after which he will win the game if both players play optimally? $(*u*, *v*)$ and $(*v*, *u*)$ are considered to be the same pair.

翻译中…

Input

翻译中…

The first line will contain one integer $*n*$ ($2 ≤ *n* ≤ 10^{5})$ — the number of nodes in the apple tree.

翻译中…

The second line will contain $*n*$ integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ ($1 ≤ *a*_{*i*} ≤ 10^{7}$) — the number of apples on each node of the tree.

翻译中…

The third line will contain $*n* - 1$ integers $*p*_{2}, *p*_{3}, ..., *p*_{*n*}$ ($1 ≤ *p*_{*i*} ≤ *n*$) — the parent of each node of the tree. Node $*i*$ has parent $*p*_{*i*}$ (for $2 ≤ *i* ≤ *n*$). Node $1$ is the root of the tree.

翻译中…

It is guaranteed that the input describes a valid tree, and the lengths of all paths from the root to any leaf will have the same parity.

翻译中…

Output

翻译中…

On a single line, print the number of different pairs of nodes $(*u*, *v*)$, $*u* ≠ *v*$ such that if they start playing after swapping the apples of both nodes, Sagheer will win the game. $(*u*, *v*)$ and $(*v*, *u*)$ are considered to be the same pair.

翻译中…

Note

翻译中…

In the first sample, Sagheer can only win if he swapped node $1$ with node $3$. In this case, both leaves will have $2$ apples. If Soliman makes a move in a leaf node, Sagheer can make the same move in the other leaf. If Soliman moved some apples from a root to a leaf, Sagheer will eat those moved apples. Eventually, Soliman will not find a move.

翻译中…

In the second sample, There is no swap that will make Sagheer win the game.

翻译中…

Note that Sagheer must make the swap even if he can win with the initial tree.

翻译中…

样例

Input #1
3
2 2 3
1 1
Output #1
1
InputCopy OutputCopy
Input #2
3
1 2 3
1 1
Output #2
0
InputCopy OutputCopy
Input #3
8
7 2 2 5 4 3 1 1
1 1 1 4 4 5 6
Output #3
4
InputCopy OutputCopy
题目信息
题号
812E
时间限制
2 s
内存限制
256 MB
标签
博弈论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。