


If N = 7, X = 2 and Edges = { {0, 1}, {0, 2}, {1, 4}, {1, 5}, {2, 3}, {4, 6} }
Then the tree is:

Here N = 7 depicts that there are seven kingdoms (numbered from 0 to 6). Edges depict that kingdom-0 and kingdom-1 are adjacent, kingdom-0 and kingdom-2 are adjacent, and so on. And Aragorn initially controls kingdom-2.
Then in this case, if you select kingdom-1 in your first then, then Aargon will select kingdom-0 in the next turn, as both rulers play optimally, therefore in the end Aargon will be ruling kingdom-0, kingdom-2 and kingdom-3 and the remaining four kingdoms will be ruled by you. Hence you will be able to win the game and therefore we will print “true”.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains three integers ‘N’, and ‘X’ denoting the number of kingdoms and the kingdom initially ruled by Aragorn respectively.
The next N - 1 lines each contain two integers each denoting the kingdoms that are adjacent to each other.
For each test case, print “true” if you can conquer more kingdoms than Aragorn, else print “false”.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
2 ≤ N ≤ 1000
0 ≤ X < N
0 ≤ Edges[i][0], Edges[i][1] < N
Time limit: 1 sec
The question becomes easy if you are able to figure out that only the decision of selecting the first kingdom to conquer will be sufficient to determine the final answer. Let’s start with something more intuitive rather than directly jumping to the final answer. If you pick a random kingdom in your first turn, in subsequent turns both you and Aragorn you will greedily conquer all the kingdoms that lie on the shortest path connecting the kingdom-X (initially ruled by Aragorn) and the random kingdom that you decide to conquer in your first turn. This happens because both of you play optimally, so both the rulers will try to decrease the number of kingdoms that the other may conquer in future, this is simply done by blocking the kingdoms that are between both the initially conquered kingdoms. Therefore by selecting a random kingdom we can determine who will win finally.
Now extending this logic further, selecting a random kingdom in the beginning gives the other player (Aragorn) the opportunity to conquer the kingdoms that lie between the initial kingdoms. We can easily remove this possibility by conquering a kingdom that is just adjacent to Aragorn’s initial kingdom. In this way, we can greedily find if it is possible for us to win, as the kingdom we initially select will make sure that we can conquer the entire subtree of that initial kingdom in future, as all the kingdoms form a tree and there would be no cycles, so Aragorn would never be able to conquer any kingdom that lies in the subtree of our initial kingdom, on the same note we would also never be able to conquer any other kingdom that doesn’t lie in the subtree of our initial kingdom.
To implement this logic, Apply depth-first search on every adjacent kingdom of X, make sure that Xth node is not visited in dfs. Find out the subtree with the maximum number of nodes. Finally, check if the subtree with the most number of nodes has more than half the total kingdoms, then print “true” else we will print “false”.
The steps are as follows :