#K34767. Treasure Hunt Routing

    ID: 25383 Type: Default 1000ms 256MiB

Treasure Hunt Routing

Treasure Hunt Routing

You are given an undirected graph representing a forest. There is a starting location \(s\) and a list of treasure locations. The forest is modeled as \(n\) nodes with \(m\) edges. Some of the nodes contain treasures. Two or more treasures are considered to be in the same group if they are connected via a sequence of edges, ignoring non‐treasure nodes.

Your task is to plan one or more routes. Each route starts at \(s\), visits a group (i.e. a connected component) of treasure nodes (using any order, but the connectivity among treasures is ensured by the graph), and finally returns to \(s\). If there are no treasures, output 0 routes.

Input Format:
The first line contains four integers \(n\), \(t\), \(s\), and \(m\) where \(n\) is the number of nodes, \(t\) is the number of treasures, \(s\) is the starting node (0-indexed), and \(m\) is the number of edges in the graph. The second line contains \(t\) space-separated integers denoting the treasure nodes (if \(t = 0\), this line will be empty). Each of the next \(m\) lines contains two integers \(u\) and \(v\), indicating that there is an undirected edge between node \(u\) and node \(v\).

Note: It is guaranteed that \(0 \le s < n\) and that the graph is undirected. Use a breadth-first search (BFS) from \(s\) to compute distances, and then use a depth-first search (DFS) to group treasures into connected components.

inputFormat

The first line contains four integers \(n\), \(t\), \(s\), and \(m\).
The second line contains \(t\) integers representing the treasure nodes (if \(t = 0\), the line is empty).
Each of the next \(m\) lines contains two integers \(u\) and \(v\) representing an edge.

outputFormat

Output the number of routes in the first line. For each route, output one line containing the route (a sequence of space‐separated node numbers) starting and ending at \(s\).

## sample
6 3 0 5
1 3 5
0 1
0 2
1 3
1 4
2 5
2

0 1 3 0 0 2 5 0

</p>