"BZOJ2819" nimdescription
Vfleaking, a famous game designer, has recently been hooked on Nim. Ordinary Nim game for: Two people to play, n heap of stones, each turn can take one of the piles of any number, can be taken, but can not be taken. Who can not take who loses. There is a winning strategy for this game. So Vfleaking decided to write a platform to play Nim games to pit the players.
In order to design a beautiful initial situation, vfleaking in the following ways to find inspiration: Take a lot of stones, put them into a pile of piles, to each heap number 1,2,3,4,... N, in the heap and the heap between the edge, there is no self-ring and heavy edge, from any heap to any heap only one path can be reached. Then he kept doing the following:
1. Randomly select two heap v,u, ask if you play NIM game in the gravel heap on the path between V and U, if there is a winning strategy, if there is, vfleaking will consider these gravel heap as one of the initial situations to pit the players.
2. Change the number of stones in Heap V to K.
Because vfleaking was too lazy, he did not bother to do it himself. Please write a program to help him.
Input
The first row is a number n, which indicates how many stones are in the heap.
The next line, number I, indicates how many stones are in the heap.
Next n-1 line, two numbers per line of V,u, representing the v,u between a side directly connected.
The next number Q, which represents the number of operations.
Next Q line, each line begins with one character:
If it is Q, then there are two number v,u, asking if there is a winning strategy if you play Nim in the gravel heap on the path between V and U.
If it is C, then there are two number v,k, which means that the number of stones in the heap V is changed to K.
For 100% of data:
1≤n≤500000, 1≤q≤500000, 0≤ the number of stones per heap at any time ≤32767
There are 30% of the data:
The gravel heap makes up a chain, and these 3 points will cause you to have a Dfs burst stack (maybe you don't have DFS?). )。 Other data Dfs visually does not explode.
Note: The range of stone numbers is 0 to Int_max
Output
For each q, the output line is yes or no, which represents the answer to the query.
Sample Input"Sample Input"
5
1 3 5) 2 5
1 5
3 5
2 5
1 4
6
Q 1 2
Q 3 5
C 3 7
Q 1 2
Q 2 4
Q 5 3
Sample OutputYes
No
Yes
Yes
Yes
A common sense conclusion: Nim game initiator win when and only if all the heap of XOR and not 0, otherwise the initiator will lose
Then use a tree-like array + multiply LCA to maintain the XOR of the DFS sequence (of course, if you maintain the stack-out sequence, you can not multiply the LCA)
#include <cstdio> #include <cstring> #include <iostream>using namespace Std;const int Maxn=500010;int N,m,cnt;int to[maxn<<1],next[maxn<<1],head[maxn],fa[maxn][20],dep[maxn],s[maxn],p[maxn],q[maxn],v[ Maxn];char Str[10];int Rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+gc-' 0 ', Gc=getchar (); return ret*f;} void Add (int a,int b) {to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;} void Dfs (int x) {p[x]=++p[0];for (int i=head[x];i!=-1;i=next[i]) if (to[i]!=fa[x][0]) fa[to[i]][0]=x,dep[to[i]]=dep[x] +1,dfs (To[i]); q[x]=p[0];} void Updata (int x,int val) {if (!x) return; for (int i=x;i<=n;i+=i&-i) S[i]^=val;} int query (int x) {int i,ret=0;for (i=x;i;i-=i&-i) ret^=s[i];return ret;} int main () {scanf ("%d", &n), int i,j,a,b,c,d;memset (head,-1,sizeof (head)), for (i=1;i<=n;i++) v[i]=rd (); for (I=1 ; i<n;i++) A=rd (), B=rd (), add (A, B), add (b,a);d Ep[1]=1,dfs (1); for (i=1;i<=n;i++) Updata (P[i],v[i]), upData (Q[i]+1,v[i]); for (J=1, (1<<j) <=n;j++) for (i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];m=rd (), for (i =1;i<=m;i++) {scanf ("%s", str), A=rd (), B=rd (), if (str[0]== ' Q ') {c=a,d=b;if (dep[a]<dep[b]) swap (A, b); for (j=19;j >=0;j--) if (Dep[fa[a][j]]>=dep[b]) a=fa[a][j];if (a!=b) {for (j=19;j>=0;j--) if (Fa[a][j]!=fa[b][j]) a=fa[a][ J],B=FA[B][J];A=FA[A][0];} if (query (P[c]) ^query (P[d]) ^v[a]) printf ("yes\n"); elseprintf ("no\n");} if (str[0]== ' C ') {updata (P[a],v[a]), Updata (Q[a]+1,v[a]), Updata (p[a],b), Updata (q[a]+1,b); v[a]=b;}} return 0;}
"BZOJ2819" Nim Tree array +lca