A game of trees
Practice
3.8 (8 votes)
Trie
Tree
Depth first search
Graphs
Algorithms
Game theory
Problem
83% Success 2047 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
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 \(n-1\) lines of each test case contain \(p_{i}\) that means the \((i+1)^{th}\) vertex's parent is \(p_{i} \).
Output format
For each test case, print \(A\) if \(A\) wins, otherwise print \(B\).
Constraints
\(1 \leq n \leq 5 \times 10^4\)
\(1 \le p_{i} \le i\)
\(1 \le t \le 10\)
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
AlgorithmsApprovedGraphsHardOpen
Points:50
13 votes
Tags:
AlgorithmsDepth First SearchDynamic ProgrammingGraphsTrees
Points:50
2 votes
Tags:
AlgorithmsGraphsBinary SearchDepth First Search
Editorial