CF1741G.Kirill and Company

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Kirill lives on a connected undirected graph of nn vertices and mm edges at vertex 11. One fine evening he gathered ff friends, the ii-th friend lives at the vertex hih_i. So all friends are now in the vertex 11, the ii-th friend must get to his home to the vertex hih_i.

The evening is about to end and it is time to leave. It turned out that kk (k≤6k \le 6) of his friends have no cars, and they would have to walk if no one gives them a ride. One friend with a car can give a ride to any number of friends without cars, but only if he can give them a ride by driving along one of the shortest paths to his house.

For example, in the graph below, a friend from vertex hi=5h_i=5 can give a ride to friends from the following sets of vertices: [2,3][2, 3], [2,4][2, 4], [2][2], [3][3], [4][4], but can't give a ride to friend from vertex 66 or a set [3,4][3, 4].

The vertices where friends without cars live are highlighted in green, and with cars — in red.

Kirill wants as few friends as possible to have to walk. Help him find the minimum possible number.

基里尔住在一个包含 nn 个顶点和 mm 条边的连通无向图上,起始位置为顶点 11。某天傍晚,他召集了 ff 位朋友,其中第 ii 位朋友住在顶点 hih_i。因此,所有朋友当前均位于顶点 11,而第 ii 位朋友需前往其住所顶点 hih_i。

夜晚即将结束,是时候离开了。结果发现,他的 kk(k≤6k \le 6)位朋友没有车,若无人搭载,他们将不得不步行。一位有车的朋友可以搭载任意数量的无车朋友,但前提是:他必须能沿着一条通往自己家的最短路径,顺路将这些朋友送达各自住所。

例如,在下图中,一位住在顶点 hi=5h_i = 5 的朋友可以搭载以下顶点集合中的朋友:[2,3][2, 3]、[2,4][2, 4]、[2][2]、[3][3]、[4][4];但无法搭载住在顶点 66 的朋友,也无法同时搭载住在顶点 33 和 44 的朋友(即集合 [3,4][3, 4])。

图中绿色顶点表示无车朋友的住所,红色顶点表示有车朋友的住所。

基里尔希望尽可能少的朋友步行。请帮助他找出最少可能的步行人数。

输入格式

The first line of input data contains an integer tt (1≤t≤1031 \le t \le 10^3) — the number of test cases in the test.

The first line of the test case contains two integers nn and mm (2≤n≤1042 \le n \le 10^4, $n-1 \le m \le min (10^4, $$ \frac{n \cdot (n - 1)}{2} $$)$) — the number of vertices and edges, respectively.

The next mm lines of the test case contain a description of the edges, two integers each uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) — indexes of vertices connected by an edge. It is guaranteed that there is at most one edge between any pair of vertices (i.e. no multiple edges in the graph).

Then follows line containing the number ff (1≤f≤1041 \le f \le 10^4) — the number of Kirill's friends.

The next line of the test case contains ff integers: h1,h2,…,hfh_1, h_2, \dots, h_f (2≤hi≤n2 \le h_i \le n) — the vertices in which they live. Some vertices may be repeated.

The next line of the set contains the number kk (1≤k≤min(6,f)1 \le k \le min(6, f)) — the number of friends without cars.

The last line of each test case contains kk integers: p1,p2,…,pkp_1, p_2, \dots, p_k (1≤pi≤f1 \le p_i \le f, pi<pi+1p_i \lt p_{i+1}) — indexes of friends without cars.

It is guaranteed that the sum of nn over all cases does not exceed 10410^4, as well as the sums of mm and ff.

输入数据的第一行包含一个整数 tt(1≤t≤1031 \le t \le 10^3)—— 表示测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤1042 \le n \le 10^4,n−1≤m≤min⁡(104,n⋅(n−1)2)n-1 \le m \le \min\left(10^4, \frac{n \cdot (n - 1)}{2}\right))—— 分别表示图的顶点数和边数。

接下来的 mm 行描述图中的边,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \ne v)—— 表示由该边连接的两个顶点的编号。保证任意一对顶点之间至多存在一条边(即图中无重边)。

随后一行包含一个整数 ff(1≤f≤1041 \le f \le 10^4)—— 表示基里尔的朋友数量。

测试用例的下一行包含 ff 个整数:h1,h2,…,hfh_1, h_2, \dots, h_f(2≤hi≤n2 \le h_i \le n)—— 表示每位朋友所居住的顶点编号。某些顶点可能被重复列出。

接下来一行包含一个整数 kk(1≤k≤min⁡(6,f)1 \le k \le \min(6, f))—— 表示没有汽车的朋友人数。

每个测试用例的最后一行包含 kk 个整数:p1,p2,…,pkp_1, p_2, \dots, p_k(1≤pi≤f1 \le p_i \le f,且 pi<pi+1p_i < p_{i+1})—— 表示没有汽车的朋友在前述 ff 人列表中的索引。

保证所有测试用例的 nn 之和不超过 10410^4,同理,所有测试用例的 mm 之和与 ff 之和也不超过 10410^4。

输出格式

Output tt lines, each of which contains the answer to the corresponding test case. As an answer, output a single integer — the minimum possible number of friends who will have to walk.

输出 tt 行,每行包含对应测试用例的答案。作为答案,输出一个整数——必须步行的朋友的最少人数。

输入输出样例

  • 输入#1

    3
    6 7
    1 2
    2 3
    2 4
    3 5
    4 5
    3 6
    6 5
    5
    2 3 4 5 6
    4
    1 2 3 5
    6 7
    1 2
    2 3
    2 4
    3 5
    4 5
    3 6
    6 5
    6
    2 3 4 5 6 5
    4
    1 2 3 5
    4 4
    1 2
    1 3
    2 3
    3 4
    3
    3 4 2
    2
    1 3

    输出#1

    2
    1
    1
  • 输入#2

    3
    2 1
    1 2
    3
    2 2 2
    3
    1 2 3
    3 3
    1 2
    1 3
    2 3
    4
    2 2 2 3
    3
    1 2 4
    4 4
    3 1
    3 2
    1 4
    2 4
    5
    3 2 2 4 2
    3
    1 3 4

    输出#2

    3
    1
    0

说明/提示

The first test case of the first example is explained in the statement.

In the second test case of the first example, two friends with cars live at vertex 55, one can give a ride to friends from vertices 22 and 33, and the second from vertex 44, only a friend from vertex 66 will have to walk.

第一个示例的第一个测试用例已在题面中说明。

在第一个示例的第二个测试用例中,有两位拥有汽车的朋友住在顶点 55;其中一位可以顺路搭载来自顶点 22 和 33 的朋友,另一位可以顺路搭载来自顶点 44 的朋友,只有来自顶点 66 的朋友需要步行。

输入解题思路,AI测评打分。不知道怎么写?

首页