2819:nim time limit:20 Sec Memory limit:128 MB
submit:1596 solved:597
[Submit] [Status] [Discuss] Description
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
Hintsource
Hubei Province Team Mutual test
Ideas
Bit+dfs sequence +lca
First know to Judge Nim game is to see Xor or and so the problem is to deal with the tree on the XOR and query.
Set F[i] means I to the root path of the XOR, then query Q (u,v) is equal to F[U]^F[V]^VAL[LCA], because there is C operation so we have to consider maintenance f[]. When modifying the value of a node u, we find that f changes only the nodes in the subtree with the root of U, based on the DFS order, we use the bit maintenance interval (XOR or set->r+1 interval), see the code.
Code
1#include <cstdio>2#include <cstring>3#include <vector>4#include <iostream>5 #definefor (A,B,C) for (int a= (b); a<= (c); a++)6 using namespacestd;7 8 Const intN =500000+Ten;9 Const intD = -;Ten One intN,q,dfsc,val[n]; Avector<int>G[n]; - - //BIT the intC[n]; - void Set(intXintv) { - while(x<=N) -C[x]^=v, x+=x&-x; + } - intQueryintx) { + intans=0; A while(x) atANS^=C[X], x-=x&-x; - returnans; - } - //LCA & DFSR - intFa[n][d],l[n],r[n],d[n]; - voidDfsintu) { inl[u]=++DFSC; - for(intI=0; I<g[u].size (); i++) { to intv=G[u][i]; + if(v!=fa[u][0]) { -fa[v][0]=u; thed[v]=d[u]+1; * for(intI=1; i<d;i++) $fa[v][i]=fa[fa[v][i-1]][i-1];Panax Notoginseng Dfs (v); - } the } +r[u]=DFSC; A } the intLCA (intUintv) { + if(d[v]>D[u]) swap (U,V); - for(inti=d-1; i>=0; i--) $ if(D[fa[u][i]]>=d[v]) u=Fa[u][i]; $ if(U==V)returnu; - for(inti=d-1; i>=0; i--) - if(Fa[u][i]!=fa[v][i]) u=fa[u][i],v=Fa[v][i]; the returnfa[u][0]; - }Wuyi voidReadint&x) { the CharC=GetChar (); - while(!isdigit (c)) c=GetChar (); Wux=0; - while(IsDigit (c)) Aboutx=x*Ten+c-'0', c=GetChar (); $ } - intMain () { - read (n); -for (I,1, N) read (val[i]); A intu,v; +for (I,1, N-1) { the Read (U), read (v); - G[u].push_back (v); $ g[v].push_back (u); the } theDFS (d[1]=1); thefor (I,1, N) the Set(L[i],val[i]),Set(r[i]+1, Val[i]); - read (q); in Charop[2]; the while(q--) { thescanf"%s", op); About Read (U), read (v); the if(op[0]=='Q') { the intLc=LCA (u,v); the if(Query (L[u]) ^query (L[v]) ^VAL[LC]) +Puts"Yes");ElsePuts"No"); - } the Else {Bayi Set(L[U],VAL[U]^V),Set(r[u]+1, val[u]^v);//^val to cut off theval[u]=v; the } - } - return 0; the}
Bzoj 2819 Nim (Bit,dfs sequence, LCA)