#P4606. [SDOI2018] 战略游戏

    ID: 3550 远端评测题 10000ms 489MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2018各省省选山东O2优化深度优先搜索,DFS割点虚树圆方树

[SDOI2018] 战略游戏

Description

With the NOI Qualifier approaching, carefree Xiao Q has no desire to practice problems. He coaxes Xiao C to slack off with him and play a strategic game.

The map of this strategic game consists of nn cities and mm undirected roads connecting these cities. From any city, it is always possible to reach any other city by following the roads.

Xiao C has already occupied at least two cities. Xiao Q may destroy one city not occupied by Xiao C, and simultaneously destroy all roads incident to that city. If, after destroying this city, there exist two cities uu and vv occupied by Xiao C such that starting from uu it is impossible to reach vv along the roads, then Xiao Q wins that round.

Xiao Q and Xiao C play qq rounds in total. In each round, you are given the set SS of cities occupied by Xiao C. You need to help Xiao Q count how many cities, if destroyed, would allow him to win that round.

Input Format

The first line contains a single integer TT, the number of test cases.

For each test case:

  • The first line contains two integers nn and mm, the number of cities and the number of roads.
  • The next mm lines each contain two integers uu and vv (1u<vn1 \le u < v \le n), indicating there is a road between city uu and city vv. There may be multiple roads between the same pair of cities.
  • The (m+1)(m + 1)-th line contains an integer qq, the number of game rounds.
  • The next qq lines: each line first gives an integer S|S| (2Sn2 \le |S| \le n), the number of cities occupied by Xiao C, followed by S|S| integers S1,S2,,SSS_1, S_2, \ldots, S_{|S|} (1S1<S2<<SSn1 \le S_1 < S_2 < \cdots < S_{|S|} \le n), denoting the cities occupied by Xiao C.

Output Format

For each round, output a single line containing one integer: the number of cities whose destruction would allow Xiao Q to win that round.

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

0
1
3
0
1
2
3

Hint

Constraints

  • 1T101 \le T \le 10.
  • 2n1052 \le n \le 10^5 and n1m2×105n - 1 \le m \le 2 \times 10^5.
  • 1q1051 \le q \le 10^5.
  • For each test case, S2×105\sum |S| \le 2 \times 10^5.

Subtasks

  • Subtask 1 (30 points): For each test case, S20\sum |S| \le 20.
  • Subtask 2 (45 points): For each query, S=2|S| = 2.
  • Subtask 3 (25 points): No additional constraints.

Translated by ChatGPT 5