#P9615. Volunteer Distribution and Island Connectivity
Volunteer Distribution and Island Connectivity
Volunteer Distribution and Island Connectivity
The Scandinavian Ministry of Higher Education is planning a large-scale experiment to study how the rock styles and preferences of volunteers, distributed over an archipelago of uninhabited islands, mutually influence each other over a relatively long period. Each island, if inhabited, has residents who can influence one another. Some pairs of islands are close enough for direct influence, while in other cases the distance prevents any direct influence. However, indirect influence can occur through a chain of other inhabited islands.
For each proposal, you are given the list of islands that will be inhabited. Two groups of islands are considered independent if no island in one group can influence any island in the other group (even indirectly). In other words, the induced subgraph of inhabited islands is divided into several connected components, and each connected component represents one independent resident group.
Formally, you are given an undirected graph with \(n\) islands (vertices) and \(m\) pairs of directly connected islands (edges). For each proposal, an induced subgraph is obtained by selecting a subset of islands that are inhabited. Your task is to determine the number of connected components in this induced subgraph. Note that if no islands are inhabited in a proposal, the answer is 0.
inputFormat
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5\), \(0 \le m \le 10^5\)), representing the number of islands and the number of direct influence pairs.
The following \(m\) lines each contain two integers \(u\) and \(v\) (\(1 \le u, v \le n,\ u \ne v\)) indicating that island \(u\) and island \(v\) are close enough for direct influence.
The next line contains an integer \(Q\) (\(1 \le Q \le 10^5\)), the number of proposals.
Each of the following \(Q\) lines starts with an integer \(k\) (\(0 \le k \le n\)), representing the number of inhabited islands in that proposal, followed by \(k\) distinct integers denoting the indices of the inhabited islands.
outputFormat
For each proposal, output a single integer representing the number of independent resident groups (i.e., the number of connected components in the induced subgraph).
sample
5 3
1 2
2 3
4 5
3
3 1 3 5
4 1 2 3 4
5 1 2 3 4 5
3
2
2
</p>