alexjercan/bugnet huggingface dataset:
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

vector<int> result((1 << 16), -1);

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int d[16];
  vector<int> isdark(101, -1);
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    isdark[d[i]] = i;
  }
  int v[101][101];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  result[(1 << m) - 1] = 0;
  queue<int> qu;
  qu.push((1 << m) - 1);
  while (!qu.empty()) {
    int now = qu.front();
    qu.pop();
    for (int j = 0; j < k; j++) {
      // 状態nowから、j番目の道を選んだとする
      int next = 0;
      for (int l = 0; l < m; l++) {
        // l番目のくらい部屋にいる人が次どこに行くかを考える
        if ((now & (1 << l)) == 0)
          continue;
        int nextroom = v[d[l]][j];
        if (isdark[nextroom] >= 0)
          next |= (1 << isdark[nextroom]);
      }
      if (result[next] == -1) {
        result[next] = result[now] + 1;
        qu.push(next);
      }
    }
  }
  cout << result[0] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int d[16];
  map<int, int> mm;
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    mm[d[i]] = i;
  }
  int v[101][101];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  vector<int> result((1 << 16), mod);
  result[(1 << m) - 1] = 0;
  for (int i = (1 << m) - 1; i >= 0; i--) {
    for (int j = 0; j < k; j++) {
      // 状態iから、j番目の道を選んだとする
      int next = 0;
      for (int l = 0; l < m; l++) {
        // l番目のくらい部屋にいる人が次どこに行くかを考える
        if ((i & (1 << l)) == 0)
          continue;
        int nextroom = v[d[l]][j];
        // tmp =
        // nextroomがくらい部屋ならば、配列dにおいて何番目の部屋なのかを指すitr
        auto tmp = mm.find(nextroom);
        // cout << l << " " << nextroom << " " << (tmp == mm.end()) << endl;
        if (tmp != mm.end())
          next += (1 << (*tmp).second);
      }
      // cout << i << " next " << next << endl;
      result[next] = min(result[next], result[i] + 1);
    }
  }
  cout << result[0] << endl;
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const int INF = numeric_limits<int>::max() / 2;
typedef vector<int> VI;
typedef vector<VI> VVI;

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

  int n, m, k;
  cin >> n >> m >> k;
  std::vector<int> d(m + 1);
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
  }
  VVI v(n + 1, vector<int>(k + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  std::vector<int> dp((1 << m) + 1, INF);
  dp[(1 << m) - 1] = 0;
  queue<int> que;
  que.push((1 << m) - 1);
  while (!que.empty()) {
    for (int i = 0; i < k; i++) {
      int tmp = que.front(), next = 0;
      for (int j = 0; j < m; j++) {
        if (tmp % 2) {
          int mv = v[d[j]][i];
          for (int p = 0; p < m; p++) {
            if (d[p] == mv)
              next |= (1 << p);
          }
        }
        tmp /= 2;
      }
      // cout<<next<<endl;
      // cout<<"*"<<dp[next]<<endl;
      if (dp[next] == INF) {
        que.push(next);
      }
      dp[next] = min(dp[next], dp[que.front()] + 1);
    }
    que.pop();
  }
  cout << dp[0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int INF = numeric_limits<int>::max() / 2;
typedef vector<int> VI;
typedef vector<VI> VVI;

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

  int n, m, k;
  cin >> n >> m >> k;
  std::vector<int> d(m + 1);
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
  }
  VVI v(n + 1, vector<int>(k + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  std::vector<int> dp((1 << m) + 1, INF);
  dp[(1 << m) - 1] = 0;
  // cout<<0<<endl;
  queue<int> que;
  que.push((1 << m) - 1);
  cout << "";
  while (!que.empty()) {
    for (int i = 0; i < k; i++) {
      int tmp = que.front(), next = 0;
      for (int j = 0; j < m; j++) {
        if (tmp % 2) {
          int mv = v[d[j]][i];
          for (int p = 0; p < m; p++) {
            if (d[p] == mv)
              next += (1 << p);
          }
        }
        tmp /= 2;
      }
      if (dp[next] == INF) {
        que.push(next);
      }
      dp[next] = min(dp[next], dp[que.front()] + 1);
      if (next == 0) {
        cout << dp[next] << endl;
        return 0;
      }
    }
    que.pop();
  }
  // cout<<dp[0]<<endl;
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int x[101][101];
vector<int> D;
int n, m, p, dp[100000];
queue<int> Q;
int main() {
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int r;
    cin >> r;
    D.push_back(r);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < p; j++)
      cin >> x[i][j];
  }
  for (int i = 1; i < 100000; i++)
    dp[i] = 998244353;
  Q.push(0);
  while (!Q.empty()) {
    int i = Q.front();
    Q.pop();
    int bit[16];
    for (int j = 0; j < m; j++)
      bit[j] = (i / (1 << j)) % 2;

    for (int o = 0; o < p; o++) {
      int bit2[16];
      for (int k = 0; k < m; k++)
        bit2[k] = 1;
      for (int j = 0; j < m; j++) {
        if (bit[j] == 0) {
          int to = x[D[j]][o];
          for (int l = 0; l < m; l++) {
            if (D[l] == to)
              bit2[l] = 0;
          }
        }
      }
      int cnt = 0;
      for (int j = 0; j < m; j++)
        cnt += (1 << j) * bit2[j];
      if (dp[cnt] == 998244353) {
        dp[cnt] = min(dp[cnt], dp[i] + 1);
        Q.push(cnt);
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int x[101][101];
vector<int> D;
int n, m, p, dp[100000];
queue<int> Q;
int main() {
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int r;
    cin >> r;
    D.push_back(r);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < p; j++)
      cin >> x[i][j];
  }
  for (int i = 1; i < 100000; i++)
    dp[i] = 998244353;
  Q.push(0);
  while (!Q.empty()) {
    int i = Q.front();
    int bit[16];
    for (int j = 0; j < m; j++)
      bit[j] = (i / (1 << j)) % 2;

    for (int o = 0; o < p; o++) {
      int bit2[16];
      for (int k = 0; k < m; k++)
        bit2[k] = 1;
      for (int j = 0; j < m; j++) {
        if (bit[j] == 0) {
          int to = x[D[j]][o];
          for (int l = 0; l < m; l++) {
            if (D[l] == to)
              bit2[l] = 0;
          }
        }
      }
      int cnt = 0;
      for (int j = 0; j < m; j++)
        cnt += (1 << j) * bit2[j];
      if (dp[cnt] == 998244353) {
        dp[cnt] = min(dp[cnt], dp[i] + 1);
        Q.push(cnt);
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  rep(i, m) {
    cin >> d[i];
    d[i]--;
  }
  vector<vector<int>> v(n, vector<int>(k));
  rep(i, n) {
    rep(j, k) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }

  int dp[1 << 16];
  rep(i, 1 << 16) dp[i] = INF;
  dp[(1 << m) - 1] = 0;

  queue<ll> que;
  vector<bool> used(1 << 16, false);
  que.push((1 << m) - 1);
  used[(1 << m) - 1] = true;

  while (que.size()) {
    int q = que.front();
    que.pop();

    rep(i, k) {
      int t = 0;
      rep(j, m) {
        if (q & (1 << j)) {
          int next_room = v[d[j]][i];
          int to = -1;
          rep(l, m) {
            if (d[l] == next_room)
              to = l;
          }
          if (to != -1)
            t |= 1 << to;
        }
      }
      if (!used[t])
        que.push(t);
      used[t] = true;
      dp[t] = min(dp[q] + 1, dp[t]);
    }
  }
  cout << dp[0] << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  rep(i, m) {
    cin >> d[i];
    d[i]--;
  }
  vector<vector<int>> v(n, vector<int>(k));
  rep(i, n) {
    rep(j, k) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }

  int dp[1 << 16];
  rep(i, 1 << 16) dp[i] = INF;
  dp[(1 << m) - 1] = 0;

  queue<ll> que;
  vector<bool> used(1 << m, false);
  que.push((1 << m) - 1);
  used[(1 << m) - 1] = true;

  while (que.size()) {
    int q = que.front();
    que.pop();

    rep(i, k) {
      int t = 0;
      rep(j, m) {
        if (q & (1 << j)) {
          int next_room = v[d[j]][i];
          int to = -1;
          rep(l, m) {
            if (d[l] == next_room)
              to = l;
          }
          if (to != -1)
            t += 1 << to;
        }
      }
      if (!used[t])
        que.push(t);
      used[t] = true;
      dp[t] = min(dp[q] + 1, dp[t]);
    }
  }
  cout << dp[0] << endl;
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

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

int n, m, k;
int d[105] = {0};
int point[20] = {0};
int dp[1000000] = {0};
int memo[105][105] = {0};
queue<int> qu;

int solve();

int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; ++i)
    d[i] = -1;
  for (int i = 0; i < m; ++i) {
    cin >> point[i];
    --point[i];
    d[point[i]] = i;
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < k; ++j) {
      cin >> memo[i][j];
      --memo[i][j];
    }
  cout << solve() << endl;
}

int solve() {
  int now, nextn;
  qu.push((1 << m) - 1);
  dp[(1 << m) - 1] = 0;
  while (qu.size() > 0) {
    now = qu.front();
    qu.pop();
    for (int i = 0; i < k; ++i) {
      nextn = 0;
      for (int j = 0; j < m; ++j)
        if (((1 << j) & now) == (1 << j)) {
          int np = point[j];
          int x = memo[np][i], bit;
          if (d[x] == -1)
            continue;
          bit = (1 << d[x]);
          nextn |= bit;
        }
      if (nextn != (1 << m) - 1 && dp[nextn] == 0) {
        dp[nextn] = dp[now] + 1;
        qu.push(nextn);
      }
    }
  }
  return dp[0];
}


Error: 0

Fixed submission:

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

int n, m, k;
int d[105] = {0};
int point[20] = {0};
int dp[100000] = {0};
int memo[105][105] = {0};
queue<int> qu;

int solve();

int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; ++i)
    d[i] = -1;
  for (int i = 0; i < m; ++i) {
    cin >> point[i];
    --point[i];
    d[point[i]] = i;
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < k; ++j) {
      cin >> memo[i][j];
      --memo[i][j];
    }
  cout << solve() << endl;
}

int solve() {
  int now, nextn;
  qu.push((1 << m) - 1);
  dp[(1 << m) - 1] = 0;
  while (qu.size() > 0) {
    now = qu.front();
    qu.pop();
    for (int i = 0; i < k; ++i) {
      nextn = 0;
      for (int j = 0; j < n; ++j)
        if (((1 << j) & now) == (1 << j)) {
          int np = point[j];
          int x = memo[np][i], bit;
          if (d[x] == -1)
            continue;
          bit = (1 << d[x]);
          nextn |= bit;
        }
      if (nextn != (1 << m) - 1 && dp[nextn] == 0) {
        dp[nextn] = dp[now] + 1;
        qu.push(nextn);
      }
    }
  }
  return dp[0];
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long N, M, K;
long D[20];
int roomlump[110];
long v[105][105];
long long dist[1 << 17], INF = 2000;
queue<long long> que;

int main() {
  cin >> N >> M >> K;
  fill(roomlump, roomlump + 110, -1);

  for (int i = 0; i < M; i++) {
    cin >> D[i];
    D[i]--;
    roomlump[D[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  long long initial = ((1 << M) - 1);
  fill(dist, dist + (1 << M), INF);
  que.push(initial);
  dist[(1 << M) - 1] = 0;

  while (!que.empty()) {
    long long current = que.front();
    que.pop();
    for (int j = 0; j < K; j++) {
      long long next = 0;
      for (int i = 0; i < M; i++) {
        if ((current) & (1 << i)) {
          if (roomlump[v[D[i]][j]] != -1) {
            next |= 1 << (roomlump[v[D[i]][j]]);
          }
        }
      }
      if (dist[next] > dist[current] + 1) {
        dist[next] = dist[current] + 1;
        if (next != 0)
          que.push(next);
      }
    }
  }
  cout << dist[0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, M, K;
int D[20];
bool roomlump[110];
int v[105][105];
int dist[1 << 16], INF = 2000;

int main() {
  cin >> N >> M >> K;
  fill(roomlump, roomlump + 110, true);

  for (int i = 0; i < M; i++) {
    cin >> D[i];
    D[i]--;
    roomlump[D[i]] = false;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  long long initial = ((1 << M) - 1);
  queue<long long> que;
  fill(dist, dist + (1 << M), INF);
  que.push(initial);
  dist[(1 << M) - 1] = 0;

  while (!que.empty()) {
    long long current = que.front();
    que.pop();
    for (int j = 0; j < K; j++) {
      long long next = 0;
      for (int i = 0; i < M; i++) {
        if ((current) & (1 << i)) {
          if (roomlump[v[D[i]][j]] == false)
            next |= 1 << (v[D[i]][j]);
        }
      }
      if (dist[next] > dist[current] + 1) {
        dist[next] = dist[current] + 1;
        if (next != 0)
          que.push(next);
      }
    }
  }
  cout << dist[0] << endl;
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main() {
  int N, M, K, D[16], A[100][100];
  int black[100];
  memset(black, -1, sizeof(black));

  cin >> N >> M >> K;
  for (int i = 0; i < M; i++) {
    cin >> D[i];
    black[--D[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      cin >> A[i][j];
      --A[i][j];
    }
  }

  int dp[1 << 16];
  fill_n(dp, 1 << 16, -1);
  queue<int> que;
  que.push((1 << M) - 1);
  dp[(1 << M) - 1] = 0;

  while (!que.empty()) {
    int i = que.front();
    que.pop();
    if (i == 0)
      break;
    for (int j = 0; j < K; j++) {
      int bitMask = 0;
      for (int k = 0; k < M; k++) {
        if ((i >> k) & 1 && ~black[A[D[k]][j]])
          bitMask |= 1 << black[A[D[k]][j]];
      }
      if (dp[bitMask] == -1) {
        que.push(bitMask);
        dp[bitMask] = dp[i] + 1;
      }
    }
  }
  cout << dp[0] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main() {
  int N, M, K, D[16], A[100][100];
  bool black[100] = {};

  cin >> N >> M >> K;
  for (int i = 0; i < M; i++) {
    cin >> D[i];
    black[--D[i]] = true;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      cin >> A[i][j];
      --A[i][j];
    }
  }

  int dp[1 << 16];
  fill_n(dp, 1 << 16, INF);
  dp[(1 << M) - 1] = 0;
  for (int i = (1 << M) - 1; i > 0; i--) {
    for (int j = 0; j < K; j++) {
      int bitMask = 0;
      for (int k = 0; k < N; k++) {
        if ((i >> k) & 1 && black[A[k][j]])
          bitMask |= 1 << A[k][j];
      }
      dp[bitMask] = min(dp[bitMask], dp[i] + 1);
    }
  }
  cout << dp[0] << endl;
}
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);
  vector<int> idx(N, -1);
  for (int i = 0; i < M; i++) {
    cin >> d[i];
    idx[d[i] - 1] = i;
  }

  vector<vector<int>> v(N, vector<int>(K));
  for (auto &i : v)
    for (auto &j : i)
      cin >> j;

  vector<int> dp(1 << M, 1 << 28);
  dp[0] = 0;

  vector<vector<int>> nx(K, vector<int>(1 << M));
  for (int i = 1; i < (1 << M); i++) {
    for (int j = 0; j < K; j++) {
      int x = 0;
      for (int k = 0; k < M; k++) {
        auto t = idx[v[d[k] - 1][j] - 1];
        if ((i & (1 << k)) && t >= 0)
          x |= (1 << t);
      }
      nx[j][i] = x;
      dp[i] = min(dp[i], dp[x] + 1);
    }
  }

  for (int g = 0; g < 2 * M; g++) {
    for (int i = 1; i < (1 << M); i++) {
      for (int j = 0; j < K; j++) {
        int x = nx[j][i];
        dp[i] = min(dp[i], dp[x] + 1);
      }
    }
  }

  cout << dp[(1 << M) - 1] << 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);
  vector<int> idx(N, -1);
  for (int i = 0; i < M; i++) {
    cin >> d[i];
    idx[d[i] - 1] = i;
  }

  vector<vector<int>> v(N, vector<int>(K));
  for (auto &i : v)
    for (auto &j : i)
      cin >> j;

  vector<int> dp(1 << M, 1 << 28);
  dp[0] = 0;

  for (int g = 0; g < M; g++) {
    for (int i = 1; i < (1 << M); i++) {
      for (int j = 0; j < K; j++) {
        int x = 0;
        for (int k = 0; k < M; k++) {
          auto t = idx[v[d[k] - 1][j] - 1];
          if ((i & (1 << k)) && t >= 0)
            x |= (1 << t);
        }
        dp[i] = min(dp[i], dp[x] + 1);
      }
    }
  }

  cout << dp[(1 << M) - 1] << endl;

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

#include <bits/stdc++.h>
using namespace std;
int memo[1 << 16][100];
int main() {
  int n, m, k, i, j, l, a, b, e, inf = 1 << 28;
  cin >> n >> m >> k;
  int dp[1 << m], d[n], c[k];
  map<int, int> r;
  vector<int> G[n][k];
  for (b = 0; b < (1 << m); b++)
    dp[b] = inf;
  for (i = 0; i < m; i++)
    cin >> d[i], r[--d[i]] = i;
  for (i = 0; i < n; i++) {
    for (j = 0; j < k; j++) {
      cin >> e;
      if (r.find(i) != r.end())
        G[e - 1][j].push_back(i);
    }
  }
  typedef pair<int, int> P;
  queue<P> q;
  q.push(P(0, 0));

  for (j = 0; j < k; j++) {
    c[j] = 0;
    for (i = 0; i < n; i++) {
      if (r.find(i) != r.end())
        continue;
      for (l = 0; l < G[i][j].size(); l++)
        if (r.find(G[i][j][l]) != r.end())
          c[j] |= (1 << r[G[i][j][l]]);
    }
  }
  memset(memo, -1, sizeof(memo));
  while (!q.empty()) {
    a = q.front().first;
    b = q.front().second;
    q.pop();
    if (dp[b] <= a)
      continue;
    dp[b] = a;
    if (b == (1 << m) - 1)
      break;
    for (j = 0; j < k; j++) {
      int o = 0;
      if (~memo[b][j]) {
        o = memo[b][j];
        if (a + 1 < dp[(o | c[j])])
          q.push(P(a + 1, (o | c[j])));
        break;
      }
      for (i = 0; i < m; i++) {
        if (((b >> i) & 1) == 0)
          continue;
        for (l = 0; l < G[d[i]][j].size(); l++)
          o |= (1 << r[G[d[i]][j][l]]);
      }
      memo[b][j] = o;
      if (a + 1 < dp[(o | c[j])])
        q.push(P(a + 1, (o | c[j])));
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int memo[1 << 16][100];
int main() {
  int n, m, k, i, j, l, a, b, e, inf = 1 << 28;
  cin >> n >> m >> k;
  int dp[1 << m], d[n], c[k];
  map<int, int> r;
  vector<int> G[n][k];
  for (b = 0; b < (1 << m); b++)
    dp[b] = inf;
  for (i = 0; i < m; i++)
    cin >> d[i], r[--d[i]] = i;
  for (i = 0; i < n; i++)
    for (j = 0; j < k; j++)
      cin >> e, G[e - 1][j].push_back(i);
  typedef pair<int, int> P;
  queue<P> q;
  q.push(P(0, 0));

  for (j = 0; j < k; j++) {
    c[j] = 0;
    for (i = 0; i < n; i++) {
      if (r.find(i) != r.end())
        continue;
      for (l = 0; l < G[i][j].size(); l++)
        if (r.find(G[i][j][l]) != r.end())
          c[j] |= (1 << r[G[i][j][l]]);
    }
  }
  memset(memo, -1, sizeof(memo));
  while (!q.empty()) {
    a = q.front().first;
    b = q.front().second;
    q.pop();
    if (dp[b] <= a)
      continue;
    dp[b] = a;
    if (b == (1 << m) - 1)
      break;
    for (j = 0; j < k; j++) {
      int o = 0;
      if (~memo[b][j]) {
        o = memo[b][j];
        if (a + 1 < dp[(o | c[j])])
          q.push(P(a + 1, (o | c[j])));
        break;
      }
      for (i = 0; i < m; i++) {
        if (((b >> i) & 1) == 0)
          continue;
        for (l = 0; l < G[d[i]][j].size(); l++)
          if (r.find(G[d[i]][j][l]) != r.end())
            o |= (1 << r[G[d[i]][j][l]]);
      }
      memo[b][j] = o;
      if (a + 1 < dp[(o | c[j])])
        q.push(P(a + 1, (o | c[j])));
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}
Problem p01812 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M, K;
vector<vector<vector<int>>> rV;
int utod[100];
int sD[100];
int dist[1 << 16];

int bfs() {
  memset(dist, -1, sizeof(dist));
  queue<int> que;

  dist[0] = 0;
  que.push(0);

  while (!que.empty()) {
    int s = que.front();
    que.pop();
    if (s == (1 << M) - 1)
      return dist[s];

    for (int i = 0; i < K; i++) {
      int t = 0;
      for (int u = 0; u < N; u++) {
        if (!sD[u]) {
          // light
          for (auto v : rV[u][i])
            t |= 1 << v;
        } else {
          // dark
          if (((s >> utod[u]) & 1) == 0)
            continue;
          for (auto v : rV[u][i])
            t |= 1 << v;
        }
      }
      if (dist[t] == -1) {
        dist[t] = dist[s] + 1;
        que.push(t);
      }
    }
  }
  return -1;
}

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

  cin >> N >> M >> K;

  for (int i = 0; i < M; i++) {
    int d;
    cin >> d;
    d--;
    utod[d] = i;
    sD[d] = true;
  }
  rV.resize(N);
  for (int i = 0; i < N; i++)
    rV[i].resize(K);

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      int v;
      cin >> v;
      v--;
      if (!sD[i])
        continue;
      rV[v][j].push_back(utod[i]);
    }
  }

  cout << bfs() << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int N, M, K;
vector<int> D;
set<int> sD;
vector<vector<int>> V;
vector<vector<vector<int>>> rV;
map<int, int> utod;

int bfs() {
  int s = 0;
  vector<int> dist(1 << M, -1);
  queue<int> que;

  dist[s] = 0;
  que.push(s);

  while (!que.empty()) {
    int s = que.front();
    que.pop();
    if (s == (1 << M) - 1)
      return dist[s];

    for (int i = 0; i < K; i++) {
      int t = 0;
      for (int u = 0; u < N; u++) {
        if (sD.find(u) == sD.end()) {
          // light
          for (auto v : rV[u][i])
            t |= 1 << v;
        } else {
          // dark
          if (((s >> utod[u]) & 1) == 0)
            continue;
          for (auto v : rV[u][i])
            t |= 1 << v;
        }
      }
      if (dist[t] == -1) {
        dist[t] = dist[s] + 1;
        que.push(t);
      }
    }
  }
  return -1;
}

int main() {
  cin >> N >> M >> K;
  D.resize(M);
  for (int i = 0; i < M; i++) {
    cin >> D[i];
    D[i]--;
    utod[D[i]] = i;
    sD.insert(D[i]);
  }
  V.resize(N);
  rV.resize(N);
  for (int i = 0; i < N; i++) {
    V[i].resize(K), rV[i].resize(K);
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      cin >> V[i][j];
      V[i][j]--;
      if (sD.find(i) == sD.end())
        continue;
      rV[V[i][j]][j].push_back(utod[i]);
    }
  }

  cout << bfs() << endl;
  return 0;
}