p518.ans1
======================
3
1 3 4

=================
p518.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, k;
vector<vector<int>> graph;
vector<int> path;

void read() {
    cin >> n;
    graph.assign(n, vector<int>(n));
    cin >> graph;
    cin >> k;
    path.resize(k);
    cin >> path;
}

void solve() {
    // BFS over states (node, pos): we are at intersection node having
    // matched the first pos road lengths of the route. From (node, pos) we
    // step to any next_node whose connecting road length graph[node][next]
    // equals path[pos], advancing to (next, pos + 1). visited[node][pos]
    // prevents revisiting a state. Every state reaching pos == k marks node
    // as a possible secret place. We collect those nodes (1-indexed),
    // sorted ascending.

    deque<pair<int, int>> queue = {{0, 0}};
    vector<vector<bool>> visited(n, vector<bool>(k + 1, false));
    visited[0][0] = true;
    set<int> possible;

    while(!queue.empty()) {
        auto [node, pos] = queue.front();
        queue.pop_front();

        if(pos == k) {
            possible.insert(node);
            continue;
        }

        for(int next_node = 0; next_node < n; next_node++) {
            if(graph[node][next_node] == path[pos] &&
               !visited[next_node][pos + 1]) {
                visited[next_node][pos + 1] = true;
                queue.push_back({next_node, pos + 1});
            }
        }
    }

    cout << possible.size() << '\n';
    if(!possible.empty()) {
        bool first = true;
        for(int x: possible) {
            cout << (first ? "" : " ") << x + 1;
            first = false;
        }
        cout << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p518.in1
======================
4
0 1 2 0
1 0 1 0
2 1 0 2
0 0 2 0
3
1 1 2

=================
p518.py
======================
from collections import deque


def solve():
    n = int(input())
    graph = [list(map(int, input().split())) for _ in range(n)]
    k = int(input())
    path = list(map(int, input().split()))

    queue = deque([(0, 0)])
    visited = [[False] * (k + 1) for _ in range(n)]
    visited[0][0] = True
    possible = set()

    while queue:
        node, pos = queue.popleft()

        if pos == k:
            possible.add(node)
            continue

        for next_node in range(n):
            if (
                graph[node][next_node] == path[pos]
                and not visited[next_node][pos + 1]
            ):
                visited[next_node][pos + 1] = True
                queue.append((next_node, pos + 1))

    possible = sorted(list(possible))
    print(len(possible))
    if possible:
        print(*[x + 1 for x in possible])


solve()

=================
statement.txt
======================
518. Kidnapping
Time limit per test: 0.5 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

Berland's Police has a serious problem. A foreign ambassador arrived to Berland with an important mission, and his daughter was kidnapped just from the Royal Palace! Inspired by adventures of Erast Fandorin, the Police Chief developed the following ingenious plan.

The ambassador agrees to pay ransom, but only if the kidnappers allow his servant to visit the girl and ensure that she is alive. The kidnappers take the blindfolded servant into a coach and transport him to the secret place, where they keep the ambassador's daughter. Certainly, the role of the servant is certainly played by a secret agent of the Police. The Police Chief knows that when the coach is moving, the wheels are creaking once on each full rotation. So, by counting the number of creaks and multiplying it by the length of the rim, one can easily calculate the distance covered by the coach.

In spite of this brilliant idea, the affair turned to be much more difficult than it could be in a detective story. There are n intersections in the city numbered from 1 to n, some pairs of intersections are connected by bidirectional roads. The kidnappers agreed to take the "servant" to the secret place, and the servant is quite sure that this place is located at one of the intersections. Also the agent has calculated the lengths of roads between each pair of consecutive intersections on the route passed by the coach. But during the trip the agent was concentrated on counting creaks, so he could not remember in which directions the coach turned at the intersections.

Now the route probably couldn't be restored uniquely! Moreover, the agent has a suspicion that the kidnappers could intentionally pass the same intersection or even the same road more than once to confuse the Police.

Your task is to determine all possible locations of the secret place, given that the trip starts at the intersection number 1.

Input
The first line of the input contains a single integer n (2 ≤ n ≤ 200). Each of the next n lines contains n integers each. The i-th number in the j-th line lij is the length of the road between the i-th and the j-th intersections. If lij = 0 then the road doesn't exist.

It is guaranteed that 0 ≤ lij ≤ 200, lii = 0 and lij = lji. The next line contains one integer k (1 ≤ k ≤ 200) — the number of roads passed by the couch. The following line contains k integers r1, r2,..., rk (1 ≤ ri ≤ 200) — the lengths of roads between each pair of consecutive intersections on the route passed by the coach from the starting point to the secret place.

Output
To the first line of the output write m — the number of all possible locations of the secret place. The second line should contain the numbers of intersections in increasing order separated by spaces.

If there are no possible locations of the secret place, the output must contain the only integer 0.

Example(s)
sample input
sample output
4
0 1 2 0
1 0 1 0
2 1 0 2
0 0 2 0
3
1 1 2
3
1 3 4

=================
