当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

7150:Static Query on Tree

题目描述
In country X, there are $n$ cities and $n - 1$ 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 $T$ test cases, and each case has $q$ queries.
输入解释
First line is one integer $T$, indicating $T$ test cases. In each case:

First line is 2 integers $n, q$, indicating $n$ cities and $q$ queries.

Next line is $n - 1$ integers $r_1, r_2, \ldots, r_{n-1}$, the $i$-th integer indicates the road from city $i + 1$ to city $r_i$.

Next is $q$ queries, in each query:

First line is 3 integer $|A|,|B|,|C|$, indicating the size of set A, B, C.

Next line is $|A|$ integers, indicating the set $A$.

Next line is $|B|$ integers, indicating the set $B$.

Next line is $|C|$ integers, indicating the set $C$.

$1 \le T \le 20,$ $1\le n, q, |A|, |B|, |C| \le 2\times 10^5,$ for all cases $\sum n\le 2\times 10^5,$ $\sum q\le 2\times 10^5,$ for all queries in all cases $\sum |A|+\sum |B|+\sum|C|\le 2\times 10^5$.
输出解释
In each case, print $q$ integers, one integer per line, $i$-th integer indicates the answer of $i$-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
来自杭电HDUOJ的附加信息
Hint For the first query, city 1, 2.(1 can be reached from 1 (A), 4 (B), can reach 1(C))(2 can be reached from 2 (A), 4 (B), can reach 1(C))For the second query, city 2, 4, 5, 6.For the third query, only city 1.

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-7150

最后修改于 2022-09-15T06:17:02+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
2000/1000MS(Java/Others) 262144/262144K(Java/Others)