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

建议使用的浏览器:

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

5923:Prediction

题目描述
There is a graph $G =\left \langle V_G, E_G \right \rangle $  with  $|V_G| = n$ and $|E_G| = m$, and a magic tree $T =\left \langle V_T, E_T \right \rangle)$ rooted at 1, which contains m vertices.

Each vertex of the magic tree corresponds to an edge in the original graph G and each edge occurs in the magic tree exactly once.

Each query includes a set $S(S \subseteq V_T)$, and you should tell Mr. Frog the number of components in the modified graph $G^` = (V_G, E_G^`)$, where $E_G^`$ is a set of edges in which every edge corresponds to a vertex v in magic tree T satisfying at least one of the following two conditions:

$\bullet v \in S$.
$\bullet $v is an ancestor of some vertices in S.

Note that the queries are independent, and namely one query will not influence another.
输入解释
The input contains several test cases and the first line of the input data is an integer T, denoting the number of test cases.

For each test case, the first line contains two integers n and m($1 \leq n \leq 500,1 \leq m \leq 10000$), where n is the number of vertices and m is the number of edges.

The second line contains m - 1 integers describing the magic tree, i-th integer represents the parent of the (i + 1)-th vertex.

Then the following m lines describe the edges of the graph G. Each line contains two integers u and v indicating the two ends of the edge.

The next line contains only one integer q($1 \leq q \leq 50000$), indicating the number of queries.

Then the following q lines represent queries,  i-th line represents the i-th query, which contains an integer $k_i$ followed by $k_i$ integers representing the set $S_i$.

It is guarenteed that $\sum_{i=1}^{q}k_i \leq 300000$.
输出解释
For each case, print a line "Case #x:", where x is the case number (starting from 1).

For each query, output a single line containing only one integer representing the answer, namely the number of components.
输入样例
1
5 4
1 1 3
1 2
2 3
3 4
4 5
3
1 2
2 2 3
2 2 4
输出样例
Case #1:
3
2
1
提示
magic tree and the original graph in the sample are:



In the first query, S = {2} and the modified graph G' = {{1, 2, 3, 4}, {(1, 2), (2, 3)}}, thus the number of the components in the modified graph is 3.
In the second query, S = {1, 2, 3}, where 1 is the ancestor of 2 (and 3) in the magic tree, and the modified graph G'' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3, 4)}},
 therefore the number of the components in the modified graph is 2.
In the third query, S = {1, 2, 3, 4}, where 1 is the ancestor of 2 (and 4), 3 is the ancestor of 4, and the modified graph G' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3,4), (4, 5)}}, 
therefore the answer equals to 1.
来自杭电HDUOJ的附加信息
Recommend wange2014

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

源链接: HDU-5923

最后修改于 2020-10-25T23:26:45+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
5000/2500MS(Java/Others) 65536/65536K(Java/Others)