A11040 | Sagheer and Apple Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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.
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.
输出格式
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.
输入输出样例
输入 #1
3 2 2 3 1 1
输出 #1
1
输入 #2
3 1 2 3 1 1
输出 #2
0
输入 #3
8 7 2 2 5 4 3 1 1 1 1 1 4 4 5 6
输出 #3
4
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted