Problem:
真っ暗な部屋
目を覚ますと、A君は真っ暗な部屋の中にいた。
どうやらA君は
N
個の部屋から構成されたダンジョンに迷い込んでしまったようだ。
あなたはA君がどの部屋に迷い込んだのかを知ることはできなかったが、幸いにもダンジョンのマップを手に入れることができた。
A君の進むべき道を示し明るい部屋に導こう。
N
個の部屋のうち
M
個の部屋が真っ暗な部屋であり、それぞれ
D_1
,
D_2
, ...,
D_M
番目の部屋が真っ暗な部屋であることが分かっている。
また、全ての部屋からちょうど
K
本の一方通行の道が順に並んでおり、
i
番目の部屋から出る道はそれぞれ
v_{i,1}
,
v_{i,2}
, ...,
v_{i,K}
番目の部屋に繋がっている。
あなたは、A君に対し今いる部屋から
a_1
,
a_2
, ...,
a_l
番目の道を順に進ませることができる。
ただし、A君は明るい部屋に到達したらそれ以降の指示は無視する。
あなたは、指示の前後においてA君が今いる部屋の情報を知ることはできないため、A君がどの部屋にいたとしても明るい部屋に辿り着けるような指示列を伝えなければならない。

そのような指示のうち、最も短いものの長さを答えよ。
Constraints
2  ≤  N  ≤  100
1  ≤  M  ≤  min(16, N − 1)
1  ≤  K  ≤  N
1  ≤  D_i  ≤  N
D_i
は全て異なる
1  ≤  v_{i, j}  ≤  N
全ての暗い部屋は少なくとも1つの明るい部屋に到達可能である
Input Format
入力は以下の形式で標準入力から与えられる。
N
M
K
D_1
D_2
...
D_M
v_{1,1}
v_{1,2}
...
v_{1,K}
v_{2,1}
v_{2,2}
...
v_{2,K}
...
v_{N,1}
v_{N,2}
...
v_{N,K}
Output Format
答えを一行に出力せよ。
Sample Input 1
4 2 2
1 2
2 4
3 1
4 2
1 3
Sample Output 1
2
1, 1 という指示を出すと
A君の初期位置が部屋1である場合、2つ目の移動で部屋3に到達する
A君の初期位置が部屋2である場合、1つ目の移動で部屋3に到達する
Sample Input 2
3 2 2
1 2
2 3
1 2
2 1
Sample Output 2
3
2, 1, 2 という指示を出すと
A君の初期位置が部屋1である場合、1つ目の移動で部屋3に到達する
A君の初期位置が部屋2である場合、3つ目の移動で部屋3に到達する
Sample Input 3
6 3 3
1 2 3
4 1 1
2 5 2
3 3 6
4 4 4
5 5 5
6 6 6
Sample Output 3
3
非連結であるケースや、自己辺、多重辺があるケースに気をつけよう

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n,m,K;cin >> n >> m >> K;
  vi D(m);cin >> D;
  vi inv(n,-1);
  rep(i,0,m)inv[--D[i]] = i;
  vvi v(n,vi(K));cin >> v;
  rep(i,0,n)rep(j,0,K)v[i][j]--;
  vvi V(1 << m,vi(K));
  rep(i,0,1 << m)rep(k,0,K){
    int msk = 0;
    rep(j,0,m)if(i >> j & 1){
      if(inv[v[D[j]][k]] == -1)continue;
      msk |= (1 << inv[v[D[j]][k]]);
    }
    V[i][k] = msk;
  }
  vi d(1 << m,-1);
  d[(1 << m)-1] = 0;
  queue<int> que;
  que.push((1 << m)-1);
  while(sz(que)){
    auto ov = que.front();que.pop();
    for(auto nv : V[ov]){
      if(d[nv] != -1)continue;
      d[nv] = d[ov] + 1;
      que.push(nv);
    }
  }
  cout << d[0] << endl;
  

  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 57748
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int N, M, K;
	cin >> N >> M >> K;
	vector<vector<int>> G(N, vector<int>(K));
	vector<int> is(N);
	vector<int> D(M);
	for (int i = 0; i < M; i++) {
		cin >> D[i]; D[i]--;
		is[D[i]] = 1;
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < K; j++) {
			cin >> G[i][j]; G[i][j]--;
		}
	}
	vector<int> res(1 << M, 10000);
	int s = (1 << M) - 1; res[s] = 0;
	queue<int> q; q.push(s);
	while (!q.empty()) {
		int p = q.front(); q.pop();
		for (int i = 0; i < K; i++) {
			int t = 0;
			for (int j = 0; j < M; j++) {
				if (p & (1 << j)) {
					if (is[G[D[j]][i]]) {
						int id = 0;
						for (int k = 0; k < M; k++) {
							if (D[k] == G[D[j]][i]) {
								id = k;
								break;
							}
						}
						t |= 1 << id;
					}
				}
			}
			if (res[t] == 10000) {
				res[t] = res[p] + 1;
				q.push(t);
			}
		}
	}
	cout << res[0] << endl;
	return 0;
}
accuracy: 1
time_ms: 500
memory_kb: 3644
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    vector<int> A(N,-1);
    vector<int> D(M);
    rep(i,0,M) {
        cin >> D[i];
        D[i]--;
        A[D[i]] = i;
    }
    vector<vector<int>> G(N, vector<int>(K));
    rep(i,0,N) rep(j,0,K) cin >> G[i][j], G[i][j]--;
    vector<int> Dist(1<<M,inf);
    queue<int> Q;
    Dist[(1<<M)-1] = 0;
    Q.push((1<<M)-1);
    while(!Q.empty()) {
        int P = Q.front();
        Q.pop();
        rep(i,0,K) {
            int NP = 0;
            rep(j,0,M) {
                if (P & (1<<j)) {
                    if (A[G[D[j]][i]] >= 0) NP |= (1<<A[G[D[j]][i]]);
                }
            }
            if (chmin(Dist[NP],Dist[P]+1)) {
                Q.push(NP);
            }
        }
    }
    cout << Dist[0] << endl;
}
accuracy: 1
time_ms: 90
memory_kb: 3632
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
int main(void) {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> d(m);
    vector<int> id(n, -1);
    rep(i, 0, m) {
        cin >> d[i];
        d[i]--;
        id[d[i]] = i;
    }
    vector<vector<int>> room(n, vector<int>(k));
    rep(i, 0, n) {
        rep(j, 0, k) {
            cin >> room[i][j];
            room[i][j]--;
        }
    }
    vector<vector<int>> g(1 << m);
    rep(mask, 0, 1 << m) {
        rep(i, 0, k) {
            int mask_to = 0;
            rep(j, 0, m) {
                if(mask & (1 << j)) {
                    int to = room[d[j]][i];
                    if(id[to] == -1) continue;
                    mask_to |= (1 << id[to]);
                }
            }
            g[mask].push_back(mask_to);
        }
    }
    vector<int> dist(1 << m, 1e9);
    queue<int> que;
    dist[(1 << m) - 1] = 0;
    que.push((1 << m) - 1);
    while(!que.empty()) {
        int cur = que.front();
        que.pop();
        rep(i, 0, (int)g[cur].size()) {
            int to = g[cur][i];
            if(dist[to] == 1e9) {
                dist[to] = dist[cur] + 1;
                que.push(to);
            }
        }
    }
    cout << dist[0] << '\n';
}
accuracy: 1
time_ms: 150
memory_kb: 38984
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;


int main() {

    int N,M,K;
    cin>>N>>M>>K;
    vector<int> D(M);
    unordered_map<int,int> MP;
    int cnt=0;
    for(auto &d:D){
        cin>>d;
        d--;
        MP[d]=cnt;
        cnt++;
    }
    vector<vector<int>> P(N,vector<int> (K));
    for(auto &V:P)for(auto &v:V){
        cin>>v;
        v--;
    }
    vector<int> DP(1<<M,1e9);
    vector<bool> seen(1<<M,0);
    DP[(1<<M)-1]=0;
    queue<int> Q;
    Q.push((1<<M)-1);
    while(!Q.empty()){
        int p=Q.front();
        Q.pop();
        if(seen[p])continue;
        seen[p]=1;
        for(int k=0;k<K;k++){
            int np=0;
            for(int m=0;m<M;m++){
                if(p&(1<<m)){
                    if(MP.count(P[D[m]][k])){
                        int nm=MP[P[D[m]][k]];
                        np|=(1<<nm);
                    }
                }
            }
            if(seen[np])continue;
            if(DP[np]<=DP[p]+1)continue;
            DP[np]=DP[p]+1;
            Q.push(np);
        }
    }
    cout<<DP[0]<<endl;
}
accuracy: 1
time_ms: 1260
memory_kb: 3628
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;


int main() {

    int N,M,K;
    cin>>N>>M>>K;
    vector<int> D(M);
    map<int,int> MP;
    int cnt=0;
    for(auto &d:D){
        cin>>d;
        d--;
        MP[d]=cnt;
        cnt++;
    }
    vector<vector<int>> P(N,vector<int> (K));
    for(auto &V:P)for(auto &v:V){
        cin>>v;
        v--;
    }
    vector<int> DP(1<<M,1e9);
    vector<bool> seen(1<<M,0);
    DP[(1<<M)-1]=0;
    queue<int> Q;
    Q.push((1<<M)-1);
    while(!Q.empty()){
        int p=Q.front();
        Q.pop();
        if(seen[p])continue;
        seen[p]=1;
        for(int k=0;k<K;k++){
            int np=0;
            for(int m=0;m<M;m++){
                if(p&(1<<m)){
                    if(MP.count(P[D[m]][k])){
                        int nm=MP[P[D[m]][k]];
                        np|=(1<<nm);
                    }
                }
            }
            if(seen[np])continue;
            if(DP[np]<=DP[p]+1)continue;
            DP[np]=DP[p]+1;
            Q.push(np);
        }
    }
    cout<<DP[0]<<endl;
}
accuracy: 1
time_ms: 860
memory_kb: 3660
final_rank: 11

code_snippet: //#define _GLIBCXX_DEBUG /* これをするとvectorの配列外参照などがチェックされる */
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const bool DEBUG = true;
#define debug(x) \
    if (DEBUG) cerr << __FILE__ << ':' << __LINE__ << ") " << #x << " : " << (x) << endl;

// dp(S \in {0, 1}^M)
// := S_i が true なら，暗い部屋iに人がいる
//    S_i が false なら，暗い部屋iに人がいない
//    この状態から，すべての人を明るい部屋へ動かす最小手数
// k = 1, 2, ..., K のそれぞれについて，
// - 全部 false からはじめて，
// - S_i が true で v_{i, k} が暗いとき，
// - S_{v_{i, k}（厳密には添字違う）} <- true
// 上でできた状態 S' として， chmin(S, dp(S') + 1)

ll n, m, k;
vector<ll> id;

vector<ll> is_dark;

vector<vector<ll> > v;

int main() {
    cin >> n >> m >> k;
    id.resize(m);
    is_dark.resize(n + 1, 0);

    for (int i = 0; i < m; i++) cin >> id[i];
    for (int i = 0; i < m; i++) is_dark[id[i]] = i + 1;
    v.resize(n);
    for (vector<ll> &x : v) {
        x.resize(k);
        for (int i = 0; i < k; i++) {
            cin >> x[i];
        }
    }

    vector<vector<ll> > G((1ll << m));

    for (int S = 0; S < (1ll << m); S++) {
        for (int e_id = 0; e_id < k; e_id++) {
            int nx_S = 0;
            for (int b = 0; b < m; b++) {
                if (S & (1 << b)) {
                    if (is_dark[v[id[b] - 1][e_id]]) {
                        nx_S |= (1 << (is_dark[v[id[b] - 1][e_id]] - 1));
                    }
                }
            }
            if (S != nx_S) G[nx_S].push_back(S);
        }
    }

    ll initil = 1e18;
    vector<ll> dist(1 << m, initil);
    dist[0] = 0;
    queue<ll> q;
    q.push(0);

    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (ll nx : G[now]) {
            if (dist[nx] == initil) {
                dist[nx] = dist[now] + 1;
                q.push(nx);
            }
        }
    }

    cout << dist[(1 << m) - 1] << endl;

    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 81872
final_rank: 15

code_snippet: #define _GLIBCXX_DEBUG /* これをするとvectorの配列外参照などがチェックされる */
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const bool DEBUG = true;
#define debug(x) \
    if (DEBUG) cerr << __FILE__ << ':' << __LINE__ << ") " << #x << " : " << (x) << endl;

// dp(S \in {0, 1}^M)
// := S_i が true なら，暗い部屋iに人がいる
//    S_i が false なら，暗い部屋iに人がいない
//    この状態から，すべての人を明るい部屋へ動かす最小手数
// k = 1, 2, ..., K のそれぞれについて，
// - 全部 false からはじめて，
// - S_i が true で v_{i, k} が暗いとき，
// - S_{v_{i, k}（厳密には添字違う）} <- true
// 上でできた状態 S' として， chmin(S, dp(S') + 1)

ll n, m, k;
vector<ll> id;

vector<ll> is_dark;

vector<vector<ll> > v;

int main() {
    cin >> n >> m >> k;
    id.resize(m);
    is_dark.resize(n + 1, 0);

    for (int i = 0; i < m; i++) cin >> id[i];
    for (int i = 0; i < m; i++) is_dark[id[i]] = i + 1;
    v.resize(n);
    for (vector<ll> &x : v) {
        x.resize(k);
        for (int i = 0; i < k; i++) {
            cin >> x[i];
        }
    }

    vector<vector<ll> > G((1ll << m));

    for (int S = 0; S < (1ll << m); S++) {
        for (int e_id = 0; e_id < k; e_id++) {
            int nx_S = 0;
            for (int b = 0; b < m; b++) {
                if (S & (1 << b)) {
                    if (is_dark[v[id[b] - 1][e_id]]) {
                        nx_S |= (1 << (is_dark[v[id[b] - 1][e_id]] - 1));
                    }
                }
            }
            if (S != nx_S) G[nx_S].push_back(S);
        }
    }

    ll initil = 1e18;
    vector<ll> dist(1 << m, initil);
    dist[0] = 0;
    queue<ll> q;
    q.push(0);

    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (ll nx : G[now]) {
            if (dist[nx] == initil) {
                dist[nx] = dist[now] + 1;
                q.push(nx);
            }
        }
    }

    cout << dist[(1 << m) - 1] << endl;

    return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 84080
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, M, K; cin >> N >> M >> K;
    vector<int> D(M);
    for (auto& d : D) cin >> d;
    for (auto& d : D) d--;
    vector<vector<int>> G(N, vector<int>(K));
    for (int i = 0 ; i < N ; i++) {
        for (int j = 0 ; j < K ; j++) {
            cin >> G[i][j];
            G[i][j]--;
        }
    }

    map<int, int> mp;
    for (int i = 0 ; i < M ; i++) mp[D[i]] = i;

    const int inf = (int)1e9;
    vector dp((1 << M), inf);
    queue<int> que;
    que.push((1 << M) - 1);
    dp[(1 << M) - 1] = 0;
    while (que.size()) {
        int bit = que.front();
        que.pop();
        for (int op = 0 ; op < K ; op++) {
            int nxt = 0;
            for (int j = 0 ; j < M ; j++) if ((bit & (1 << j))) {
                int x = G[D[j]][op];
                if (not mp.count(x)) continue;
                nxt |= (1 << mp[x]);
            }
            if (dp[nxt] > dp[bit] + 1) {
                dp[nxt] = dp[bit] + 1;
                que.push(nxt);
            }
        }
    }

    // for (auto d : dp) cout << d << ' ';
    // cout << endl;

    cout << dp[0] << endl;
}
accuracy: 1
time_ms: 880
memory_kb: 3652
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, M, K; cin >> N >> M >> K;
    vector<int> D(M);
    for (auto& d : D) cin >> d;
    for (auto& d : D) d--;
    vector<vector<int>> G(N, vector<int>(K));
    for (int i = 0 ; i < N ; i++) {
        for (int j = 0 ; j < K ; j++) {
            cin >> G[i][j];
            G[i][j]--;
        }
    }

    map<int, int> mp;
    for (int i = 0 ; i < M ; i++) mp[D[i]] = i;

    const int inf = (int)1e9;
    vector dp((1 << M), inf);
    dp[(1 << M) - 1] = 0;
    for (int bit = (1 << M) - 1 ; bit >= 0 ; bit--) {
        if (dp[bit] == inf) continue;
        for (int op = 0 ; op < K ; op++) {
            int nxt = 0;
            for (int j = 0 ; j < M ; j++) if ((bit & (1 << j))) {
                int x = G[D[j]][op];
                if (not mp.count(x)) continue;
                nxt |= (1 << mp[x]);
            }
            // cout << bit << ' ' << op << ' ' << nxt << endl;
            dp[nxt] = min(dp[nxt], dp[bit] + 1);
        }
    }

    // for (auto d : dp) cout << d << ' ';
    // cout << endl;

    cout << dp[0] << endl;
}
accuracy: 0.08
time_ms: 390
memory_kb: 3444
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, M, K; cin >> N >> M >> K;
    vector<int> D(M);
    for (auto& d : D) cin >> d;
    for (auto& d : D) d--;
    vector<vector<int>> G(N, vector<int>(K));
    for (int i = 0 ; i < N ; i++) {
        for (int j = 0 ; j < K ; j++) {
            cin >> G[i][j];
            G[i][j]--;
        }
    }

    map<int, int> mp;
    for (int i = 0 ; i < M ; i++) mp[D[i]] = i;

    const int inf = (int)1e9;
    vector dp((1 << M), inf);
    dp[(1 << M) - 1] = 0;
    for (int bit = (1 << M) - 1 ; bit >= 0 ; bit--) {
        for (int op = 0 ; op < K ; op++) {
            int nxt = 0;
            for (int j = 0 ; j < M ; j++) if ((bit & (1 << j))) {
                int x = G[D[j]][op];
                if (not mp.count(x)) continue;
                nxt |= (1 << mp[x]);
            }
            // cout << bit << ' ' << op << ' ' << nxt << endl;
            dp[nxt] = min(dp[nxt], dp[bit] + 1);
        }
    }

    // for (auto d : dp) cout << d << ' ';
    // cout << endl;

    cout << dp[0] << endl;
}
accuracy: 0.08
time_ms: 380
memory_kb: 3444
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n, m, k;
vector<int> d;
vector<int> dnum;
vector<vector<int>> v;

vector<int> dp;

int f(int mask){
    if(dp[mask] != -1) return dp[mask];

    dp[mask] = 1 << 30;
    for(int nex = 0; nex < k; nex++){
        int nmask = 0;
        for(int i = 0; i < m; i++){
            if((mask >> i) & 1){
                if(dnum[v[d[i]][nex]] != -1) nmask |= 1 << dnum[v[d[i]][nex]];
            }
        }
        dp[mask] = min(dp[mask], f(nmask)+1);
    }
    return dp[mask];
}

