#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 to , if after removing this edge there exists a node that can reach both and , then such an edge does not appear in the graph.
In addition, every node except node has exactly one incoming edge.
Xiao A wants to know, for all nodes reachable from a node (including itself), how many distinct gift types are there.
Input Format
- The first line contains the number of gift points ().
- The next line contains integers, representing the parent (in-neighbor) of nodes respectively; that is, the -th integer is the parent of node .
- The next line contains integers, where the -th integer is the gift ID of node .
- The next line contains , the number of queries.
- Each of the next 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 can reach nodes , with three gift types .
- Node can reach nodes , with two gift types .
Constraints:
| Test point ID | (color) | ||
|---|---|---|---|
Translated by ChatGPT 5
京公网安备 11011102002149号