#P1204C. Anna, Svyatoslav and Maps
Anna, Svyatoslav and Maps
No submission language available for this problem.
Description
The main characters have been omitted to be short.
You are given a directed unweighted graph without loops with vertexes and a path in it (that path is not necessary simple) given by a sequence of vertexes; for each there is an arc from to .
Define the sequence of vertexes as good, if is a subsequence of , , , and is one of the shortest paths passing through the vertexes , , in that order.
A sequence is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero or all) elements. It is obvious that the sequence is good but your task is to find the shortest good subsequence.
If there are multiple shortest good subsequences, output any of them.
The first line contains a single integer () — the number of vertexes in a graph.
The next lines define the graph by an adjacency matrix: the -th character in the -st line is equal to if there is an arc from vertex to the vertex else it is equal to . It is guaranteed that the graph doesn't contain loops.
The next line contains a single integer () — the number of vertexes in the path.
The next line contains integers () — the sequence of vertexes in the path. It is guaranteed that for any there is an arc from to .
In the first line output a single integer () — the length of the shortest good subsequence. In the second line output integers , , () — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.
Input
The first line contains a single integer () — the number of vertexes in a graph.
The next lines define the graph by an adjacency matrix: the -th character in the -st line is equal to if there is an arc from vertex to the vertex else it is equal to . It is guaranteed that the graph doesn't contain loops.
The next line contains a single integer () — the number of vertexes in the path.
The next line contains integers () — the sequence of vertexes in the path. It is guaranteed that for any there is an arc from to .
Output
In the first line output a single integer () — the length of the shortest good subsequence. In the second line output integers , , () — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.
Samples
Note
Below you can see the graph from the first example:
The given path is passing through vertexes , , , . The sequence is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes , and in that order is . Note that subsequences and aren't good because in both cases the shortest path passing through the vertexes of these sequences is .
In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes.
In the fourth example, the paths and are the shortest paths passing through the vertexes and .