int main(){
    cin >> n >> m >> k;
    d = vector<int>(m);
    for(auto &it: d){
        cin >> it;
        it--;
    }
    v = vector<vector<int>>(n, vector<int>(k));
    for(auto &vec: v){
        for(auto &it: vec){
            cin >> it;
            it--;
        }
    }

    dnum = vector<int>(n, -1);
    for(int i = 0; i < m; i++){
        dnum[d[i]] = i;
    }
    dp = vector<int>(1 << m, -1);
    dp[0] = 0;

    cout << f((1 << m)-1) << endl;
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 4404
final_rank: 6

code_snippet: // clang-format off
#include <bits/stdc++.h>
//#pragma GCC optimize ("-Ofast")
//#pragma GCC optimize ("unroll-loops")
#define int long long
#define endl '\n'
//#define double __float80
using namespace std;
#define fi first
#define se second
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define rfor(i, a) for (auto &i: a)
#define all(obj) begin(obj), end(obj)
#define _max(x) *max_element(all(x))
#define _min(x) *min_element(all(x))
#define _sum(x) accumulate(all(x), 0LL)

const int MOD   = 1000000007;
// const int MOD    = 998244353;
const int INF    = 1e18;
// const int INF    = 1e13 + 7;
// const int INF    = LLONG_MAX; // 9.2e18
const double EPS = 1e-20;
const double PI  = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T, class S>  using P = pair<T, S>;
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return true;}return false;}
template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return true;}return false;}
int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
template<class T> T chmod(T &a, T mod=MOD) {a =  a >= 0 ? a % mod : a - (mod * _ceil(a, mod)); return a;};
int _mod(int a, int mod=MOD) {return a >= 0 ? a % mod : a - (mod * _ceil(a, mod));}
double _mod(double a, int mod = MOD) { return fmod(a, mod) >= 0 ? fmod(a, mod) : fmod(a, mod) + mod; }
int _pow(int a, int b, int mod=MOD) {int res = 1;for (a %= mod; b; a = a * a % mod, b >>= 1)if (b & 1) res = res * a % mod;return res;}
vector<int> iota(int n){vector<int> ret(n); iota(begin(ret), end(ret), 0); return ret;}
struct mint {long long x;mint(long long x = 0): x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) {(x *= a.x) %= MOD;return *this;}mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; }mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long n) const {mint ret(1), mul(x);while (n > 0) {if (n & 1) ret *= mul;mul *= mul;n >>= 1;}return ret;}mint inv() const {int a = x, b = MOD, u = 1, v = 0, t;while (b > 0) {t = a / b;swap(a -= t * b, b);swap(u -= t * v, v);}return mint(u);}mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }bool operator==(const mint a) const { return x == a.x; }bool operator!=(const mint a) const { return x != a.x; }friend istream &operator>>(istream &is, mint &a) { return is >> a.x; }friend ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }};
// clang-format on

signed main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int N, M, K;
    cin >> N >> M >> K;
    V<int> D(M);
    rep(i, M) cin >> D[i];
    rep(i, M) D[i]--;
    map<int, int> D_check;
    rep(i, M) D_check[D[i]] = i;
    VV<int> v(N, V<int>(K));
    rep(i, N) rep(j, K) cin >> v[i][j];
    rep(i, N) rep(j, K) v[i][j]--;
    V<int> dp((1 << M), INF);
    dp[(1 << M) - 1] = 0;
    deque<int> to_do = {(1 << M) - 1};
    while (to_do.size()) {
        int i = to_do[0];
        to_do.pop_front();
        rep(j, K) {
            int next = 0;
            rep(k, M) {
                if ((i >> k) & 1 and D_check.count(v[D[k]][j])) {
                    int to = D_check[v[D[k]][j]];
                    next |= (1 << to);
                }
            }
            if (dp[next] > dp[i] + 1) {
                dp[next] = dp[i] + 1;
                to_do.emplace_back(next);
            }
        }
    }
    cout << dp[0] << endl;
}
accuracy: 1
time_ms: 840
memory_kb: 4104
final_rank: 10

code_snippet: // clang-format off
#include <bits/stdc++.h>
//#pragma GCC optimize ("-Ofast")
//#pragma GCC optimize ("unroll-loops")
#define int long long
#define endl '\n'
//#define double __float80
using namespace std;
#define fi first
#define se second
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define rfor(i, a) for (auto &i: a)
#define all(obj) begin(obj), end(obj)
#define _max(x) *max_element(all(x))
#define _min(x) *min_element(all(x))
#define _sum(x) accumulate(all(x), 0LL)

const int MOD   = 1000000007;
// const int MOD    = 998244353;
const int INF    = 1e18;
// const int INF    = 1e13 + 7;
// const int INF    = LLONG_MAX; // 9.2e18
const double EPS = 1e-20;
const double PI  = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T, class S>  using P = pair<T, S>;
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return true;}return false;}
template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return true;}return false;}
int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
template<class T> T chmod(T &a, T mod=MOD) {a =  a >= 0 ? a % mod : a - (mod * _ceil(a, mod)); return a;};
int _mod(int a, int mod=MOD) {return a >= 0 ? a % mod : a - (mod * _ceil(a, mod));}
double _mod(double a, int mod = MOD) { return fmod(a, mod) >= 0 ? fmod(a, mod) : fmod(a, mod) + mod; }
int _pow(int a, int b, int mod=MOD) {int res = 1;for (a %= mod; b; a = a * a % mod, b >>= 1)if (b & 1) res = res * a % mod;return res;}
vector<int> iota(int n){vector<int> ret(n); iota(begin(ret), end(ret), 0); return ret;}
struct mint {long long x;mint(long long x = 0): x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) {(x *= a.x) %= MOD;return *this;}mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; }mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long n) const {mint ret(1), mul(x);while (n > 0) {if (n & 1) ret *= mul;mul *= mul;n >>= 1;}return ret;}mint inv() const {int a = x, b = MOD, u = 1, v = 0, t;while (b > 0) {t = a / b;swap(a -= t * b, b);swap(u -= t * v, v);}return mint(u);}mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }bool operator==(const mint a) const { return x == a.x; }bool operator!=(const mint a) const { return x != a.x; }friend istream &operator>>(istream &is, mint &a) { return is >> a.x; }friend ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }};
// clang-format on

signed main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int N, M, K;
    cin >> N >> M >> K;
    V<int> D(M);
    rep(i, M) cin >> D[i];
    rep(i, M) D[i]--;
    map<int, int> D_check;
    rep(i, M) D_check[D[i]] = i;
    VV<int> v(N, V<int>(K));
    rep(i, N) rep(j, K) cin >> v[i][j];
    rep(i, N) rep(j, K) v[i][j]--;
    V<int> dp((1 << M), INF);
    dp[(1 << M) - 1] = 0;
    rep3(i, (1 << M) - 1, 1) {
        rep(j, K) {
            int next = 0;
            rep(k, M) {
                if ((i >> k) & 1 and D_check.count(v[D[k]][j])) {
                    int to = D_check[v[D[k]][j]];
                    next |= (1 << to);
                }
            }
            chmin(dp[next], dp[i] + 1);
        }
    }
    cout << dp[0] << endl;
}
accuracy: 0.08
time_ms: 410
memory_kb: 3552
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
    int N, M, K, bi = 0;
    cin >> N >> M >> K;
    vector<int> D(M);
    vector<int> L(N, -1);
    for(auto &&v:D){
        cin >> v;
        L[--v] = bi++;
    }
    vector<vector<int>> A(N, vector<int>(K));
    for(int i = 0; i < N; i++){
        for(int j = 0; j < K; j++){
            cin >> A[i][j];
            A[i][j]--;
        }
    }
    queue<int> nxt;
    vector<int> dp(1 << M, 1 << 29);
    dp.back() = 0;
    nxt.push(dp.size() - 1);
    auto f = [&](int S, int k){
        int T = 0;
        for(int i = 0; i < M; i++){
            if(~S >> i & 1)continue;
            int from = D[i];
            int to = A[from][k];
            if(L[to] != -1)T |= 1 << L[to];
        }
        return T;
    };
    while(!nxt.empty()){
        int S = nxt.front();
        nxt.pop();
        if(S == 0){
            cout << dp[0] << '\n';
            return 0;
        }
        for(int i = 0; i < K; i++){
            int T = f(S, i);
            if(dp[S] + 1 >= dp[T])continue;
            dp[T] = dp[S] + 1;
            nxt.push(T);
        }
    }
}
accuracy: 1
time_ms: 130
memory_kb: 3668
final_rank: 5

code_snippet: #line 2 "library/KowerKoint/base.hpp"

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)
#define ALL(a) (a).begin(),(a).end()
#define END(...) { print(__VA_ARGS__); return; }

using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using ll = long long;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VS = vector<string>;
using VVS = vector<VS>;
using VVVS = vector<VVS>;
using VC = vector<char>;
using VVC = vector<VC>;
using VVVC = vector<VVC>;
using P = pair<int, int>;
using VP = vector<P>;
using VVP = vector<VP>;
using VVVP = vector<VVP>;
using LP = pair<ll, ll>;
using VLP = vector<LP>;
using VVLP = vector<VLP>;
using VVVLP = vector<VVLP>;

template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};

void print() { cout << '\n'; }
template<typename T>
void print(const T &t) { cout << t << '\n'; }
template<typename Head, typename... Tail>
void print(const Head &head, const Tail &... tail) {
    cout << head << ' ';
    print(tail...);
}

#ifdef DEBUG
void dbg() { cerr << '\n'; }
template<typename T>
void dbg(const T &t) { cerr << t << '\n'; }
template<typename Head, typename... Tail>
void dbg(const Head &head, const Tail &... tail) {
    cerr << head << ' ';
    dbg(tail...);
}
#else
template<typename... Args>
void dbg(const Args &... args) {}
#endif

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
    os << p.first << " " << p.second;
    return os;
}

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

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
    for(int i = 0; i < (int) v.size(); i++) {
        os << v[i] << (i + 1 != (int) v.size() ? " " : "");
    }
    return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
    for(T &in : v) is >> in;
    return is;
}

template<typename T>
vector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {
    vector<vector<T>> res;
    vector<T> cur;
    for(auto it = begin; it != end; it++) {
        if(*it == val) {
            res.push_back(cur);
            cur.clear();
        } else cur.push_back(*it);
    }
    res.push_back(cur);
    return res;
}

