#P9137. Static Query on Tree
Static Query on Tree
Source: Super League of Chinese College Students Algorithm Design 2022, Contest 2 by HDU.
Problem Description
In country X, there are cities and one-way roads, and all city can reach city 1. One query will give 3 sets of cities A, B, C. Alice will choose a city x in set A, choose a city z in set C, and walk from x to z (if x can reach z). Bob will choose a city y in set B, and walk from y to z (if y can reach z). How many cities can possibly be the city where Alice and Bob meet each other?
In other words, how many cities can be reached from at least one city in set A, and can be reached from at least one city in set B, and can reach at least one city in set C?
There are test cases, and each case has queries.
Input
First line is one integer , indicating test cases. In each case:
First line is 2 integers , indicating cities and queries.
Next line is integers , the -th integer indicates the road from city to city .
Next is queries, in each query:
First line is 3 integer , indicating the size of set A, B, C.
Next line is integers, indicating the set .
Next line is integers, indicating the set .
Next line is integers, indicating the set .
for all cases for all queries in all cases .
Output
In each case, print integers, one integer per line, -th integer indicates the answer of -th query.
Sample
1
7 3
1 1 2 2 3 3
2 1 1
1 2
4
1
4 4 3
4 5 6 7
4 5 6 7
2 4 6
2 1 1
4 5
6
1
2
4
1
For the firstquery, city .
( can be reached from (A), (B), can reach (C))
( can be reached from (A), (B), can reach (C))
For the second query, city .
For the third query, only city .