#P16459. [UOI 2026] Tree Subsets

[UOI 2026] Tree Subsets

题目描述

You are given a rooted tree with nn vertices. The root of the tree is vertex 11.

Each vertex ii has a value aia_i, which is either 00 or 11.

For each size kk (1kn1 \le k \le n), determine which xor values can be obtained by choosing a set of vertices SS that satisfies the following conditions:

  • S=k|S| = k;
  • if vertex vv belongs to the set SS, then all vertices in the subtree of vertex vv also belong to SS.

The value of a set SS is defined as the xor of the values of all vertices in this set: vSav.\bigoplus_{v \in S} a_v.

The subtree of vertex vv is the set of all vertices uu such that vertex vv lies on the simple path from the root 11 to vertex uu.

The xor operation for bits is defined as follows: $0 \oplus 0 = 0, 0 \oplus 1 = 1, 1 \oplus 0 = 1, 1 \oplus 1 = 0.$

The child of vertex vv is a vertex whose parent is vv.

输入格式

The first line contains one integer tt (1t10001 \le t \le 1000) --- the number of test cases.

Each test case consists of three lines.

The first line of each test case contains one integer nn (2n41052 \le n \le 4 \cdot 10^5) --- the number of vertices in the tree.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (ai{0,1}a_i \in \{0, 1\}) --- the values of the vertices.

The third line contains n1n-1 integers p2,p3,,pnp_2, p_3, \ldots, p_n (1pi<i1 \le p_i < i), where pip_i is the parent of vertex ii.

It is guaranteed that the sum of nn over all test cases does not exceed 41054 \cdot 10^5.

输出格式

For each test case, output one line with nn integers ans1,ans2,,ansnans_1, ans_2, \ldots, ans_n.

For each kk:

  • ansk=0ans_k = 0, if among all valid sets of size kk only xor 00 can be obtained;
  • ansk=1ans_k = 1, if among all valid sets of size kk only xor 11 can be obtained;
  • ansk=2ans_k = 2, if among all valid sets of size kk both xor 00 and xor 11 can be obtained.
2
5
1 0 1 1 0
1 2 1 4
9
0 0 0 1 1 1 1 1 1
1 2 3 2 4 4 6 8
2 1 2 0 1 
1 0 1 0 1 2 0 0 0 

提示

This is what the trees in the first and second test cases look like, respectively.

:::align{center} :::

Consider the first test case.

For k=1k = 1, both xor values can be obtained. For example, the set S={5}S = \{5\} is valid and has xor 00. And the set S={3}S = \{3\} is also valid and has xor 11. Therefore, ans1=2ans_1 = 2.

For k=3k = 3, both xor values can also be obtained. For example, the set S={2,3,5}S = \{2, 3, 5\} is valid because together with vertex 22 all vertices in its subtree are chosen. Its xor is $a_2 \oplus a_3 \oplus a_5 = 0 \oplus 1 \oplus 0 = 1$. And the set S={3,4,5}S = \{3, 4, 5\} is also valid because together with vertex 44 all vertices in its subtree are chosen. Its xor is $a_3 \oplus a_4 \oplus a_5 = 1 \oplus 1 \oplus 0 = 0$. Therefore, ans3=2ans_3 = 2.

Thus, the answer for the first test case is 2 1 2 0 12\ 1\ 2\ 0\ 1.

Consider the second test case.

In this test case, both xor values can be obtained only for k=6k = 6.

For example, the set S={3,4,6,7,8,9}S = \{3, 4, 6, 7, 8, 9\} is valid. Its xor is $a_3 \oplus a_4 \oplus a_6 \oplus a_7 \oplus a_8 \oplus a_9 = 0 \oplus 1 \oplus 1 \oplus 1 \oplus 1 \oplus 1 = 1$.

And the set S={4,5,6,7,8,9}S = \{4, 5, 6, 7, 8, 9\} is also valid. Its xor is $a_4 \oplus a_5 \oplus a_6 \oplus a_7 \oplus a_8 \oplus a_9 = 1 \oplus 1 \oplus 1 \oplus 1 \oplus 1 \oplus 1 = 0$. Therefore, ans6=2ans_6 = 2.

Thus, the answer for the second test case is 1 0 1 0 1 2 0 0 01\ 0\ 1\ 0\ 1\ 2\ 0\ 0\ 0.

Scoring

A leaf is a vertex that has no children.

A bamboo is a tree in which every vertex has at most one child.

  • (22 points): n20\sum n \le 20;
  • (33 points): pi=1p_i=1;
  • (55 points): n3108\sum n^3 \le 10^8;
  • (88 points): n2108\sum n^2 \le 10^8;
  • (1212 points): the total number of leaves over all test cases does not exceed 100100;
  • (88 points): after removing vertex 11, each component is a bamboo, and there are at most two such components;
  • (99 points): after removing vertex 11, each component is a bamboo;
  • (99 points): all trees are full binary trees, that is, n=2q1n = 2^q - 1 for some integer qq, and for every vertex i>1i > 1 we have pi=i2p_i = \left\lfloor \frac{i}{2} \right\rfloor;
  • (2222 points): n2105n \le 2 \cdot 10^5 for each test case;
  • (2222 points): no additional constraints.