Approximate test instructions:
Give you a tree, this tree has the following characteristics: There are several apples on each node, and the path length from the root node to any leaf node has the same parity.
A and B play (busy) (get) play (panic).
During the game, A-B turns a number of apples from any node to one of its leaf nodes, and if there are no leaf nodes, the apples disappear (eaten).
If a player is unable to operate, then he loses.
The game starts with a, and B can choose to exchange the number of apples on the two nodes first. Ask B How many ways of exchange, so that the last B win.
Nim Games:
If you understand the Nim game, do not look at the \ (^o^)/~ You must have this problem.
Understand the definition of Nim game, children who do not know the proof, skip 6 lines and look down.
These 6 lines are addressed to Moe's new
Let lazy me excerpt a Baidu encyclopedia words:
Let's look at the wonderful conclusion:
Simply put, as long as the number of each pile of stones all xor up, and finally get 0, then there is a winning strategy, otherwise the initiator has a winning strategy.
Nim game conclusion proves that:
Do not want to see proof, want to see the practice, skip 13 lines down to see.
Let me describe, if XOR up is 0, the winning strategy of the back.
We first set each heap of stones to all Xor up, the result is P.
At first P=0.
If the initiator selects a heap of stones, the number of stones is X, he takes some stones, and the remaining stones count as Y.
Then we'll remember T=x^y.
Easy to know, t≠0, after the initiator operation, p=t≠0.
So, it is easy to know, the a^t<a will find a pile of stone (number of stones a), meet the
Because a must be found, it is 1 with the binary highest bit of T. (This cannot be understood, give up oi ...) All right, a joke.
Then, after a change of a to A^t, p becomes 0 again.
So, after the initiator operation, p is never 0, after the operation, p is always 0. And the game is not endless, so must be the win.
so , suppose the first p≠0, what is the initiator strategy?
The obvious is to find a heap of stones (number of stones is a), to meet the A^p<a,
The initiator turns a into a^p, so that p becomes 0. Then you can do the same with the strategy.
Practice:
Perhaps when the game players Sagheer and Soliman know each other using the winning strategy, they will not like to play this game.
Because, at the beginning of the game situation decided, who can win.
Let me see--
There is a very good condition in the subject,
So the subject is very difficult to become very simple.
D[x] represents the parity of the path length of x to a leaf node in that subtree. (0 means even, 1 means odd)
If you cannot exchange nodes at first, then the strategy can be:
Suppose that a point with a D value of 1 belongs to a collection with a 1,d value of 0 belonging to collection 0.
①: The initiator moves the y apples of point x in collection 1 down to Z,
So we know d[z]=0, then we can move the y apples of z Down
② if the points in set 0 are listed, then this is a typical NIM game.
As long as the number of apples on these points p=0 the value of the XOR, the other can win.
back to the original title : the first opportunity to exchange two nodes.
Assuming p=0, the FLAC can exchange any two points in the set 0, or any two points in the collection 1.
Otherwise, you must exchange a point in collection 0 with one of the points in collection 1.
Get a bucket, enumerate OK.
Code:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 #defineRef (I,X,Y) for (int i=x;i<=y;++i)5 intRead () {6 CharC=getchar ();intD=0, f=1;7 for(;c<'0'|| C>'9'; C=getchar ())if(c=='-') f=-1;8 for(; c>='0'&&c<='9';d =d*Ten+c- -, c=GetChar ());9 returnd*F;Ten } One Const intn=100001, m=16777216; A intN,cnt,t[m],d[n],a[n],head[n],to[n],nxt[n]; - voidAddedge (intXinty) { -++cnt;to[cnt]=y;nxt[cnt]=head[x];head[x]=CNT; the } - voidDfsintx) { - for(intI=head[x];i;i=Nxt[i]) { - DFS (To[i]); +d[x]=d[to[i]]^1; - } + } A intMain () at { -n=read (); - refI1, N) a[i]=read (); - refI2, N) Addedge (read (), i); -Dfs1); - inttmp=0, tot=0; in Long Longans=0; - refI1, N)if(!d[i]) tmp^=A[i]; to refI1, N)if(D[i]) t[a[i]]++,tot++; + if(tmp==0) ans=1ll*tot* (tot-1)/2+1ll* (N-tot) * (n-tot-1)/2; - refI1, N)if(!d[i]) ans+=t[tmp^A[i]]; theprintf"%lld\n", ans); *}
Codeforces 812E Sagheer and Apple Tree