vector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {
    vector<string> res;
    string cur = "";
    for(auto it = begin; it != end; it++) {
        if(*it == val) {
            res.push_back(cur);
            cur.clear();
        } else cur.push_back(*it);
    }
    res.push_back(cur);
    return res;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template <typename T>
pair<VI, vector<T>> compress(const vector<T> &a) {
    int n = a.size();
    vector<T> x;
    REP(i, n) x.push_back(a[i]);
    sort(ALL(x)); x.erase(unique(ALL(x)), x.end());
    VI res(n);
    REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();
    return make_pair(res, x);
}

template <typename It>
auto rle(It begin, It end) {
    vector<pair<typename It::value_type, int>> res;
    if(begin == end) return res;
    auto pre = *begin;
    int num = 1;
    for(auto it = begin + 1; it != end; it++) {
        if(pre != *it) {
            res.emplace_back(pre, num);
            pre = *it;
            num = 1;
        } else num++;
    }
    res.emplace_back(pre, num);
    return res;
}

template <typename It>
vector<pair<typename It::value_type, int>> rle_sort(It begin, It end) {
    vector<typename It::value_type> cloned(begin, end);
    sort(ALL(cloned));
    auto e = rle(ALL(cloned));
    sort(ALL(e), [](const auto& l, const auto& r) { return l.second < r.second; });
    return e;
}

template <typename T>
pair<vector<T>, vector<T>> factorial(int n) {
    vector<T> res(n+1), rev(n+1);
    res[0] = 1;
    REP(i, n) res[i+1] = res[i] * (i+1);
    rev[n] = 1 / res[n];
    for(int i = n; i > 0; i--) {
        rev[i-1] = rev[i] * i;
    }
    return make_pair(res, rev);
}
#line 3 "library/KowerKoint/internal_operator.hpp"

namespace internal_operator {
    template <typename T>
    T default_add(T a, T b) { return a + b; }
    template <typename T>
    T default_sub(T a, T b) { return a - b; }
    template <typename T>
    T zero() { return T(0); }
    template <typename T>
    T default_div(T a, T b) { return a / b; }
    template <typename T>
    T default_mult(T a, T b) { return a * b; }
    template <typename T>
    T one() { return T(1); }
    template <typename T>
    T default_xor(T a, T b) { return a ^ b; }
    template <typename T>
    T default_and(T a, T b) { return a & b; }
    template <typename T>
    T default_or(T a, T b) { return a | b; }
    ll mod3() { return 998244353LL; }
    ll mod7() { return 1000000007LL; }
    ll mod9() { return 1000000009LL; }
    template <typename T>
    T default_max(T a, T b) { return max(a, b); }
    template <typename T>
    T default_min(T a, T b) { return min(a, b); }
}

#line 3 "library/KowerKoint/integer.hpp"

ll kth_root(ll x, ll k) {
    if(k == 1) return x;
    ll res = 0;
    for(int i = 31; i >= 0; i--) {
        bool over = false;
        ll tmp = 1;
        ll nxt = res | 1LL << i;
        REP(i, k) {
            if(tmp > x / nxt) {
                over = true;
                break;
            }
            tmp *= nxt;
        }
        if(!over) res = nxt;
    }
    return res;
}

ll sqrt(ll x) {
    return kth_root(x, 2);
}

struct Prime {
    VI sieved;
    VL primes;

    Prime() {}
    Prime(ll n) {
        expand(n);
    }

    void expand(ll n) {
        ll sz = (ll)sieved.size() - 1;
        if(n <= sz) return;
        sieved.resize(n+1);
        sieved[0] = sieved[1] = 1;
        primes.clear();
        primes.push_back(2);
        for(ll d = 4; d <= n; d += 2) sieved[d] = 1;
        FOR(d, 3, n+1) {
            if(!sieved[d]) {
                primes.push_back(d);
                for(ll i = d*d; i <= n; i += d*2) sieved[i] = 1;
            }
        }
    }

    bool is_prime(ll n) {
        assert(n > 0);
        if(n <= (ll)sieved.size() - 1) return !sieved[n];
        for(ll d = 2; d*d <= n; d++) {
            if(n % d == 0) return false;
        }
        return true;
    }

    VL least_prime_factors(ll n) {
        assert(n > 0);
        VL lpfs(n+1, -1), primes;
        FOR(d, 2, n+1) {
            if(lpfs[d] == -1) {
                lpfs[d] = d;
                primes.push_back(d);
            }
            for(ll p : primes) {
                if(p * d > n || p > lpfs[d]) break;
                lpfs[p*d] = p;
            }
        }
        return lpfs;
    }

    VL prime_list(ll n) {
        assert(n > 0);
        expand(n);
        return VL(primes.begin(), upper_bound(ALL(primes), n));
    }

    vector<pair<ll, int>> prime_factor(ll n) {
        assert(n > 0);
        vector<pair<ll, int>> factor;
        expand(sqrt(n));
        for(ll prime : primes) {
            if(prime * prime > n) break;
            int cnt = 0;
            while(n % prime == 0) {
                n /= prime;
                cnt++;
            }
            if(cnt) factor.emplace_back(prime, cnt);
        }
        if(n > 1) factor.emplace_back(n, 1);
        return factor;
    }


    VL divisor(ll n) {
        assert(n > 0);
        auto factor = prime_factor(n);
        VL res = {1};
        for(auto [prime, cnt] : factor) {
            int sz = res.size();
            res.resize(sz * (cnt+1));
            REP(i, sz*cnt) res[sz+i] = res[i] * prime;
            REP(i, cnt) inplace_merge(res.begin(), res.begin() + sz*(i+1), res.begin() + sz*(i+2));
        }
        return res;
    }
};

ll extgcd(ll a, ll b, ll& x, ll& y) {
    x = 1, y = 0;
    ll nx = 0, ny = 1;
    while(b) {
        ll q = a / b;
        tie(a, b) = LP(b, a % b);
        tie(x, nx) = LP(nx, x - nx*q);
        tie(y, ny) = LP(ny, y - ny*q);
    }
    return a;
}

ll inv_mod(ll n, ll m) {
    ll x, y;
    assert(extgcd(n, m, x, y) == 1);
    x %= m;
    if(x < 0) x += m;
    return x;
}

ll pow_mod(ll a, ll n, ll m) {
    if(n == 0) return 1LL;
    if(n < 0) return inv_mod(pow_mod(a, -n, m), m);
    ll res = 1;
    while(n) {
        if(n & 1) {
            res *= a;
            res %= m;
        }
        n >>= 1;
        a *= a;
        a %= m;
    }
    return res;
}

#line 5 "library/KowerKoint/modint.hpp"

template <ll (*mod)()>
struct Modint {
    ll val;
    
    Modint(): val(0) {}

    Modint(ll x): val(x) {
        val %= mod();
        if(val < 0) val += mod();
    }

    Modint& operator+=(const Modint& r) {
        val += r.val;
        if(val >= mod()) val -= mod();
        return *this;
    }
    friend Modint operator+(const Modint& l, const Modint& r) {
        return Modint(l) += r;
    }

    Modint& operator-=(const Modint& r) {
        val -= r.val;
        if(val < 0) val += mod();
        return *this;
    }
    friend Modint operator-(const Modint& l, const Modint& r) {
        return Modint(l) -= r;
    }

    Modint& operator*=(const Modint& r) {
        val *= r.val;
        val %= mod();
        return *this;
    }
    Modint operator*(const Modint& r) {
        return (Modint(*this) *= r);
    }
    friend Modint operator*(const Modint& l, const Modint& r) {
        return Modint(l) *= r;
    }

    Modint pow(ll n) const {
        return Modint(pow_mod(val, n, mod()));
    }

    Modint inv() const {
        return Modint(inv_mod(val, mod()));
    }

    Modint& operator/=(const Modint& r) {
        return (*this *= r.inv());
    }
    friend Modint operator/(const Modint& l, const Modint& r) {
        return Modint(l) /= r;
    }

    Modint& operator^=(const ll n) {
        val = pow_mod(val, n, mod());
        return *this;
    }
    Modint operator^(const ll n) {
        return this->pow(n);
    }

    Modint operator+() const { return *this; }
    Modint operator-() const { return Modint() - *this; }

    Modint& operator++() {
        val++;
        if(val == mod()) val = 0LL;
        return *this;
    }
    Modint& operator++(int) {
        Modint res(*this);
        ++*this;
        return res;
    }

    Modint& operator--() {
        if(val == 0LL) val = mod();
        val--;
        return *this;
    }
    Modint& operator--(int) {
        Modint res(*this);
        --*this;
        return res;
    }

    friend bool operator==(const Modint& l, const Modint& r) {
        return l.val == r.val;
    }
    friend bool operator!=(const Modint& l, const Modint& r) {
        return l.val != r.val;
    }

    static pair<vector<Modint>, vector<Modint>> factorial(int n) {
        vector<Modint> fact(n+1), rfact(n+1);
        fact[0] = 1;
        REP(i, n) fact[i+1] = fact[i] * (i+1);
        rfact[n] = 1 / fact[n];
        for(int i = n-1; i >= 0; i--) rfact[i] = rfact[i+1] * (i+1);
        return {fact, rfact};
    }

    friend istream& operator>>(istream& is, Modint& mi) {
        is >> mi.val;
        return is;
    }

    friend ostream& operator<<(ostream& os, const Modint& mi) {
        os << mi.val;
        return os;
    }
};

using MI3 = Modint<internal_operator::mod3>;
using V3 = vector<MI3>;
using VV3 = vector<V3>;
using VVV3 = vector<VV3>;
using MI7 = Modint<internal_operator::mod7>;
using V7 = vector<MI7>;
using VV7 = vector<V7>;
using VVV7 = vector<VV7>;
using MI9 = Modint<internal_operator::mod9>;
using V9 = vector<MI9>;
using VV9 = vector<V9>;
using VVV9 = vector<VV9>;
#line 3 "library/KowerKoint/counting.hpp"

template <typename T>
struct Counting {
    vector<T> fact, ifact;

    Counting() {}
    Counting(ll n) {
        expand(n);
    }

    void expand(ll n) {
        ll sz = (ll)fact.size();
        if(sz > n) return;
        fact.resize(n+1);
        ifact.resize(n+1);
        fact[0] = 1;
        FOR(i, max(1LL, sz), n+1) fact[i] = fact[i-1] * i;
        ifact[n] = 1 / fact[n];
        for(ll i = n-1; i >= sz; i--) ifact[i] = ifact[i+1] * (i+1);
    }

    T permutation(ll n, ll r) {
        assert(n >= r);
        assert(r >= 0);
        expand(n);
        return fact[n] * ifact[n-r];
    }

    T combination(ll n, ll r) {
        assert(n >= r);
        assert(r >= 0);
        expand(n);
        return fact[n] * ifact[r] * ifact[n-r];
    }

    T stirling(ll n, ll k) {
        assert(n >= k);
        assert(k >= 0);
        if(n == 0) return 1;
        T res = 0;
        int sign = k%2? -1 : 1;
        expand(k);
        REP(i, k+1) {
            res += sign * ifact[i] * ifact[k-i] * T(i).pow(n);
            sign *= -1;
        }
        return res;
    }

    vector<vector<T>> stirling_table(ll n, ll k) {
        assert(n >= 0 && k >= 0);
        vector<vector<T>> res(n+1, vector<T>(k+1));
        res[0][0] = 1;
        FOR(i, 1, n+1) FOR(j, 1, k+1) {
            res[i][j] = res[i-1][j-1] + j * res[i-1][j];
        }
        return res;
    }

    T bell(ll n, ll k) {
        assert(n >= 0 && k >= 0);
        expand(k);
        vector<T> tmp(k+1);
        int sign = 1;
        tmp[0] = 1;
        FOR(i, 1, k+1) {
            sign *= -1;
            tmp[i] = tmp[i-1] + sign * ifact[i];
        }
        T res = 0;
        REP(i, k+1) {
            res += T(i).pow(n) * ifact[i] * tmp[k-i];
        }
        return res;
    }

    vector<vector<T>> partition_table(ll n) {
        assert(n >= 0);
        vector<vector<T>> res(n+1, vector<T>(n+1));
        REP(i, n+1) res[0][i] = 1;
        FOR(i, 1, n+1) FOR(j, 1, n+1) {
            res[i][j] = res[i][j-1] + (i<j? 0 : res[i-j][j]);
        }
        return res;
    }
};
#line 2 "library/KowerKoint/segtree.hpp"

template <typename T, T (*func)(const T, const T), T (*e)()>
struct SegTree {
    int n, sz;
    vector<T> state;

    SegTree(int n_): n(n_) {
        sz = 1;
        while(sz < n) sz <<= 1;
        state = vector<T>(sz*2, e());
    }
    SegTree(vector<T> v): n(v.size()) {
        sz = 1;
        while(sz < n) sz <<= 1;
        state = vector<T>(sz*2, e());
        REP(i, v.size()) state[sz+i] = v[i];
        for(int i = sz-1; i > 0; i--) state[i] = func(state[i*2], state[i*2+1]);
    }

    T operator[](int i) const {
        assert(0 <= i && i < n);
        return state[sz+i];
    }
    T get(int i) const {
        assert(0 <= i && i < n);
        return state[sz+i];
    }

    void set(int i, const T &x) {
        assert(0 <= i && i < n);
        i += sz;
        state[i] = x;
        while(i >>= 1) {
            state[i] = func(state[i*2], state[i*2+1]);
        }
    }
    void apply(int i, const T &x) {
        set(i, x);
    }

    T prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= n);
        T left_prod = e(), right_prod = e();
        l += sz, r += sz;
        while(l < r) {
            if(l & 1) left_prod = func(left_prod, state[l++]);
            if(r & 1) right_prod = func(state[--r], right_prod);
            l >>= 1;
            r >>= 1;
        }
        return func(left_prod, right_prod);
    }
    
    T all_prod() const {
        return state[1];
    }

    template <typename F>
    int max_right(int l, F f) {
        assert(0 <= l && l <= n);
        assert(f(e()));
        if(l == n) return n;
        l += sz;
        while(l % 2 == 0) l >>= 1;
        T sum = e();
        while(f(func(sum, state[l]))) {
            if(__builtin_clz(l) != __builtin_clz(l+1)) return n;
            sum = func(sum, state[l]);
            l++;
            while(l % 2 == 0) l >>= 1;
        }
        while(l < sz) {
            if(!f(func(sum, state[l*2]))) l *= 2;
            else {
                sum = func(sum, state[l*2]);
                l = l*2 + 1;
            }
        }
        return min(n, l - sz);
    }
};

