[AGC017D]Game on Tree
阿新 • • 發佈:2019-01-13
digi put clas getc for isdigit ice 刪掉 結點
[AGC017D]Game on Tree
題目大意:
一棵\(n(n\le10^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; }
[AGC017D]Game on Tree