日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)

發布時間:2023/11/27 生活经验 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?

Codeforces Round #417:E. FountainsSagheer and Apple Tree(樹上博弈)

標簽:?codeforces ?29人閱讀?評論(0)?收藏?舉報 ?分類:

E. Sagheer and Apple Tree time limit per test ?2 seconds memory limit per test ?256 megabytes input ?standard input output ?standard output

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.

Examples 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


題意:

有一顆很奇怪的蘋果樹,這個蘋果樹所有葉子節點的深度要不全是奇數,要不全是偶數,并且包括根在內的所有節點上都有若干個蘋果,現有兩個極其聰明的人又來無聊的游戲了,每個人可以吃掉某個葉子節點上的部分蘋果(不能不吃),或者將某個非葉子結點上的部分蘋果移向它的孩子(當然也不能不移),吃掉樹上最后一個蘋果的人獲勝,問先手是否必勝?不,不是問這個,因為后手可以作弊,他可以交換任意兩個節點的蘋果數量,問有多少種不同的作弊(交換)方式可以使得后手必勝(不能不交換)?


樹上尼姆:

不如考慮什么時候先手必勝,在尼姆博弈中,將所有的蘋果個數依次異或,如果最后答案為0就先手必敗,否則必勝,那么樹上的呢?其實一樣,不妨把節點按照深度分成兩種:①想要移動到任意葉子結點都需要偶數步數(深度和最深的葉子結點深度奇偶性相同);②想要移動到任意葉子結點都需要奇數步數


對于①,玩家只要移動x個蘋果到它孩子節點上,那么這x個蘋果就一定會變成狀態②之下

對于②玩家只要移動x個蘋果到它孩子節點上,那么這x個蘋果就一定會變成狀態①之下


而孩子結點屬于狀態①,所以當前玩家只要移動狀態②下的x個蘋果,那么另一個玩家只要照搬你的移動,將這x個蘋果再次移動到當前的孩子,就又變成狀態②了,若當前已經到葉子節點無法移動了,另一個玩家就可以直接將這x個蘋果吃掉(你就GG,這幾個蘋果就和沒有一樣!)


這樣就很明顯了,先手對于狀態②下的蘋果,無論怎么移動,最后一定都會被聰明的后手玩家用上述方法吃掉,所以狀態②的蘋果毫無意義,那么狀態①的呢,只要移動一部就會變得狀態②從而毫無意義(和沒有一樣),那這不就轉化成了裸的尼姆博弈了么?


結論:只要將所有狀態①下的蘋果數量異或,最后結果ans若為0則先手必敗,否則必勝!


那么怎么解決這道題,其實已經好辦了,先判斷一波先手必勝還必敗。

如果已經必敗了,那么后手作弊交換節點時就要盡量避免改變戰局,要知道a^b==b^a,異或是滿足交換律的,所以B可以將所以狀態①中任意兩個節點互換,或者將狀態②中任意兩個節點互換,或者將分別屬于狀態①和狀態②中但數量相同的兩堆互換,三種情況個數加在一起即是答案。


如果先手必勝,那么后手的交換一定要改變戰局,這個時候必須將狀態①中的某個節點和狀態②中的某個節點互換,那么怎么換呢?亦或性質a^b^b==a,假設當前異或結果是ans,那么只要遍歷一遍狀態①中的所有節點,對于當前節點的蘋果個數temp,在狀態②中找到蘋果個數為ans^temp的節點,交換即可!最后統計一遍個數便是答案