template <typename T>
using RMaxQ = SegTree<T, internal_operator::default_max<T>, numeric_limits<T>::min>;
template <typename T>
using RMinQ = SegTree<T, internal_operator::default_min<T>, numeric_limits<T>::max>;
template <typename T>
using RSumQ = SegTree<T, internal_operator::default_add<T>, internal_operator::zero<T>>;
#line 3 "Contests/Dummy/main.cpp"

/* #include <atcoder/all> */
/* using namespace atcoder; */
/* #include "KowerKoint/expansion/ac-library/all.hpp" */

void solve(){
    int n, m, k; cin >> n >> m >> k;
    VI d(m); cin >> d;
    REP(i, m) d[i]--;
    sort(ALL(d));
    VVI v(n, VI(k)); cin >> v;
    REP(i, n) REP(j, k) v[i][j]--;
    VI dist(1 << m, INF);
    dist[(1<<m)-1] = 0;
    queue<int> que;
    que.push((1<<m) - 1);
    while(!que.empty()) {
        int from = que.front(); que.pop();
        REP(i, k) {
            int nxt = 0;
            REP(j, m) {
                if(from >> j & 1) {
                    int to = v[d[j]][i];
                    auto it = lower_bound(ALL(d), to);
                    if(it != d.end() && *it == to) {
                        nxt |= 1 << (it - d.begin());
                    }
                }
            }
            if(chmin(dist[nxt], dist[from] + 1)) que.push(nxt);
        }
    }
    print(dist[0]);
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    // Fasterize input/output script
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(100);
    // scanf/printf user should delete this fasterize input/output script

    int t = 1;
    //cin >> t; // comment out if solving multi testcase
    for(int testCase = 1;testCase <= t;++testCase){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 710
memory_kb: 3680
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const int INF = 1e9;

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

    int N, M, K;
    cin >> N >> M >> K;
    vector<int> D(M);
    rep(i, M) cin >> D[i], D[i]--;
    vector V(N, vector<int>(K));
    rep(i, N) rep(j, K) cin >> V[i][j], V[i][j]--;

    // 暗い部屋なら何番目
    // 明るい部屋なら-1
    vector<int> dark_id(N, -1);
    rep(i, M) dark_id[D[i]] = i;

    // i番目から 道jを選んだときに行く場所
    vector G(M, vector<int>(K));
    rep(j, K) {
        rep(i, M) G[i][j] = dark_id[V[D[i]][j]];
    }

    queue<int> que;
    que.push((1 << M) - 1);
    vector<int> d(1 << M, INF);
    d[(1 << M) - 1] = 0;
    while (!que.empty()) {
        int state = que.front();
        que.pop();

        rep(k, K) {
            int next = 0;
            rep(i, M) {
                if (state & (1 << i)) {
                    // 明るい部屋に行った
                    if (G[i][k] == -1) continue;
                    next |= (1 << G[i][k]);
                }
            }

            if (d[next] == INF) {
                que.push(next);
                d[next] = d[state] + 1;
            }
        }
    }
    cout << d[0] << "\n";

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3628
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;

  vector< int > ds(m);
  vector< int > rev_ds(n, -1);
  for (auto &d: ds) {
    cin >> d;
    d--;
  }
  for (int i = 0; i < m; i++) {
    rev_ds[ds[i]] = 1 << i;
  }

  vector< vector< int > > vss(n, vector<int>(k));
  for (auto &vs: vss) {
    for (auto &v: vs) {
      cin >> v;
      v--;
    }
  }

  constexpr int inf = 1001001001;
  vector< int > dists(1 << m, inf);
  queue< int > que;

  dists[(1 << m) - 1] = 0;
  que.emplace((1 << m) - 1);

  while (not que.empty()) {
    int bit = que.front();
    que.pop();

    for (int i = 0; i < k; i++) {
      int nbit = 0;
      for (int v = 0; v < m; v++) {
        if (not ((1 << v) & bit)) continue;
        int u = vss[ds[v]][i];
        if (rev_ds[u] == -1) continue;
        nbit |= rev_ds[u];
      }

      if (dists[nbit] > dists[bit] + 1) {
        dists[nbit] = dists[bit] + 1;
        que.emplace(nbit);
      }
    }
  }

  cout << dists[0] << endl;
}

int main() {
  solve();
}
accuracy: 1
time_ms: 70
memory_kb: 3632
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;

  vector< int > ds(m);
  vector< int > rev_ds(n, -1);
  for (auto &d: ds) {
    cin >> d;
    d--;
  }
  for (int i = 0; i < m; i++) {
    rev_ds[ds[i]] = 1 << i;
  }

  vector< vector< int > > vss(n, vector<int>(k));
  for (auto &vs: vss) {
    for (auto &v: vs) {
      cin >> v;
      v--;
    }
  }

  constexpr int inf = 1001001001;
  vector< int > dists(1 << m, inf);
  queue< int > que;

  dists[(1 << m) - 1] = 0;
  que.emplace((1 << m) - 1);

  while (not que.empty()) {
    int bit = que.front();
    que.pop();

    for (int i = 0; i < k; i++) {
      int nbit = 0;
      for (int v = 0; v < m; v++) {
        if (not ((1 << v) & bit)) continue;
        int u = vss[v][i];
        if (rev_ds[u] == -1) continue;
        nbit |= rev_ds[u];
      }

      if (dists[nbit] > dists[bit] + 1) {
        dists[nbit] = dists[bit] + 1;
        que.emplace(nbit);
      }
    }
  }

  cout << dists[0] << endl;
}

int main() {
  solve();
}
accuracy: 0.06
time_ms: 40
memory_kb: 3432
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const int INF = 1e9;

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

    int N, M, K;
    cin >> N >> M >> K;
    vector<int> D(M);
    rep(i, M) cin >> D[i], D[i]--;
    vector G(N, vector<int>(K));
    rep(i, N) rep(j, K) {
        int v;
        cin >> v;
        v--;
        G[i][j] = v;
    }
    vector<int> rd(N, -1);
    rep(i, M) rd[D[i]] = i;

    vector<int> dist(1 << M, -1);
    dist[(1 << M) - 1] = 0;
    queue<int> q;
    q.push((1 << M) - 1);
    while (!q.empty()) {
        int bit = q.front();
        q.pop();

        rep(i, K) {
            int next = 0;

            rep(j, M) {
                if (~bit & (1 << j)) continue;

                int v = G[D[j]][i];
                if (rd[v] == -1) continue;

                next |= 1 << rd[v];
            }

            if (dist[next] == -1) {
                dist[next] = dist[bit] + 1;
                q.push(next);
            }
        }
    }
    cout << dist[0] << "\n";

    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3716
final_rank: 4