A game of trees

3.8

8 votes
Trie, Tree, Depth First Search, Graphs, Algorithms, Game Theory
Problem

You are given a rooted tree with n vertices. Here, A and B are playing with the tree. In each turn (starting with A), each player can select a vertex v that contains no parent and delete a path with at least two vertices starting from v.
A player loses if he or she cannot make the turn in the game. For each test case, determine who will win.

Input format

  • The first line contains t denoting the number of test cases.
  • The first line of each test case contains n denoting the number of tree's vertices.
  • Next n1 lines of each test case contain pi that means the (i+1)th vertex's parent is pi.

Output format

For each test case, print A if A wins, otherwise print B.

Constraints

1n5×104

1pii

1t10

Sample Input
2
5
1
2
1
4
5
1
1
1
1
Sample Output
B
A
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test, player B will always have a path of two vertices after A's move that will finish the game, so B is the winner.
In the second test, after any player A's choice for the path, the tree will become a set of vertices without any edge, so A is the winner. 

Editor Image

?