Title 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 output
Yes
No
Yes
Yes
Yes
Exercises
DFS sequence + Tree array + Multiply LCA
First, there is a conclusion: Nim game to win the full necessary condition is the number of stones per heap of different or not 0. The proof is everywhere, it's not here.
So we just need to maintain the XOR of the number on the two-point path.
Considering the x^x=0, we can maintain a DFS into the stack, and the weights for each location are w[x].
In the query, you can use Root~x and Root~y xor, but because the graph is a point, this will lose the LCA (x, y), so need to ask for another LCA and add to the answer.
Using a tree array to maintain the XOR and the multiplication of the tree, the time complexity of $o (n\log N) $
In addition, the subject bare on the DFS, if only pass a parameter, is not exploding stack.
#include <cstdio> #include <algorithm> #define N 500010using namespace Std;int a[n], head[n], to[n << 1] , Next[n << 1], CNT, fa[n][20], deep[n], log[n], f[n << 1], v[n << 1], tot, lp[n], Rp[n];char str[5];void Add (int x, int y) {to[++cnt] = y, next[cnt] = head[x], head[x] = cnt;} void Dfs (int x) {int i;lp[x] = ++tot, V[tot] = a[x];for (i = 1; I <= log[deep[x]]; i + +) fa[x][i] = Fa[fa[x][i-1]][i -1];for (i = head[x]; i; i = Next[i]) if (to[i]! = fa[x][0]) fa[to[i]][0] = x, deep[to[i]] = deep[x] + 1, DFS (To[i]); rp[ X] = ++tot, V[tot] = a[x];} int Getlca (int x, int y) {int i;if (deep[x] < deep[y]) swap (x, y); for (i = log[deep[x]-deep[y]]; ~i; i--) if (deep[x ]-(1 << i) >= deep[y]) x = fa[x][i];for (i = log[deep[x]]; ~i; i--) if (Deep[x]-(1 << i) >= 0 && Amp Fa[x][i]! = Fa[y][i]) x = fa[x][i], y = fa[y][i];return x = = y? X:FA[X][0];} void update (int x, int a) {int i;for (i = x; i <= tot; i + = i &-i) f[i] ^= A;} int query (int x) {int I, ans = 0;for (i = x; i; i-= i & i) ans ^= f[i];return ans;} int main () {int n, m, I, X, Y, t;scanf ("%d", &n), for (i = 1; I <= n; i + +) scanf ("%d", &a[i]); for (i = 1 ; I < n; i + +) scanf ("%d%d", &x, &y), add (x, y), add (y, x); for (i = 2; I <= n; i + +) log[i] = log[i >> 1] + 1;DFS (1); for (i = 1; I <= tot; i + +) update (I, V[i]), scanf ("%d", &m), and while (M--) {scanf ("%s%d%d", str, &am P;x, &y); if (str[0] = = ' Q ') t = Getlca (x, y), printf ("%s\n", Query (lp[x]) ^ query (lp[y]) ^ A[getlca (x, y)]? "Yes": "No"), Else Update (lp[x], a[x] ^ y), update (Rp[x], a[x] ^ y), a[x] = y;} return 0;}
"bzoj2819" Nim DFS sequence + tree-like array + multiply LCA