Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)
E
Wooden Game
Codeforces
2000
原题
← DF →
翻译中…

You are given a forest of $k$ rooted trees$^{\text{∗}}$. Lumberjack Timofey wants to cut down the entire forest by applying the following operation:

翻译中…
  • Select a subtree$^{\text{†}}$ of any vertex of one of the trees and remove it from the tree.
翻译中…

Timofey loves bitwise operations, so he wants the bitwise OR of the sizes of the subtrees he removed to be maximum. Help him and find the maximum result he can obtain.

翻译中…

$^{\text{∗}}$ A tree is a connected graph without cycles, loops, or multiple edges. In a rooted tree, a selected vertex is called a root. A forest is a collection of one or more trees.$^{\text{†}}$ The subtree of a vertex $v$ is the set of vertices for which $v$ lies on the shortest path from this vertex to the root, including $v$ itself.

翻译中…

Input

翻译中…

Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then follows the description of the test cases.

翻译中…

The first line of each test case contains a single integer $k$ ($1 \leq k \leq 10^6$) — the number of trees in the forest.

翻译中…

This is followed by a description of each of the $k$ trees:

翻译中…

The first line contains a single integer $n$ ($1 \leq n \leq 10^6$) — the size of the tree. The vertices of the tree are numbered with integers from $1$ to $n$. The root of the tree is vertex number $1$.

翻译中…

The second line contains $n - 1$ integers $p_2, p_3, \ldots p_n$ ($1 \leq p_i \lt i$), where $p_i$ — the parent of vertex $i$.

翻译中…

It is guaranteed that the sum of $k$ and $n$ for all sets of input data does not exceed $10^6$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the maximum result that can be obtained.

翻译中…

Note

翻译中…

In the second test case, the trees look like this:

翻译中…

image

翻译中…

The first operation removes the entire second tree.

翻译中…

image

翻译中…

The second operation removes vertex $4$ from the first tree.

翻译中…

image

翻译中…

The third operation removes the first tree. The result is $6|1|3 = 7$ ($|$ denotes bitwise OR).

翻译中…

In the third test case, the entire tree needs to be removed.

翻译中…

样例

Input #1
3
1
1

2
4
1 2 2
6
1 1 3 1 3
1
10
1 2 2 1 1 5 7 6 4
Output #1
1
7
10
InputCopy OutputCopy
题目信息
题号
1994E
时间限制
2 s
内存限制
256 MB
标签
位运算
贪心
数学
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。