Game On Leaves
Contest: Codeforces Round 646 (Div. 2)
Difficulty rating: 1600
Tags: games, trees

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.

Input format:
The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10)$$$ — the number of testcases. The description of the test cases follows.

The first line of each testcase contains two integers $$$n$$$ and $$$x$$$ $$$(1\leq n \leq 1000, 1 \leq x \leq n)$$$ — the number of nodes in the tree and the special node respectively.

Each of the next $$$n-1$$$ lines contain two integers $$$u$$$, $$$v$$$ $$$(1 \leq u, v \leq n, \text{ } u \ne v)$$$, meaning that there is an edge between nodes $$$u$$$ and $$$v$$$ in the tree.

Output format:
For every test case, if Ayush wins the game, print "Ayush", otherwise print "Ashish" (without quotes).

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For the $$$1$$$st test case, Ayush can only remove node $$$2$$$ or $$$3$$$, after which node $$$1$$$ becomes a leaf node and Ashish can remove it in his turn.

For the $$$2$$$nd test case, Ayush can remove node $$$2$$$ in the first move itself.

Example input:
1
3 1
2 1
3 1

Example output:
Ashish

Example input:
1
3 2
1 2
1 3

Example output:
Ayush

Editorial:
Key Idea: The main idea of this problem is to think backwards. Instead of thinking about how the game will proceed, we think about how the penultimate state of the game will look like, etc. Also, we take care of the cases where the game will end immediately (i.e, when the special node is a leaf node).
Detailed Explanation: First, let us take care of the cases where the game ends immediately. This only occurs when the special node $$$x$$$ is a leaf node, so all we must do is check that deg[$$$x$$$] = 1. Please note that $$$n = 1$$$ must be handled seperately here (just output Ayush).
Now, in the case where $$$x$$$ is not a leaf node, the answer is as follows: Ashish wins if $$$n$$$ is odd, and Ayush wins if $$$n$$$ is even. I will provide a short sketch of the proof below.
With the hint from the key idea, let us analyze this game backwards. (I will assume that $$$n>10$$$ for the sake of a clear explanation). When $$$x$$$ is removed from the game, it cannot be the only node remaining (because then the previous player could have also removed $$$x$$$, and thus he did not play optimally). Assume the structure of the game is something like the following WLOG at the last step (The tree attached to $$$x$$$ could be any tree):
Consider also that Ayush won, and the last move was to remove $$$x$$$. Now, what could have been the state before this move? If Ashish had removed a node from the tree, then he did not play optimally - since he could have removed $$$x$$$! Thus, he must have removed something from $$$x$$$, which looks like the following:
Considering this state, Ashish should not infact remove $$$6$$$, and instead remove something from the tree! Hence, the state that we assumed the game should look like at the end is impossible - and indeed, the tree attached to $$$x$$$ should only consist of only one node (we already proved that $$$x$$$ cannot be the only node remaining).
Thus, all we have to do is find who's turn it will be when the structure of the tree is as follows:
It is Ashish's turn if $$$n$$$ is odd, and Ayush's turn if $$$n$$$ is even. QED!
Time complexity: $$$O(N)$$$