#P3906. Geodetic集合

Geodetic集合

Description

A graph G\text G is an undirected connected graph with no self-loops, and there is at most one edge between any two vertices. We define the shortest path between vertices vv and uu as a path from vv to uu that uses the fewest edges. Every vertex that lies on some shortest vv-uu path is called a vv-uu Geodetic vertex, and the set of these vertices is denoted by I(v,u)I(v,u).

We call the set I(v,u)I(v,u) a Geodetic set.

For example, in the figure below, I(2,5)={2,3,4,5}I(2,5)=\{2,3,4,5\}, I(1,5)={1,3,5}I(1,5)=\{1,3,5\}, and I(2,4)={2,4}I(2,4)=\{2,4\}.

Given a graph G\text G and several pairs v,uv, u, please compute I(v,u)I(v,u) for each pair.

Input Format

The first line contains two integers n,mn, m, representing the number of vertices and edges of G\text G (vertices are numbered 1n1 \sim n).

The next mm lines each contain two integers a,ba, b, indicating that there is an undirected edge between vertices aa and bb.

The (m+2)(m+2)-th line contains an integer kk, representing the number of given pairs.
The next kk lines each contain two integers v,uv, u, the start and end vertices of each pair.

Output Format

Output kk lines. For each pair v,uv, u in the input, output in one line all vertex indices in I(v,u)I(v,u) in increasing order of vertex indices.

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

Hint

For all testdata, 1n401 \le n \le 40, 1m,kn(n1)21 \le m, k \le \frac{n(n-1)}{2}.

Translated by ChatGPT 5