洛谷题目链接
CF1363C Game On Leaves
题目描述
Ayush and Ashish play a game on an unrooted tree consisting of $ n $ nodes numbered $ 1 $ to $ n $ . Players make the following move in turns:
- Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to $ 1 $ .
A tree is a connected undirected graph without cycles.
There is a special node numbered $ x $ . The player who removes this node wins the game.
Ayush moves first. Determine the winner of the game if each player plays optimally.
输入格式
The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 10 ) (1 \leq t \leq 10) (1≤t≤10) — the number of testcases. The description of the test cases follows.
The first line of each testcase contains two integers n n n and x x x ( 1 ≤ n ≤ 1000 , 1 ≤ x ≤ n ) (1\leq n \leq 1000, 1 \leq x \leq n) (1≤n≤1000,1≤x≤n) — the number of nodes in the tree and the special node respectively.
Each of the next n − 1 n-1 n−1 lines contain two integers u u u , v v v ( 1 ≤ u , v ≤ n , u ≠ v ) (1 \leq u, v \leq n, \text{ } u \ne v) (1≤u,v≤n, u=v) , meaning that there is an edge between nodes u u u and v v v in the tree.
输出格式
For every test case, if Ayush wins the game, print “Ayush”, otherwise print “Ashish” (without quotes).
输入输出样例 #1
输入 #1
1
3 1
2 1
3 1
输出 #1
Ashish
输入输出样例 #2
输入 #2
1
3 2
1 2
1 3
输出 #2
Ayush
说明/提示
For the 1 1 1 st test case, Ayush can only remove node 2 2 2 or 3 3 3 , after which node 1 1 1 becomes a leaf node and Ashish can remove it in his turn.
For the 2 2 2 nd test case, Ayush can remove node 2 2 2 in the first move itself.
题意翻译
给定 n 个节点的无根树。
两名选手轮流操作,每次可以选择一个叶节点并删除它以及所有和它相连的边。叶节点指度数不超过 1 的节点。删除节点 x 的选手胜利。
你需要判断先手是否有必胜策略。具体来讲,如果先手有必胜策略,输出 Ayush,否则输出 Ashish。
多组数据,数据组数 t≤10,n≤1000
分析:
我们考虑两种情况
①x在叶子节点上:此时先手只需直接删 x x x点便能获胜
②x不在叶结点上:先手何时能胜?就是在 x x x的度为 2 2 2并且只剩 3 3 3个点的时候(如图所示)将树扔给后手,此时后手只能删掉非 x x x点中的一个,那么 x x x的度就变为 1 1 1,先手必胜。
分析:我们考虑两种情况
所以,我们首先特判 x x x是否在叶节点上,再看边的条数的奇偶性.是奇数, x x x最后就被先手删了。
#include<bits/stdc++.h>
using namespace std;
int n,T,a,b,x;
int degree[100001];//记录每个点的度数
int main(){cin>>T;while(T--){cin>>n>>x;memset(degree,0,sizeof degree);//多组数据要清空for(int i=1;i<n;i++){cin>>a>>b;degree[a]++;degree[b]++;}if(degree[x]<=1) cout<<"Ayush";//特判else{if(n%2==1){cout<<"Ashish";}else{cout<<"Ayush";}}cout<<endl; }return 0;
}