CF812E.Sagheer and Apple Tree
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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:
- eat the apples, if the node is a leaf.
- 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.
萨赫尔正在和他最好的朋友索利曼玩一个游戏。他带来了一棵有 n 个节点的树,节点编号为 1 到 n,根节点为 1。第 i 个节点上有 ai 个苹果。这棵树具有一个特殊性质:从根节点到任意叶节点的所有路径长度具有相同的奇偶性(即所有路径长度均为偶数,或均为奇数)。
萨赫尔和索利曼将轮流进行游戏,索利曼先手。无法进行操作的玩家判负。
每次操作中,当前玩家需选择一个节点,并从中取走一个非空的苹果子集,然后执行以下两种操作之一:
- 若该节点是叶节点,则吃掉这些苹果;
- 若该节点不是叶节点,则将这些苹果移动到它的一个子节点上。
在索利曼开始游戏之前,萨赫尔会恰好对树做一次修改:他将选出两个不同的节点 u 和 v,并交换它们所含的苹果数量。
你能帮萨赫尔计算出有多少种交换方式(即选择无序对 (u,v) 的方案数),使得在双方均采取最优策略的前提下,萨赫尔能够获胜?注意:(u,v) 与 (v,u) 被视为同一对。
输入格式
The first line will contain one integer n (2 ≤ n ≤ 105) — the number of nodes in the apple tree.
The second line will contain n integers _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 107) — 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.
第一行包含一个整数 n(2≤n≤105)—— 苹果树的节点数。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤107)—— 树中每个节点上的苹果数量。
第三行包含 n−1 个整数 p2,p3,…,pn(1≤pi≤n)—— 树中每个节点的父节点。节点 i 的父节点为 pi(其中 2≤i≤n)。节点 1 是树的根节点。
保证输入描述一棵合法的树,且从根节点到任意叶节点的所有路径长度具有相同的奇偶性。
输出格式
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.
在一行中,输出满足以下条件的不同节点对 (u,v)(其中 u=v)的个数:若交换节点 u 与 v 上的苹果后双方开始游戏,则 Sagheer 将赢得该局游戏。注意:(u,v) 与 (v,u) 被视为同一对。
输入输出样例
输入#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.
在第一个样例中,Sagheer 只有在将节点 1 与节点 3 交换时才能获胜。此时,两个叶子节点均拥有 2 个苹果。若 Soliman 在某个叶子节点上进行操作,则 Sagheer 可在另一个叶子节点上执行相同的操作;若 Soliman 将若干苹果从根节点移至某个叶子节点,则 Sagheer 将吃掉这些被移动的苹果。最终,Soliman 将无法再进行任何操作。
在第二个样例中,不存在任何交换操作能使 Sagheer 获胜。
注意:即使 Sagheer 在初始树结构下即可获胜,他也必须执行一次交换操作。
输入解题思路,AI测评打分。不知道怎么写?