码迷,mamicode.com
首页 > 移动开发 > 详细

(博弈\sg) Codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

时间:2017-06-02 11:32:42      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:parent   max   moved   需要   vector   转换   选择   pairs   art   

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 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?≤?105) — the number of nodes in the apple tree.

The second line will contain n integers a1,?a2,?...,?an (1?≤?ai?≤?107) — the number of apples on each node of the tree.

The third line will contain n?-?1 integers p2,?p3,?...,?pn (1?≤?pi?≤?n) — the parent of each node of the tree. Node i has 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 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.

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 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.

 

题意:

一棵树,满足每个叶子结点的深度奇偶性相同,每个结点上放有一定数量物品,每次操作可以选择某一结点的一些物品(数量不为0),如果是叶子结点,去掉这些物品,非叶子结点,将这些物品下放到某一子结点。现后手可以先交换某两个结点的物品,求解有多少种交换方法使得后手可以取得胜利。(u、v和v、u算同一种交换)

 

解题思路:

容易发现这是nim游戏的一个变形。对于nim游戏,将所有堆的元素个数异或起来,如果得到0,则先手负,不然先手胜。

先考虑一个nim的简单变形,如果游戏中允许将某一堆加上若干个(非0)物品(通过某种方式使这种操作有限),判断胜负的方法与前同。因为这样的操作如果由后手进行,先手可以直接去掉这些物品。

对于本题,可以转换为上述nim的简单变形。

首先,将所有叶子结点染为蓝色,从叶子结点出发按红-蓝-红……的顺序不断染其父结点。由于该树满足"每个叶子结点的深度奇偶性相同",所以通过这种染色方法,每个结点颜色是唯一的。将蓝色结点视为nim变形中进行操作的物品堆,红色结点视为上述nim变形中控制添加物品这一操作有限的方法。(因为红色结点都不是叶子结点,进行操作后产生的影响是:其某一蓝色子结点物品个数增加,即上述nim变形游戏中的“添加”操作)

所以我们只需要考虑蓝色结点的异或和。

先只考虑交换两蓝色结点、或两红色结点。

如果初始时异或和为0,则先手已经必负。任意交换两蓝色结点、或交换两蓝色结点均可。

如过初始时异或和非0,先手必胜,只交换蓝色或红色,并不会改变异或和。

再考虑交换蓝色、红色的情况。设原本蓝色结点异或和为sum,想要交换的红色结点为x,则只需要在蓝色结点中找值为sum^x的结点个数(因为 sum^x^y=0 当且仅当y=sum^x)这可以通过之前用map记录一下实现。

将上述方法个数求和即得到了可行的交换个数。 

 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>
10 #include <list>
11 #include <vector>
12 #include <stack>
13 #define mp make_pair
14 //#define P make_pair
15 #define MIN(a,b) (a>b?b:a)
16 //#define MAX(a,b) (a>b?a:b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int MAX=1e5+5;
20 const int MAX_V=1e3+5;
21 const ll INF=4e18+5;
22 const double M=4e18;
23 using namespace std;
24 const int MOD=1e9+7;
25 typedef pair<ll,int> pii;
26 const double eps=0.000000001;
27 #define rank rankk
28 int n,dep,parity,sum;
29 int a[MAX],d[MAX],cnt[3];
30 map <int,int> re;
31 ll an;
32 vector <int>edge[MAX];
33 void dfs(int rt)
34 {
35     for(int i=0;i<edge[rt].size();i++)
36     {
37         int to=edge[rt][i];
38         d[to]=d[rt]+1;
39         dep=max(dep,d[to]);
40         dfs(to);
41     }
42 }
43 int main()
44 {
45     scanf("%d",&n);
46     for(int i=1;i<=n;i++)
47         scanf("%d",&a[i]);
48     for(int i=2;i<=n;i++)
49     {
50         int rt;scanf("%d",&rt);edge[rt].push_back(i);
51     }
52     dfs(1);
53     parity=dep%2;
54     for(int i=1;i<=n;i++)
55     {
56         ++cnt[d[i]&1];d[i]=d[i]&1;
57         if(d[i]==parity)
58             sum^=a[i];
59     }
60     if(sum==0)
61     {
62          an+=(ll)cnt[1]*(cnt[1]-1)/2;
63          an+=(ll)cnt[0]*(cnt[0]-1)/2;
64     }
65     for(int i=1;i<=n;i++)
66         if(d[i]==parity)
67             ++re[a[i]];
68     for(int i=1;i<=n;i++)
69         if(d[i]!=parity)
70             an+=re[sum^a[i]];
71     printf("%I64d\n",an);
72 }

 

(博弈\sg) Codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

标签:parent   max   moved   需要   vector   转换   选择   pairs   art   

原文地址:http://www.cnblogs.com/quintessence/p/6931930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!