8486: Static Query on Tree

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

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 any city in set A, any city in set B, and can reach
any city in set C?
There are test cases, and each case has queries.

输入格式

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 .
1 ≤ T ≤ 20, 1 ≤ n,q,∣A∣,∣B∣,∣C∣ ≤ 2 × 10 ,
∑ n ≤ 2 × 10 ,
∑ q ≤ 2 × 10 ,

输出格式

In each case, print integers, one integer per line, -th integer indicates the answer of -th query.

输入样例 复制

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