M - Game
Flynn and oooccc1 are playing game some day. The game is played on a rooted graph which is an undirected graph with every edge attached by some path to a special vertex called the root or the ground. The ground is denoted in the figure that follows by a dotted line. You could see it from the figure.
On each step, one player could chop out one edge, and removing that edge and anything not connected to the ground. The one who have nothing to chop with fails.
Flynn is the one who is going to move. Help him by telling him if it is possible to win by carefully choose the edge to chop with.
Input
There are a few cases. Process to the end of file
Each case starts with N (1 <= N <= 1000) stand for the number of points.
The next line give N numbers, the i-th (i = 0 … N-1) of which represents its father nodes. The root nodes was the one with value -1.
Output
Print “YES” if could win if play optimally, and “NO” otherwise.
Sample Input
8
-1 1 2 2 -1 5 6 6
Sample Output
NO
这道题目的意思是给你一棵树,边是没有方向的,每次操作你可以删掉一条边,并且与这条边相连的子树也一起删掉,给你信息是第i-1个点的父节点是第i-1个数字,让你算一下,第一个人是否是有必胜的策略,每一个节点的sg值与子树的sg值是有关的
sg[fa]^=(sg[son]+1);//就是所有儿子的sg值加上1的所有值异或;
看一下所有的根节点的sg值的异或值是否是0就可以判断了,
节点 sg
3 0
2 4 0^(0+1) 0
1(根节点) 0^(0^(0+1))^0;
vector <int>ma[Max];
bool visited[Max];
int n,boss[Max],sg[Max];
void DFS(int u,int fa){
visited[u]=true;
int len=ma[u].size();
int h=0;
rep(i,0,len-1){
int to=ma[u][i];
if(to!=fa){
DFS(to,u);
h^=(sg[to]+1);
}
}
sg[u]=h;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
rep(i,0,n) ma[i].clear();
rep(i,0,n-1){
int x;
scanf("%d",&x);
boss[i]=x;
ma[x].push_back(i);
if(boss[i]!=-1) ma[i].push_back(x);
sg[i]=0;
}
int key=0;
rep(i,0,n-1){
if(boss[i]<0){
DFS(i,-1);
key^=sg[i];
}
}
rep(i,0,n-1) printf("%d ",sg[i]);
printf("\n");
if(key==0) printf("NO\n");
else printf("YES\n");
}
return 0;
}