atcoder#ARC148C. [ARC148C] Lights Out on Tree

[ARC148C] Lights Out on Tree

Score : 500500 points

Problem Statement

We have a rooted tree with NN vertices numbered 11 to NN. Vertex 11 is the root, and the parent of vertex ii is vertex PiP_i. There are NN coins with heads and tails, one on each vertex. Additionally, there are NN buttons numbered 11 to NN. Pressing button nn flips all coins on the vertices in the subtree rooted at vertex nn.

Process QQ queries described below.

In the ii-th query, you are given a vertex set of size MiM_i: $S_i = \lbrace v_{i,1}, v_{i,2},\dots, v_{i,M_i} \rbrace$. Now, the coins on the vertices in SiS_i are facing heads-up, and the others are facing tails-up. In order to make all NN coins face tails-up by repeatedly choosing a button and pressing it, at least how many button presses are needed? Print the answer, or 1-1 if there is no way to make all the coins face tails-up.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1Pi<i1 \leq P_i \lt i
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 1Mi1 \leq M_i
  • i=1QMi2×105\displaystyle \sum_{i=1}^Q M_i \leq 2 \times 10^5
  • 1vi,jN1 \leq v_{i,j} \leq N
  • vi,1,vi,2,,vi,Miv_{i,1}, v_{i,2},\dots,v_{i,M_i} are pairwise different.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN QQ

P2P_2 P3P_3 \dots PNP_N

M1M_1 v1,1v_{1,1} v1,2v_{1,2} \dots v1,M1v_{1,M_1}

M2M_2 v2,1v_{2,1} v2,2v_{2,2} \dots v2,M2v_{2,M_2}

\vdots

MQM_Q vQ,1v_{Q,1} vQ,2v_{Q,2} \dots vQ,MQv_{Q,M_Q}

Output

Print QQ lines. The ii-th line should contain the answer to the ii-th query.

6 6
1 1 2 2 5
6 1 2 3 4 5 6
3 2 5 6
1 3
3 1 2 3
3 4 5 6
4 2 3 4 5
1
2
1
3
2
3

For the first query, you can satisfy the requirement in one button press, which is the minimum needed, as follows.

  • Press button 11, flipping the coins on vertices 1,2,3,4,5,61,2,3,4,5,6.

For the second query, you can satisfy the requirement in two button presses, which is the minimum needed, as follows.

  • Press button 44, flipping the coin on vertex 44.
  • Press button 22, flipping the coins on vertex 2,4,5,62,4,5,6.