NC24628. [USACO 2010 Hol G]Rocks and Trees
描述
My country's bigger than most
And if asked I boast
'Cause I'm really proud
So I shout it loud
Though our numbers are few
We will welcome you
Although we don't have history
Gold medal winning teams
Heroes or prisoners
World famous volcanoes
Still what we've got's glorious
'Cause we've got
Rocks and trees
And trees and rocks
And rocks and trees
And trees and rocks
And rocks and trees
And trees and rocks
And rocks and trees
And trees and rocks
And water
-The Arrogant Worms, on Canada
Consider this example with three nodes numbered as shown and the shape shown in Board 0. Initially, there are 5 rocks on node 2 and 3 rocks on node 3; see Board 1.
For the first change, Ted removes 2 rocks from node 2 (thus leaving 3); see Board 2. For the second change, Ted removes 2 rocks from node 3 (thus leaving 1). Note that node 2 still has 3 rocks; see Board 3.
1 - - -
/ \ / \ / \ / \
2 3 5 3 3 3 3 1
Board 0 Board 1 Board 2 Board 3
Your program should determine in each case who wins.
For about 30% of the test cases, N <= 10, and T <= 100, and no tree node will have more than 5 rocks on it after any of Ted's changes.
输入描述
* Line 1: Three space-separated integers: N, T, and L
* Lines 2..N: Line i contains two space-separted integers: and
* Lines N+1..N+T: Line j+N describes Ted's next change using two space-separated integers: and
输出描述
* Lines 1..T: Line i contains 'Yes' if Ted can win the game after change i; and 'No' otherwise.
示例1
输入:
3 2 10 1 5 1 3 2 3 3 1
输出:
No Yes
C++11(clang++ 3.9) 解法, 执行用时: 14ms, 内存消耗: 512K, 提交时间: 2020-01-11 08:04:45
#include <bits/stdc++.h> #define maxn 10005 #define maxL 1005 using namespace std; int a,b,x,n,T,L,r[maxn],sg[maxL],prt[maxn],dep[maxn]; inline bool Solve(int pos,int chg){ if(!(dep[pos]&1)) return x; x^=sg[r[pos]]; x^=sg[r[pos]=chg]; return x; } int main() { scanf("%d%d%d",&n,&T,&L); for(int i=1;i<=1000;++i) sg[i]=i%(L+1); for(int i=2;i<=n;++i){ scanf("%d%d",&prt[i],&r[i]); dep[i]=dep[prt[i]]+1; if(dep[i]&1) x^=sg[r[i]]; } for(int i=1;i<=T;++i){ scanf("%d%d",&a,&b); if(Solve(a,b)) printf("Yes\n"); else printf("No\n"); } return 0; }
C++14(g++5.4) 解法, 执行用时: 10ms, 内存消耗: 548K, 提交时间: 2019-11-25 22:06:14
#include<stdio.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) int T,n,L,sg,dep[11000],v[11000],x,y,pa; int main(){ scanf("%d%d%d",&n,&T,&L); fo(i,2,n){ scanf("%d%d",&pa,&v[i]); dep[i]=dep[pa]+1; v[i]%=(L+1); if (dep[i]&1) sg^=v[i]; } while (T--){ scanf("%d%d",&x,&y); if (dep[x]&1){ sg^=v[x]; y%=(L+1); sg^=y; v[x]=y; } if (sg) printf("Yes\n");else printf("No\n"); } return 0; }