#K42632. Count Flower Spots
Count Flower Spots
Count Flower Spots
You are given a series of test cases representing a garden of spots connected by bidirectional pathways. Each spot has an identifier (a string) and a list of flower types (each represented by an integer). Some spots may not have any flower types. For each test case, you will also be given a set of queries. Each query gives a flower type and a starting spot. Your task is to determine the number of spots that are reachable from the starting spot (including the starting spot itself) and that contain the queried flower type.
The connections between spots form an undirected graph. To solve the problem, you may use a depth-first search or breadth-first search to traverse the connected component. Formally, for each query, if \(G=(V,E)\) is the graph and \(v\) is the starting spot, let \(R(v)\) denote the set of reachable spots from \(v\). Then, for a given flower type \(f\), you must count the number of spots \(u \in R(v)\) such that the flower type \(f\) is present in the set of flowers at \(u\).
Note: Each test case is processed independently.
inputFormat
The input is read from standard input (stdin) and has the following format:
T</p>// For each test case: N M // Next N lines: each line describes a spot. The first token is the spot identifier (a string), followed by an integer K representing the number of flower types at that spot, and then K integers denoting the flower types. If K is 0, no flower types follow. spot_id K [flower_1 flower_2 ... flower_K]
// Next M lines: each line contains two spot identifiers, representing a bidirectional pathway. spot_id1 spot_id2
// Next line: Q, the number of queries. Q // Next Q lines: each line contains an integer (flower type) and a spot identifier (starting spot). flower_type starting_spot
T is the number of test cases. The output for each query should be printed on a separate line in the order they appear in the input.
outputFormat
For each query, print a single integer on a new line representing the number of spots (in the connected component of the starting spot) that contain the queried flower type.
## sample1
4 3
A 2 1 2
B 2 2 3
C 2 1 4
D 2 3 4
A B
A C
C D
2
1 A
3 B
2
2
</p>