alexjercan/bugnet huggingface dataset:
Problem p01812 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
using namespace std;
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int D[M], V[N][K];
  for (int i = 0; i < M; ++i) {
    cin >> D[i];
    --D[i];
  }
  map<int, int> R;
  for (int i = 0; i < M; ++i) {
    R[D[i]] = i + 1;
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < K; ++j) {
      cin >> V[i][j];
      --V[i][j];
    }
  }
  queue<int> que;
  // queue< pair<int,int> > que;
  map<int, int> L;
  int d = (1 << M) - 1;
  que.push(d);
  // que.push(make_pair(d,1));
  L[d] = 1;
  while (!que.empty()) {
    int s = que.front(), len = L[s];
    // int s = que.front().first, len = que.front().second;
    que.pop();
    for (int i = 0; i < K; ++i) {
      int t = 0;
      for (int j = 0; j < M; ++j) {
        if (((s >> j) & 1) && R[V[D[j]][i]]) {
          t |= (1 << (R[V[D[j]][i]] - 1));
        }
      }
      if (!t) {
        cout << len << endl;
        return 0;
      }
      if (len + 1 < L[t] || !L[t]) {
        que.push(t);
        // que.push(make_pair(t,len+1));
        L[t] = len + 1;
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
using namespace std;
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int D[M], V[N][K];
  for (int i = 0; i < M; ++i) {
    cin >> D[i];
    --D[i];
  }
  map<int, int> R;
  for (int i = 0; i < M; ++i) {
    R[D[i]] = i + 1;
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < K; ++j) {
      cin >> V[i][j];
      --V[i][j];
    }
  }
  // queue<int> que;
  queue<pair<int, int>> que;
  // map<int,int> L;
  int d = (1 << M) - 1;
  // que.push(d);
  que.push(make_pair(d, 1));
  // L[d] = 1;
  while (!que.empty()) {
    // int s = que.front(), len = L[s];
    int s = que.front().first, len = que.front().second;
    que.pop();
    for (int i = 0; i < K; ++i) {
      int t = 0;
      for (int j = 0; j < M; ++j) {
        if (((s >> j) & 1) && R[V[D[j]][i]]) {
          t |= (1 << (R[V[D[j]][i]] - 1));
        }
      }
      if (!t) {
        cout << len << endl;
        return 0;
      }
      // if(len+1 < L[t] || !L[t]){
      // que.push(t);
      que.push(make_pair(t, len + 1));
      // L[t] = len+1;
      // }
    }
  }
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> dark_map;
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    dark_map[d[i]] = i;
  }
  vector<vector<int>> v(n, vector<int>(k));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  vector<vector<int>> v2(m);
  for (int i = 0; i < m; i++) {
    v2[i] = v[d[i]];
  }
  const int INF = 1e9;
  vector<int> dp(1 << m, INF);
  int first_p = (1 << m) - 1;
  dp[first_p] = 0;
  pair<int, int> state = make_pair(0, first_p);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(state);

  while (!que.empty()) {
    state = que.top();
    que.pop();
    int p = state.second;
    int sc = state.first;
    if (dp[p] < sc)
      continue;

    for (int i = 0; i < k; i++) {
      int top = 0;
      for (int j = 0; j < m; j++) {
        if (p & (1 << j)) {
          if (dark_map.count(v2[j][i])) {
            top |= (1 << (dark_map[v2[j][i]]));
          }
        }
      }
      if (dp[top] > sc + 1) {
        dp[top] = sc + 1;
        que.push(make_pair(sc + 1, top));
      }
    }
  }
  cout << dp[0] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> dark_map;
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    dark_map[d[i]] = i;
  }
  vector<vector<int>> v(n, vector<int>(k));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  vector<vector<int>> v2(m);
  for (int i = 0; i < m; i++) {
    v2[i] = v[d[i]];
  }
  const int INF = 1e9;
  vector<int> dp(1 << m, INF);
  int first_p = (1 << m) - 1;
  dp[first_p] = 0;
  pair<int, int> state = make_pair(0, first_p);
  priority_queue<pair<int, int>> que;
  que.push(state);

  while (!que.empty()) {
    state = que.top();
    que.pop();
    int p = state.second;
    int sc = state.first;
    if (dp[p] < sc)
      continue;

    for (int i = 0; i < k; i++) {
      int top = 0;
      for (int j = 0; j < m; j++) {
        if (p & (1 << j)) {
          if (dark_map.count(v2[j][i])) {
            top |= (1 << (dark_map[v2[j][i]]));
          }
        }
      }
      if (dp[top] > sc + 1) {
        dp[top] = sc + 1;
        que.push(make_pair(sc + 1, top));
      }
    }
  }
  cout << dp[0] << endl;
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF 100000000
using namespace std;
typedef pair<int, int> pii;

