luogu#P7864. 「EVOI-RD1」摘叶子

「EVOI-RD1」摘叶子

Problem Description

One day, Little A and Little B were happily playing a game together.

They found a tree rooted at node 11. Obviously, as a tree, it has one or more leaf nodes. Little A and Little B play a turn-based game.

On each turn, Little A or Little B may choose any number of leaf nodes and pick (remove) them from the tree (each time they can only pick leaf nodes; the number picked each time is not limited, but they must pick at least one, and they cannot pick more than the number of existing leaf nodes).

Clearly, after picking some leaves, their parent nodes may become new leaf nodes. At this time, these original parent nodes that have become leaf nodes can also be picked.

Now Little A picks first, then Little B, and they alternate. The person who picks (removes) node 11 wins. We know Little A and Little B will always play optimally. Determine who will win.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, indicating that there are TT test cases.

For each test case, the second line contains a positive integer nn, indicating that the tree has nn nodes.

For each test case, the third line contains n1n-1 integers, representing the parent node index for nodes 22 through nn.

Output Format

Output TT lines, each containing an integer 11 or 00.

11 means Little A will win, and 00 means Little B will win.

2
3
1 1
4
1 2 3

1
0

Hint

The testdata is random. With a simple analysis of the properties, it is easy to get partial points, so this problem uses bundled tests.

For 40%40\% of the testdata: 1n1001 \leq n \leq 100.

For 100%100\% of the testdata: 1n1061 \leq n \leq 10^6, 1T101 \leq T \leq 10.

The time and memory limits of this problem (especially memory) are very generous. There are no tricky constant-factor limits and no nasty pitfalls, so feel free to solve it.

Translated by ChatGPT 5