以深度划分节点为两类,奇数深度和偶数深度的节点(根节点深度为0).
我们的最终目的是使得所有石子在根节点上,也就是奇数数深度的节点上的石子数之和为0.
对于从偶数节点向奇数节点移动的石子,可以从奇数节点上再次向偶数节点移动,从而使得
奇数节点上的石子数之和不变.所以我们只需考虑从奇数节点向偶数节点移动的情况.
这显然就是Nim游戏了.
注意一下对L的处理.
code:
/**************************************************************
Problem: 1777
User: exponent
Language: Pascal
Result: Accepted
Time:36 ms
Memory:480 kb
****************************************************************/
type edge=record
v,n:longint;
end;
const maxn=10001;
var e:Array[0..maxn*2] of edge;
h,s:array[0..maxn] of longint;
vis,o:Array[0..maxn] of boolean;
n,t,l,i,x,y,fi,res,cnt:longint;
procedure add(u,v:longint);
begin
inc(cnt);
e[cnt].v:=v;
e[cnt].n:=h[u];
h[u]:=cnt;
end;
procedure dfs(u,d:longint);
var v,p:longint;
begin
if odd(d) then o[u]:=true;
vis[u]:=true;
p:=h[u];
while p<>0 do
begin
v:=e[p].v;
if not vis[v] then dfs(v,d+1);
p:=e[p].n;
end;
end;
begin
readln(n,t,l);
for i:=2 to n do
begin
readln(fi,s[i]);
add(i,fi);
add(fi,i);
end;
dfs(1,0);
res:=0;
for i:=1 to n do
begin
s[i]:=s[i] mod (l+1);
if o[i] then res:=res xor s[i];
end;
for i:=1 to t do
begin
readln(x,y);
if o[x] then
begin
y:=y mod (l+1);
res:=res xor s[x];
s[x]:=y;
res:=res xor y;
end;
if res=0 then writeln('No')
else writeln('Yes');
end;
end.