#P2720. 小A的礼物

小A的礼物

Description

Xiao A went to school to attend an event, and the school gives out gifts. The school has many gift points.

Each gift point has outgoing roads to other gift points and a gift type.

To prevent picking up gifts multiple times, these roads are directed and acyclic.

Moreover, for any edge from aa to bb, if after removing this edge there exists a node cc that can reach both aa and bb, then such an edge does not appear in the graph.

In addition, every node except node 11 has exactly one incoming edge.

Xiao A wants to know, for all nodes reachable from a node SS (including SS itself), how many distinct gift types are there.

Input Format

  • The first line contains the number of gift points nn (n100000n \le 100000).
  • The next line contains n1n - 1 integers, representing the parent (in-neighbor) of nodes 2,,n2, \dots, n respectively; that is, the ii-th integer is the parent of node i+1i + 1.
  • The next line contains nn integers, where the ii-th integer is the gift ID of node ii.
  • The next line contains mm, the number of queries.
  • Each of the next mm lines contains one node ID, representing a query node.

Output Format

For each query, output the number of distinct gifts.

4
1 2 2
1 2 3 3
2
1
2
3
2

Hint

Sample Explanation:

  • Node 11 can reach nodes (1,2,3,4)(1, 2, 3, 4), with three gift types (1,2,3)(1, 2, 3).
  • Node 22 can reach nodes 2,3,42, 3, 4, with two gift types 2,32, 3.

Constraints:

Test point ID nn mm cic_i (color)
11 102\le 10^2
232 \sim 3 103\le 10^3
454 \sim 5 104\le 10^4 20\le 20
66 5×104\le 5 \times 10^4 5×104\le 5 \times 10^4 5×104\le 5 \times 10^4
787 \sim 8 105\le 10^5 6×104\le 6 \times 10^4

Translated by ChatGPT 5