(game \sg) codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

Source: Internet
Author: User

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 ai apples. This tree had a special property:the lengths of all paths from the root to any leaf having the same parity (i.e. all paths has even length or all paths has odd length).

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

In each move, the current player would pick a single node, take a non-empty subset of apples from it and do one of the foll Owing 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'll make exactly one change to the tree. He'll pick, different nodes u and v and swap the apples of u with the apples o F v.

Can Sagheer count the number of ways to make the swap (i.e. to choose u and v) after WH Ich he would win the game if both players play optimally? (u,? ) V) and (v,? U) is considered to be the same pair.

Input

The first line would contain one integer n (2?≤? N? ≤?105)-the number of nodes in the apple tree.

The second line would 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 would contain n?-? 1 integers p2,? P3,?...,? Pn (1?≤? Pi? ≤? N)-the parent of each node of the tree. Node I has the parent pi (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 would have the T He 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'll win the game. (u,? ) V) and (v,? U) is considered to be the same pair.

Example

Input
3
2 2 3
1 1
Output
1
Input
3
1 2 3
1 1
Output
0
Input
8
7 2 2 5 4 3 1 1
1 1 1 4 4 5 6
Output
4

Note

In the first sample, Sagheer can only win if he swapped node 1 with node 3. In this case, both leaves would 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 would eat those moved apples. Eventually, Soliman would not find a move.

In the second sample, there are no swap that would make Sagheer win the game.

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

Test instructions

A tree, satisfying each leaf node depth parity, each node placed a certain number of items, each operation can choose a certain node of the items (the number is not 0), if the leaf node, remove these items, non-leaf nodes, the items are decentralized to a sub-node. Now, you can exchange a two-node object first, solve how many kinds of exchange method so that can win. (U, V and V, U are counted in the same exchange)

Problem Solving Ideas:

It is easy to see that this is a variant of the Nim game. For NIM games, the number of elements of all heaps is different or up, if you get 0, then the initiator negative, otherwise the initiator wins.

Consider the simple deformation of a nim, if the game allows a heap plus a number of (not 0) items (in some way to make this operation Limited), the method of judging the outcome and the previous. Because if this operation is done by the hand, the initiator can directly remove these items.

For the subject, can be converted to the simple deformation of the above Nim.

First, all the leaves knot Dianran blue, from the leaf knot to press red-blue-red ... The order of the parent node is constantly dyed. Since the tree satisfies "the same depth parity of each leaf node", the color of each node is unique through this staining method. The blue node is treated as a heap of objects operating in the Nim deformation, and the red node is considered a limited way to control the addition of items in the Nim deformation. (Because the red node is not a leaf node, the effect after operation is: the number of its blue sub-node items increased, that is, the above Nim deformation game "add" action)

So we just have to consider the different or the blue nodes.

Consider swapping only two blue nodes, or two red nodes first.

If the initial time is different or 0, then the initiator will already be negative. Switch between two blue nodes, or swap two blue nodes at any point.

If the initial time is different or not 0, the initiator wins, only exchange blue or red, and will not change the XOR.

Consider swapping the blue and red cases again. Set the original blue node Xor sum to the red node that you want to swap for x, you only need to find the number of nodes in the blue node that are sum^x (because sum^x^y=0 and only if y=sum^x) this can be done by using a map before.

The number of possible exchanges is obtained by summing the above methods.

1#include <iostream>2#include <string>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <queue>8#include <Set>9#include <map>Ten#include <list> One#include <vector> A#include <stack> - #defineMP Make_pair - //#define P Make_pair the #defineMIN (A, B) (A&GT;B?B:A) - //#define MAX (A, B) (A>B?A:B) -typedefLong Longll; -typedef unsignedLong Longull; + Const intmax=1e5+5; - Const intmax_v=1e3+5; + Constll inf=4e18+5; A Const Doublem=4e18; at using namespacestd; - Const intmod=1e9+7; -typedef PAIR&LT;LL,int>PII; - Const Doubleeps=0.000000001; - #defineRank RANKK - intn,dep,parity,sum; in inta[max],d[max],cnt[3]; -Map <int,int>re; to ll an; +Vector <int>Edge[max]; - voidDfsintRT) the { *      for(intI=0; I<edge[rt].size (); i++) $     {Panax Notoginseng         intto=Edge[rt][i]; -d[to]=d[rt]+1; thedep=Max (dep,d[to]); + Dfs (to); A     } the } + intMain () - { $scanf"%d",&n); $      for(intI=1; i<=n;i++) -scanf"%d",&a[i]); -      for(intI=2; i<=n;i++) the     { -         intRT;SCANF ("%d",&RT); Edge[rt].push_back (i);Wuyi     } theDfs1); -parity=dep%2; Wu      for(intI=1; i<=n;i++) -     { About++cnt[d[i]&1];d [i]=d[i]&1; $         if(d[i]==parity) -sum^=A[i]; -     } -     if(sum==0) A     { +an+= (LL) cnt[1]* (cnt[1]-1)/2; thean+= (LL) cnt[0]* (cnt[0]-1)/2; -     } $      for(intI=1; i<=n;i++) the         if(d[i]==parity) the++Re[a[i]]; the      for(intI=1; i<=n;i++) the         if(d[i]!=parity) -an+=re[sum^A[i]]; inprintf"%i64d\n", an); the}

(game \sg) codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.