304260: CF812E. Sagheer and Apple Tree
Memory Limit:256 MB
Time Limit:2 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Sagheer and Apple Tree
题意翻译
给定一棵$n$个节点的苹果树,第$i$个点上有$a_i$个苹果。这棵树有一个特殊的性质是:从根到任何叶子的路径长度的奇偶性相同。 Sagheer 和 Soliman 将在树上轮流移动,Soliman 先手,当轮到某个玩家在自己的回合不能移动时这个玩家失败。 每次移动时,两个玩家都可以任意选择一个节点,从中取出至少一个苹果,进行以下两种操作之一: 1. 若选择叶子节点,则吃掉这些苹果 2. 若选择非叶节点,则可以将这些苹果移动到某个子节点。 在每次游戏开始之前,Sagheer 可以对苹果树进行一次更改:选择两个节点$u,v(u\ne v)$然后交换$a_u,a_v$。 假设双方都走最优方案,求出有多少对$(u,v)$满足后手获胜($(u,v)$与$(v,u)$视为一样)。题目描述
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.输入输出格式
输入格式
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.
输出格式
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