int N, M, K, D[16], V[100][100];
bool visited[1 << 16];

int main(void) {
  cin >> N >> M >> K;
  REP(i, 0, M) cin >> D[i], D[i]--;
  REP(i, 0, N) REP(j, 0, K) {
    int v;
    cin >> v;
    v--;
    V[i][j] = -1;
    REP(k, 0, M) if (i == D[k]) REP(l, 0, M) if (v == D[l]) V[i][j] = l;
  }
  REP(i, 0, 1 << M) visited[i] = false;

  queue<pii> q;
  q.push(pii((1 << M) - 1, 0));

  while (q.size()) {
    int s = q.front().first, d = q.front().second;
    q.pop();

    if (visited[s])
      continue;
    visited[s] = true;

    if (s == 0) {
      cout << d << endl;
      break;
    }

    REP(i, 0, K) {
      int t = 0;
      REP(j, 0, M)
      if (((s >> j) & 1) && V[D[j]][i] >= 0) t = t | (1 << V[D[j]][i]);
      q.push(pii(t, d + 1));
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF 100000000
using namespace std;
typedef pair<int, int> pii;

int N, M, K, D[16], V[100][100];

int main(void) {
  cin >> N >> M >> K;
  REP(i, 0, M) cin >> D[i], D[i]--;
  REP(i, 0, N) REP(j, 0, K) {
    int v;
    cin >> v;
    v--;
    V[i][j] = -1;
    REP(k, 0, M) if (i == D[k]) REP(l, 0, M) if (v == D[l]) V[i][j] = l;
  }

  queue<pii> q;
  q.push(pii((1 << M) - 1, 0));

  while (q.size()) {
    int s = q.front().first, d = q.front().second;
    q.pop();

    if (s == 0) {
      cout << d << endl;
      break;
    }

    REP(i, 0, K) {
      int t = 0;
      REP(j, 0, M)
      if (((s >> j) & 1) && V[D[j]][i] >= 0) t = t | (1 << V[D[j]][i]);
      q.push(pii(t, d + 1));
    }
  }

  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define out(S) cout << (S) << endl;
#define ShowAll(collection)                                                    \\
  for (auto i : collection) {                                                  \\
    out(i);                                                                    \\
  }
#define beginend(v) v.begin(), v.end()

using pii = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvc = vector<vector<char>>;
using ti3 = tuple<int, int, int>;
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}

int N, M, K, D[16];
bool used[1 << 16];
int G[101][101];

int solve() {
  cin >> N >> M >> K;
  rep(i, 1 << 16) used[i] = false;
  rep(i, M) cin >> D[i], D[i]--;
  rep(i, N) rep(j, K) {
    int v;
    cin >> v;
    v--;
    G[i][j] = -1;
    rep(k, M) if (i == D[k]) rep(l, M) if (v == D[l]) G[i][j] = l;
  }

  queue<pii> Q;
  Q.push(pii((1 << M) - 1, 0));

  while (!Q.empty()) {
    int s, distance;
    tie(s, distance) = Q.front();
    Q.pop();

    if (used[s])
      continue;
    used[s] = true;
    if (s == 0)
      return distance;

    rep(i, K) {
      int t = 0;
      rep(j, M) if (((s >> j) & 1) && G[D[j]][i] >= 0) t |= 1 << G[D[j]][i];
      Q.push(pii(t, distance + 1));
    }
  }
  return 0;
}

int main() { out(solve()); }


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define out(S) cout << (S) << endl;
#define ShowAll(collection)                                                    \\
  for (auto i : collection) {                                                  \\
    out(i);                                                                    \\
  }
#define beginend(v) v.begin(), v.end()

using pii = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvc = vector<vector<char>>;
using ti3 = tuple<int, int, int>;
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}

int N, M, K, dic[100];
bool D[100], used[1 << 16];
int G[100][100];

int solve() {
  cin >> N >> M >> K;
  rep(i, N) D[i] = false, dic[i] = -1;
  rep(i, 1 << 16) used[i] = false;
  rep(i, K) {
    int di;
    cin >> di;
    D[di - 1] = true;
    dic[di - 1] = i;
  }
  rep(i, N) rep(j, K) {
    int v;
    cin >> v;
    v--;
    G[i][j] = -1;
    if (D[i] && D[v])
      G[i][j] = dic[v];
  }

  queue<pii> Q;
  Q.push(pii((1 << M) - 1, 0));

  while (!Q.empty()) {
    int s, distance;
    tie(s, distance) = Q.front();
    Q.pop();

    if (used[s])
      continue;
    used[s] = true;
    if (s == 0)
      return distance;

    rep(i, K) {
      int t = 0;
      rep(j, N) if (D[j] && (s >> j) & 1 && G[j][i] >= 0) t |= 1 << G[j][i];
      Q.push(pii(t, distance + 1));
    }
  }
  return 0;
}

int main() { out(solve()); }
Problem p01812 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int n, m, k;
int d[101];
int e[101][101];
int dp[2][1 << 16];
int go[101];
bool calced[1 << 16];

int main(void) {
  scanf("%d%d%d", &n, &m, &k);
  memset(go, -1, sizeof(go));
  for (int i = 0; i < m; i++) {
    scanf("%d", &d[i]);
    d[i]--;
    go[d[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      scanf("%d", &e[i][j]);
      e[i][j]--;
    }
  }
  int prev = 0, now = 1;
  memset(dp, false, sizeof(dp));
  memset(calced, false, sizeof(calced));
  dp[0][(1 << m) - 1] = true;
  int cnt = 0;
  while (1) {
    for (int i = 0; i < (1 << m); i++) {
      if (dp[prev][i]) {
        if (calced[i])
          continue;
        calced[i] = true;
        for (int j = 0; j < k; j++) {
          int bit = 0;
          for (int l = 0; l < m; l++) {
            if (i >> l & 1) {
              if (go[e[d[l]][j]] >= 0) {
                bit |= (1 << go[e[d[l]][j]]);
              }
            }
          }
          dp[now][bit] = true;
        }
      }
    }
    cnt++;
    if (dp[now][0]) {
      printf("%d\\n", cnt);
      return 0;
    }
    swap(now, prev);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int n, m, k;
int d[101];
int e[101][101];
int dp[2][1 << 16];
int go[101];

int main(void) {
  scanf("%d%d%d", &n, &m, &k);
  memset(go, -1, sizeof(go));
  for (int i = 0; i < m; i++) {
    scanf("%d", &d[i]);
    d[i]--;
    go[d[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      scanf("%d", &e[i][j]);
      e[i][j]--;
    }
  }
  int prev = 0, now = 1;
  memset(dp, false, sizeof(dp));
  dp[0][(1 << m) - 1] = true;
  int cnt = 0;
  while (1) {
    for (int i = 0; i < (1 << m); i++) {
      if (dp[prev][i]) {
        for (int j = 0; j < k; j++) {
          int bit = 0;
          for (int l = 0; l < m; l++) {
            if (i >> l & 1) {
              if (go[e[d[l]][j]] >= 0) {
                bit |= (1 << go[e[d[l]][j]]);
              }
            }
          }
          dp[now][bit] = true;
        }
      }
    }
    cnt++;
    if (dp[now][0]) {
      printf("%d\\n", cnt);
      return 0;
    }
    swap(now, prev);
  }
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> dark(m);
  vector<int> isDark(n, -1);
  for (int i = 0; i < m; i++) {
    int d;
    cin >> d;
    d--;
    isDark[d] = i;
    dark[i] = d;
  }

  vector<vector<int>> v(n, vector<int>(k));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int val;
      cin >> val;
      val--;
      v[i][j] = val;
    }
  }

  vector<vector<int>> edge(1 << m, vector<int>(k));
  for (int i = 0; i < (1 << m); i++) {
    for (int j = 0; j < k; j++) {
      int to = 0;
      for (int r = 0; r < m; r++) {
        if ((i & (1 << r)) && isDark[v[dark[r]][j]] != -1) {
          to |= (1 << isDark[v[dark[r]][j]]);
        }
      }
      edge[i][j] = to;
    }
  }

  const int INF = 1e9;
  vector<int> d(1 << m, INF);
  queue<pair<int, int>> que;
  d[(1 << m) - 1] = 0;
  que.push({(1 << m) - 1, 0});
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    if (p.first == 0) {
      cout << p.second << endl;
      return 0;
    }
    for (int i = 0; i < edge[p.first].size(); i++) {
      if (d[edge[p.first][i]] >= INF) {
        d[edge[p.first][i]] = p.second + 1;
        que.push({edge[p.first][i], p.second + 1});
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  map<int, int> dark;
  for (int i = 0; i < m; i++) {
    int d;
    cin >> d;
    d--;
    dark[d] = i;
  }

  vector<vector<int>> v(n, vector<int>(k));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int val;
      cin >> val;
      val--;
      v[i][j] = val;
    }
  }

  vector<vector<int>> edge(1 << m, vector<int>(k));
  for (int i = 0; i < (1 << m); i++) {
    for (int j = 0; j < k; j++) {
      int to = 0;
      for (auto &e : dark) {
        int r = e.first;
        if (dark.count(r) && (i & (1 << dark[r])) && dark.count(v[r][j])) {
          to |= (1 << dark[v[r][j]]);
        }
      }
      edge[i][j] = to;
    }
  }

  const int INF = 1e9;
  vector<int> d(1 << m, INF);
  queue<pair<int, int>> que;
  d[(1 << m) - 1] = 0;
  que.push({(1 << m) - 1, 0});
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    if (p.first == 0) {
      cout << p.second << endl;
      return 0;
    }
    for (int i = 0; i < edge[p.first].size(); i++) {
      if (d[edge[p.first][i]] >= INF) {
        d[edge[p.first][i]] = p.second + 1;
        que.push({edge[p.first][i], p.second + 1});
      }
    }
  }

  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int V[100][100]; //[v][i] leads to
int isDark
    [100]; //-1テ」ツ?ェテ」ツつ嘉ヲツ伉偲」ツつ凝」ツ??0~M-1テ」ツ?ァテヲツ堋療」ツ??
const int INF = 100000;
int D[16];
int N, M, K;

int dist[1 << 16];
int move_to[1 << 16][100];
bool isvisit[1 << 16];

int main() {
  cin >> N >> M >> K;
  fill(isDark, isDark + 100, -1);
  fill(dist, dist + (1 << 16), INF);
  fill(isvisit, isvisit + (1 << 16), false);
  for (int i = 0; i < M; i++) {
    cin >> D[i];
    D[i]--;
    isDark[D[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      cin >> V[i][j];
      V[i][j]--;
    }
  }
  for (int b = 0; b < (1 << M); b++) {
    for (int k = 0; k < K; k++) {
      move_to[b][k] = 0;
      for (int m = 0; m < M; m++) {
        if (b & (1 << m)) {
          int to = V[D[m]][k];
          if (isDark[to] != -1) {
            move_to[b][k] |= 1 << isDark[to];
          }
        }
      }
    }
  }
  deque<P> que;
  dist[(1 << M) - 1] = 0;
  que.emplace_back(0, (1 << M) - 1);
  while (!que.empty()) {
    int d, v;
    tie(d, v) = que.front();
    que.pop_front();
    if (!v) {
      cout << d << endl;
      return 0;
    }
    if (isvisit[v])
      continue;
    isvisit[v] = true;
    for (int k = 0; k < K; k++) {
      int to = move_to[v][k];
      if (dist[to] > d + 1) {
        que.emplace_back(d + 1, to);
        dist[to] = d + 1;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int E[100][100]; //[v][i] leads to
int DP[1 << 16];
int isDark
    [100]; //-1テ」ツ?ェテ」ツつ嘉ヲツ伉偲」ツつ凝」ツ??0~M-1テ」ツ?ァテヲツ堋療」ツ??
const int INF = 100000;
int D[16];
int N, M, K;

int memo[1 << 16];

int move(int b, int k) {
  int bb = 0;
  for (int m = 0; m < M; m++) {
    if (b & (1 << m)) {
      int to = isDark[E[D[m]][k]];
      if (to != -1) {
        bb |= 1 << to;
      }
    }
  }
  return bb;
}

int dist(int b) {
  static bool isvisit[1 << 16];
  if (isvisit[b])
    return INF;
  if (!b)
    return 0;
  if (memo[b] != -1)
    return memo[b];
  isvisit[b] = true;
  int d = INF;
  for (int k = 0; k < K; k++) {
    d = min(d, dist(move(b, k)) + 1);
  }
  if (d != INF)
    memo[b] = d;
  isvisit[b] = false;
  return d;
}

int main() {
  cin >> N >> M >> K;
  fill(isDark, isDark + 100, -1);
  for (int i = 0; i < M; i++) {
    cin >> D[i];
    D[i]--;
    isDark[D[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      cin >> E[i][j];
      E[i][j]--;
    }
  }

  fill(memo, memo + (1 << 16), -1);

  /**
  fill(DP,DP+(1<<M),INF);
  DP[0]=0;
  for(int k=1;k<=M;k++){
      //MCkテ」ツつ津・ツ按療ヲツ個?
      int comb = (1<<k) -1;
      while(comb<1<<M){
          for(int i=0;i<K;i++){
              DP[comb]=min(DP[comb],DP[move(comb,i)]+1);
          }

          int x=comb&-comb, y=comb+x;
          comb = ((comb&~y)/x>>1)|y;
      }
  }
  cout<<DP[(1<<M)-1]<<endl;
  **/
  cout << dist((1 << M) - 1) << endl;

  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi acos(-1.0)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int INF = 1e9;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  VI d(n, -1);
  REP(i, m) {
    int x;
    cin >> x;
    d[x - 1] = i;
  }
  VVI e(m, VI(k));
  REP(i, n) REP(j, k) {
    int x;
    cin >> x;
    if (d[i] == -1)
      continue;
    e[d[i]][j] = d[x - 1];
  }

  VI dist(1 << m, INF);
  dist[(1 << m) - 1] = 0;
  queue<int> que;
  que.push((1 << m) - 1);
  while (dist[0] == INF) {
    int a = que.front();
    que.pop();
    REP(j, k) {
      int x = 0;
      REP(i, m) {
        if ((a >> i) & 1 && e[i][j] >= 0) {
          x |= (1 << e[i][j]);
        }
      }
      if (dist[x] != INF)
        continue;
      dist[x] = dist[a] + 1;
      que.push(x);
    }
  }

  cout << dist[0] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi acos(-1.0)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int INF = 1e9;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  VI d(n, -1);
  REP(i, m) {
    int x;
    cin >> x;
    d[x - 1] = i;
  }
  VVI e(m, VI(k));
  REP(i, n) REP(j, k) {
    int x;
    cin >> x;
    if (d[i] == -1)
      continue;
    e[d[i]][j] = d[x - 1];
  }

  VI dist(1 << m, INF);
  dist[(1 << m) - 1] = 0;
  queue<int> que;
  que.push((1 << m) - 1);
  while (dist[0] == INF) {
    int a = que.front();
    que.pop();
    REP(j, k) {
      int x = 0;
      REP(i, n) {
        if ((a >> i) & 1 && e[i][j] >= 0) {
          x += (1 << e[i][j]);
        }
      }
      if (dist[x] != INF)
        continue;
      dist[x] = dist[a] + 1;
      que.push(x);
    }
  }

  cout << dist[0] << endl;

  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> mp;
  int v[101][101] = {};
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    mp[d[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  int bits = (1 << m) - 1;
  vector<bool> flag((1 << m) + 1);
  vector<int> dst((1 << m) + 1);
  queue<int> q;
  q.push(bits);
  flag[bits] = 1;
  dst[bits] = 0;
  while (!q.empty()) {
    auto x = q.front();
    q.pop();
    for (int i = 0; i < k; i++) {
      int sa = 0;
      for (int j = 0; j < m; j++) {
        if (x & (1 << j)) {
          if (mp.find(v[d[j]][i]) != mp.end()) {
            int y = mp[v[d[j]][i]];
            if (!(sa & 1 << y))
              sa += 1 << y;
          }
        }
      }
      if (!flag[sa]) {
        flag[sa] = 1;
        dst[sa] = dst[x] + 1;
        q.push(sa);
      }
    }
  }
  cout << dst[0] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> mp;
  int v[101][101] = {};
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    mp[d[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  int bits = (1 << m) - 1;
  vector<bool> flag((1 << m) + 1);
  vector<int> dst((1 << m) + 1);
  queue<int> q;
  q.push(bits);
  flag[bits] = 1;
  dst[bits] = 0;
  while (!q.empty()) {
    auto x = q.front();
    q.pop();
    for (int i = 0; i < k; i++) {
      int sa = 0;
      for (int j = 0; j < m; j++) {
        if (x & (1 << j)) {
          if (mp.find(v[d[j]][i]) != mp.end()) {
            int y = mp[v[d[j]][i]];
            sa += 1 << y;
          }
        }
      }
      if (!flag[sa]) {
        flag[sa] = 1;
        dst[sa] = dst[x] + 1;
        q.push(sa);
      }
    }
  }
  cout << dst[0] << endl;
  return 0;
}