[AGC017D]Game on Tree

[AGC017D]Game on Tree

题目大意:

一棵(n(nle10^5))个结点的树。A和B轮流进行游戏,A先手。每次删掉一棵子树,根结点不能删。最先不能操作的人输,问最后谁赢。

思路:

根据树上删边游戏的经典结论,根结点的sg值等于各子结点的sg值+1后的异或和。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
	register char ch;
	while(!isdigit(ch=getchar()));
	register int x=ch^'0';
	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
	return x;
}
const int N=1e5+1;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
	e[u].push_back(v);
	e[v].push_back(u);
}
int dfs(const int &x,const int &par) {
	int sg=0;
	for(auto &y:e[x]) {
		if(y==par) continue;
		sg^=dfs(y,x)+1;
	}
	return sg;
}
int main() {
	const int n=getint();
	for(register int i=1;i<n;i++) {
		add_edge(getint(),getint());
	}
	puts(dfs(1,0)?"Alice":"Bob");
	return 0;
}
原文地址:https://www.cnblogs.com/skylee03/p/10262629.html