[cpp]?view plaincopy
  1. //?http://codeforces.com/contest/812/problem/E??
  2. #include<stdio.h>??
  3. #include<algorithm>??
  4. #include<math.h>??
  5. #include<stdlib.h>??
  6. #include<map>??
  7. #include<vector>??
  8. using?namespace?std;??
  9. #define?LL?long?long??
  10. vector<LL>?G[200005];??
  11. map<LL,?LL>?p,?q;??
  12. LL?a[200005],?dep[200005],?maxdep,?jl[200005];??
  13. void?Sech(LL?u,?LL?p,?LL?k)??
  14. {??
  15. ????LL?i,?v;??
  16. ????dep[u]?=?k;??
  17. ????maxdep?=?max(maxdep,?k);??
  18. ????for(i=0;i<G[u].size();i++)??
  19. ????{??
  20. ????????v?=?G[u][i];??
  21. ????????if(v==p)??
  22. ????????????continue;??
  23. ????????Sech(v,?u,?k+1);??
  24. ????}??
  25. }??
  26. int?main(void)??
  27. {??
  28. ????LL?n,?i,?v,?ans,?sp,?sq,?temp,?lala;??
  29. ????scanf("%I64d",?&n);??
  30. ????for(i=1;i<=n;i++)??
  31. ????????scanf("%I64d",?&a[i]),?jl[i]?=?a[i];??
  32. ????sort(jl+1,?jl+n+1);??
  33. ????lala?=?unique(jl+1,?jl+n+1)-(jl+1);??
  34. ????for(i=2;i<=n;i++)??
  35. ????{??
  36. ????????scanf("%I64d",?&v);??
  37. ????????G[i].push_back(v);??
  38. ????????G[v].push_back(i);??
  39. ????}??
  40. ????maxdep?=?1;??
  41. ????Sech(1,?-1,?1);??
  42. ????ans?=?sp?=?sq?=?0;??
  43. ????for(i=1;i<=n;i++)??
  44. ????{??
  45. ????????if(maxdep%2==dep[i]%2)??
  46. ????????????p[a[i]]++,?ans?^=?a[i],?sp++;??
  47. ????????else??
  48. ????????????q[a[i]]++,?sq++;??
  49. ????}??
  50. ????if(ans==0)??
  51. ????{??
  52. ????????ans?=?sp*(sp-1)/2+sq*(sq-1)/2;??
  53. ????????for(i=1;i<=lala;i++)??
  54. ????????????ans?+=?p[jl[i]]*q[jl[i]];??
  55. ????????printf("%I64d\n",?ans);??
  56. ????}??
  57. ????else??
  58. ????{??
  59. ????????temp?=?ans;??
  60. ????????ans?=?0;??
  61. ????????for(i=1;i<=n;i++)??
  62. ????????{??
  63. ????????????if(maxdep%2==dep[i]%2)??
  64. ????????????????ans?+=?q[temp^a[i]];??
  65. ????????}??
  66. ????????printf("%I64d\n",?ans);??
  67. ????}??
  68. ????return?0;??
  69. }??
#include<bits/stdc++.h>//博主自己寫的垃圾代碼
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int a[maxn],dep[maxn],du[maxn],p[maxn];
map<ll,ll> cnt2;
vector<int> g;
int main()
{int n;cin>>n;for(int i=1;i<=n;i++)cin>>a[i];for(int i=2;i<=n;i++){cin>>p[i];du[p[i]]++;}for(int i=1;i<=n;i++){if(du[i]==0)g.push_back(i);}for(int i=0;i<g.size();i++){dep[g[i]]=0;for(int j=g[i];j!=1;j=p[j]){dep[p[j]]=max(dep[j]+1,dep[p[j]]);}}ll ans=0,ans1=0,ans2=0;for(int i=1;i<=n;i++){if(dep[i]%2==0){ans=ans^a[i];ans1++;}else cnt2[a[i]]++;}ll t =0;if(ans==0){for(int i=1;i<=n;i++){if(dep[i]%2==0){t=t+cnt2[a[i]];}}ans2=n-ans1;cout<<ans1*(ans1-1)/2+ans2*(ans2-1)/2+t<<endl;}else{for(int i=1;i<=n;i++){if(dep[i]%2==0){t=t+cnt2[ans^a[i]];}} cout<<t<<endl;}
}


總結

以上是生活随笔為你收集整理的Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。