prithivMLmods/Coder-Stat huggingface dataset:
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

int main() {
  ll N, M;
  cin >> N >> M;

  struct Key {
    uint32_t open;
    ll cost;
  };

  constexpr size_t MAXSIZE = (1 << 13);

  // state, cost
  vector<ll> dp(MAXSIZE, -1);
  dp[0] = 0;

  vector<Key> keys(M);
  for (ll i = 0; i < M; i++) {
    ll cost, open_N;
    cin >> cost >> open_N;
    uint32_t open = 0;
    for (ll j = 0; j < open_N; j++) {
      ll x;
      cin >> x;
      x--;
      open |= (1 << x);
    }
    keys[i] = {open, cost};
  }

  for (uint32_t i = 0; i < (1 << M); i++) {
    if (dp[i] == -1) {
      continue;
    }
    for (const auto &key : keys) {
      ll to = (i | key.open);
      if (dp[to] == -1) {
        dp[to] = dp[i] + key.cost;
      } else {
        dp[to] = min(dp[to], dp[i] + key.cost);
      }
    }
  }
  cout << dp[(1 << N) - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

int main() {
  ll N, M;
  cin >> N >> M;

  struct Key {
    uint32_t open;
    ll cost;
  };

  constexpr size_t MAXSIZE = (1 << 13);

  // state, cost
  vector<ll> dp(MAXSIZE, -1);
  dp[0] = 0;

  vector<Key> keys(M);
  for (ll i = 0; i < M; i++) {
    ll cost, open_N;
    cin >> cost >> open_N;
    uint32_t open = 0;
    for (ll j = 0; j < open_N; j++) {
      ll x;
      cin >> x;
      x--;
      open |= (1 << x);
    }
    keys[i] = {open, cost};
  }

  for (uint32_t i = 0; i < (1 << N); i++) {
    if (dp[i] == -1) {
      continue;
    }
    for (const auto &key : keys) {
      ll to = (i | key.open);
      if (dp[to] == -1) {
        dp[to] = dp[i] + key.cost;
      } else {
        dp[to] = min(dp[to], dp[i] + key.cost);
      }
    }
  }
  cout << dp[(1 << N) - 1] << endl;

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main(void) {
  int n, m, i, j, a[1003], b, c, l[1003] = {}, dp[1003][1003] = {};
  const int INF = 1e9;

  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b;
    for (j = 0; j < b; j++) {
      cin >> c;
      l[i] += (1 << (c - 1));
    }
  }

  for (i = 0; i <= m; i++)
    for (j = 1; j < (1 << n); j++) {
      dp[i][j] = INF;
    }

  dp[0][0] = 0;
  for (i = 0; i < m; i++) {
    for (j = 0; j < (1 << n); j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][j | l[i]] = min(dp[i + 1][j | l[i]], dp[i][j] + a[i]);
    }
  }

  if (dp[m][(1 << n) - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[m][(1 << n) - 1] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main(void) {
  int n, m, i, j, a[1003], b, c, l[1003] = {}, dp[1003][4100] = {};
  const int INF = 1e9;

  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b;
    for (j = 0; j < b; j++) {
      cin >> c;
      l[i] += (1 << (c - 1));
    }
  }

  for (i = 0; i <= m; i++)
    for (j = 1; j < (1 << n); j++) {
      dp[i][j] = INF;
    }

  dp[0][0] = 0;
  for (i = 0; i < m; i++) {
    for (j = 0; j < (1 << n); j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][j | l[i]] = min(dp[i + 1][j | l[i]], dp[i][j] + a[i]);
    }
  }

  if (dp[m][(1 << n) - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[m][(1 << n) - 1] << endl;
  }

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):

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

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

const int N = 13;
const int M = 1001;
int n, m, a, b, c;
ii p[M];
int dp[1 << N];

int main() {
  freopen("in", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    int mask = 0;
    for (int j = 0; j < b; ++j) {
      cin >> c;
      mask |= (1 << (c - 1));
    }
    p[i] = {a, mask};
  }
  memset(dp, 255, sizeof dp);
  dp[0] = 0;

  for (int ms = 0; ms < (1 << n) - 1; ++ms) {
    if (dp[ms] != -1) {
      for (int i = 0; i < m; ++i) {
        auto pa = p[i];
        int cost = pa.first;
        int mm = pa.second;
        int nm = mm | ms;
        if (dp[nm] == -1 || dp[nm] > dp[ms] + cost) {
          dp[nm] = dp[ms] + cost;
        }
      }
    }
  }

  if (dp[(1 << n) - 1] == -1) {
    cout << -1 << endl;
  } else {
    cout << dp[(1 << n) - 1] << endl;
  }

  return 0;
}


Fixed submission:

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

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

const int N = 13;
const int M = 1001;
int n, m, a, b, c;
ii p[M];
int dp[1 << N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    int mask = 0;
    for (int j = 0; j < b; ++j) {
      cin >> c;
      mask |= (1 << (c - 1));
    }
    p[i] = {a, mask};
  }
  memset(dp, 255, sizeof dp);
  dp[0] = 0;

  for (int ms = 0; ms < (1 << n) - 1; ++ms) {
    if (dp[ms] != -1) {
      for (int i = 0; i < m; ++i) {
        auto pa = p[i];
        int cost = pa.first;
        int mm = pa.second;
        int nm = mm | ms;
        if (dp[nm] == -1 || dp[nm] > dp[ms] + cost) {
          dp[nm] = dp[ms] + cost;
        }
      }
    }
  }

  if (dp[(1 << n) - 1] == -1) {
    cout << -1 << endl;
  } else {
    cout << dp[(1 << n) - 1] << endl;
  }

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 3e2 + 5;
ll a[N], dp[1 << 12];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  ll n;
  cin >> n;
  ll m;
  cin >> m;
  dp[0] = 0;
  for (int i = 0; i < (1ll << n); i++)
    dp[i] = 1e18;
  for (int i = 1; i <= m; i++) {
    ll k;
    cin >> a[i] >> k;
    ll sum = 0;
    for (int j = 0; j < k; j++) {
      ll x;
      cin >> x;
      sum |= (1ll << (x - 1));
    }
    for (int j = 1; j < (1ll << n); j++) {
      if ((sum & j) == j) {
        dp[j] = min(dp[j], a[i]);
      }
    }
  }
  for (int i = 1; i < (1ll << n); i++) {
    for (int j = 1; j <= i; j++) {
      if ((i & j) == j) {
        ll x = i;
        x ^= j;
        dp[i] = min(dp[i], dp[j] + dp[x]);
      }
    }
  }
  if (dp[(1ll << n) - 1] != 1e18)
    cout << dp[(1ll << n) - 1];
  else
    cout << -1;
}


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

#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 1e3 + 5;
ll a[N], dp[1 << 12];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  ll n;
  cin >> n;
  ll m;
  cin >> m;
  dp[0] = 0;
  for (int i = 0; i < (1ll << n); i++)
    dp[i] = 1e18;
  for (int i = 1; i <= m; i++) {
    ll k;
    cin >> a[i] >> k;
    ll sum = 0;
    for (int j = 0; j < k; j++) {
      ll x;
      cin >> x;
      sum |= (1ll << (x - 1));
    }
    for (int j = 1; j < (1ll << n); j++) {
      if ((sum & j) == j) {
        dp[j] = min(dp[j], a[i]);
      }
    }
  }
  for (int i = 1; i < (1ll << n); i++) {
    for (int j = 1; j <= i; j++) {
      if ((i & j) == j) {
        ll x = i;
        x ^= j;
        dp[i] = min(dp[i], dp[j] + dp[x]);
      }
    }
  }
  if (dp[(1ll << n) - 1] != 1e18)
    cout << dp[(1ll << n) - 1];
  else
    cout << -1;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 999999999
using namespace std;

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> key;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b;
    int d = 0;
    for (int j = 0; j < b; j++) {
      cin >> c;
      c--;
      d += (1 << c);
    }
    key.push_back(P(a, d));
  }
  int dp[10][4100];
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 4100; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < 4096; j++) {
      dp[i][j | key[i - 1].second] =
          min(dp[i][j | key[i - 1].second], dp[i - 1][j] + key[i - 1].first);
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
    }
  }
  if (dp[m][(1 << n) - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[m][(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 999999999
using namespace std;

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> key;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b;
    int d = 0;
    for (int j = 0; j < b; j++) {
      cin >> c;
      c--;
      d += (1 << c);
    }
    key.push_back(P(a, d));
  }
  int dp[1005][4100];
  for (int i = 0; i < 1005; i++) {
    for (int j = 0; j < 4100; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < 4096; j++) {
      dp[i][j | key[i - 1].second] =
          min(dp[i][j | key[i - 1].second], dp[i - 1][j] + key[i - 1].first);
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
    }
  }
  if (dp[m][(1 << n) - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[m][(1 << n) - 1] << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, M;
int A[1005], B[1005];
int C[1005][15];

int main() {
  const int INF = 1e9;

  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    for (int j = 0; j < B[i]; j++) {
      cin >> C[i][j];
    }
  }

  vector<vector<int>> dp(15, vector<int>(5000, INF));
  for (int i = 0; i < M; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < M; i++) {
    int key = 0;
    for (int j = 0; j < B[i]; j++) {
      key |= (1 << (C[i][j] - 1));
    }
    for (int bits = 0; bits < (1 << N); bits++) {
      chmin(dp[i + 1][bits], dp[i][bits]);
      chmin(dp[i + 1][bits | key], dp[i][bits] + A[i]);
    }
  }
  int ans = dp[M][(1 << N) - 1];
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, M;
int A[1005], B[1005];
int C[1005][15];

int main() {
  const int INF = 1e9;

  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    for (int j = 0; j < B[i]; j++) {
      cin >> C[i][j];
    }
  }

  vector<vector<int>> dp(1005, vector<int>(5000, INF));
  for (int i = 0; i <= M; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < M; i++) {
    int key = 0;
    for (int j = 0; j < B[i]; j++) {
      key |= (1 << (C[i][j] - 1));
    }
    for (int bits = 0; bits < (1 << N); bits++) {
      chmin(dp[i + 1][bits], dp[i][bits]);
      chmin(dp[i + 1][bits | key], dp[i][bits] + A[i]);
    }
  }
  int ans = dp[M][(1 << N) - 1];
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << endl;

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
const int L = 12;

int dp[1 << L];

void solve() {
  int n, m;
  cin >> n >> m;
  const int MSK = 1 << n;

  memset(dp, INF, sizeof dp);
  dp[0] = 0;
  for (int _ = 0; _ < m; _++) {
    int cost, _n;
    cin >> cost >> _n;
    int k = 0;
    for (int __ = 0; __ < _n; __++) {
      int x;
      cin >> x;
      k |= 1 << (x - 1);
    }

    for (int msk = MSK; msk >= 0; msk--) {
      dp[msk | k] = min(dp[msk | k], dp[msk] + cost);
    }
  }
  if (dp[MSK - 1] == INF)
    dp[MSK - 1] = -1;
  cout << dp[MSK - 1];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
const int L = 12;

int dp[1 << L];

void solve() {
  int n, m;
  cin >> n >> m;
  const int MSK = 1 << n;

  memset(dp, INF, sizeof dp);
  dp[0] = 0;
  for (int _ = 0; _ < m; _++) {
    int cost, _n;
    cin >> cost >> _n;
    int k = 0;
    for (int __ = 0; __ < _n; __++) {
      int x;
      cin >> x;
      k |= 1 << (x - 1);
    }

    for (int msk = MSK - 1; msk >= 0; msk--) {
      dp[msk | k] = min(dp[msk | k], dp[msk] + cost);
    }
  }
  if (dp[MSK - 1] == INF)
    dp[MSK - 1] = -1;
  cout << dp[MSK - 1];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ll long long

int main() {
  std::ios::sync_with_stdio(false);
  using namespace std;
  const int max_v = 100'000, max_n = 12, max_cost = max_v * max_n;
  int n, m, a, b, c;
  cin >> n >> m;
  vector<int> A(n), B(n, 0), C(1 << n, max_cost + 1);
  for (int i = 0; i < m; i++) {
    cin >> A[i] >> b;
    for (int j = 0; j < b; j++) {
      cin >> c;
      B[i] += (1 << (c - 1));
    }
  }
  C[0] = 0;
  for (int i = 1; i < (1 << n); i++) {
    for (int j = 0; j < m; j++)
      C[i] = min(C[i], C[i & (~B[j])] + A[j]);
  }
  int res = C[(1 << n) - 1];
  cout << (res == max_cost + 1 ? -1 : res) << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define ll long long

int main() {
  std::ios::sync_with_stdio(false);
  using namespace std;
  const int max_v = 100'000, max_n = 12, max_cost = max_v * max_n;
  int n, m, a, b, c;
  cin >> n >> m;
  vector<int> A(m), B(m, 0), C(1 << n, max_cost + 1);
  for (int i = 0; i < m; i++) {
    cin >> A[i] >> b;
    for (int j = 0; j < b; j++) {
      cin >> c;
      B[i] += (1 << (c - 1));
    }
  }
  C[0] = 0;
  for (int i = 1; i < (1 << n); i++) {
    for (int j = 0; j < m; j++)
      C[i] = min(C[i], C[i & (~B[j])] + A[j]);
  }
  int res = C[(1 << n) - 1];
  cout << (res == max_cost + 1 ? -1 : res) << "\n";
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// from https://atcoder.jp/contests/abc142/submissions/7731316

typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int n, m;
int a[1050], b[1050], c[1050][20];

void input() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
    for (int j = 1; j <= b[i]; j++)
      cin >> c[i][j];
  }
}

void solve() {
  input();
  const int inf = 1e9;
  vector<int> dp(5000, inf);
  dp[0] = 0;
  for (int item = 1; item <= m; item++) {
    int now = 0;
    for (int j = 0; j <= b[item]; j++) {
      now |= (1 << (c[item][j] - 1));
    }
    for (int bits = 0; bits < (1 << n); bits++) {
      chmin(dp[bits | now], dp[bits] + a[item]);
    }
  }
  if (dp[(1 << n) - 1] == inf)
    dp[(1 << n) - 1] = -1;
  cout << dp[(1 << n) - 1] << endl;
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// from https://atcoder.jp/contests/abc142/submissions/7731316

typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int n, m;
int a[1050], b[1050], c[1050][20];

void input() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
    for (int j = 1; j <= b[i]; j++)
      cin >> c[i][j];
  }
}

void solve() {
  input();
  const int inf = 1e9;
  vector<int> dp(5000, inf);
  dp[0] = 0;
  for (int item = 1; item <= m; item++) {
    int now = 0;
    for (int j = 1; j <= b[item]; j++) {
      now |= (1 << (c[item][j] - 1));
    }
    for (int bits = 0; bits < (1 << n); bits++) {
      chmin(dp[bits | now], dp[bits] + a[item]);
    }
  }
  if (dp[(1 << n) - 1] == inf)
    dp[(1 << n) - 1] = -1;
  cout << dp[(1 << n) - 1] << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int n, m;
int dp[1010][1 << 12];
int v[1010];
int val[1010];

int f(int pos, int mask) {
  if (pos == m) {
    if (mask == (1 << n) - 1)
      return 0;
    return 1e9;
  }
  int &ans = dp[pos][mask];
  ans = 1e9;
  ans = min(ans, f(pos + 1, mask | v[pos]) + val[pos]);
  ans = min(ans, f(pos + 1, mask));
  return ans;
}

int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    val[i] = a;
    for (int j = 0; j < b; ++j) {
      int x;
      scanf("%d", &x);
      x--;
      v[i] |= (1 << x);
    }
  }
  int ans = f(0, 0);
  if (ans == 1e9)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n, m;
int dp[1010][1 << 12];
int v[1010];
int val[1010];

int f(int pos, int mask) {
  if (pos == m) {
    if (mask == (1 << n) - 1)
      return 0;
    return 1e9;
  }
  int &ans = dp[pos][mask];
  if (ans != -1)
    return ans;
  ans = 1e9;
  ans = min(ans, f(pos + 1, mask | v[pos]) + val[pos]);
  ans = min(ans, f(pos + 1, mask));
  return ans;
}

int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    val[i] = a;
    for (int j = 0; j < b; ++j) {
      int x;
      scanf("%d", &x);
      x--;
      v[i] |= (1 << x);
    }
  }
  int ans = f(0, 0);
  if (ans == 1e9)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  vector<int> c(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) {
      int k;
      cin >> k;
      k--;
      c[i] ^= (1 << k);
    }
  }
  vector<int> dp(1 << n, inf);
  dp[0] = 0;
  rep(i, 1 << n) {
    if (dp[i] != inf) {
      rep(j, m) { dp[i | c[j]] = min(dp[i | c[j]], dp[i] + a[j]); }
    }
  }
  if (dp[(1 << n) - 1] == inf) {
    cout << -1 << endl;
  } else {
    cout << dp[(1 << n) - 1] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  vector<int> c(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) {
      int k;
      cin >> k;
      k--;
      c[i] ^= (1 << k);
    }
  }
  vector<int> dp(1 << n, inf);
  dp[0] = 0;
  rep(i, 1 << n) {
    if (dp[i] != inf) {
      rep(j, m) { dp[i | c[j]] = min(dp[i | c[j]], dp[i] + a[j]); }
    }
  }
  if (dp[(1 << n) - 1] == inf) {
    cout << -1 << endl;
  } else {
    cout << dp[(1 << n) - 1] << endl;
  }
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
int dp[(1 << 12) + 5][maxn], inf = 1e9;
struct Key {
  int cost, status;
};
Key key[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m, cnt, box;
  cin >> n >> m;
  int bound = (1 << n);
  for (int i = 0; i <= m; ++i)
    for (int j = 0; j < bound; ++j)
      dp[j][i] = inf;
  for (int i = 1; i <= m; ++i) {
    cin >> key[i].cost >> cnt;
    for (int j = 0; j < cnt; ++j) {
      cin >> box;
      key[i].status |= (1 << (box - 1));
    }
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j <= bound; ++j)
      if (dp[j][i - 1] != inf) {
        dp[j][i] = min(dp[j][i], dp[j][i - 1]);
        dp[j | key[i].status][i] =
            min(dp[j | key[i].status][i], dp[j][i - 1] + key[i].cost);
      }
    dp[key[i].status][i] = min(dp[key[i].status][i], key[i].cost);
  }
  if (dp[(1 << n) - 1][m] == inf)
    cout << "-1\n";
  else
    cout << dp[bound - 1][m] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
int dp[(1 << 13) + 5][maxn], inf = 1e9;
struct Key {
  int cost, status;
};
Key key[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m, cnt, box;
  cin >> n >> m;
  int bound = (1 << n);
  for (int i = 0; i <= m; ++i)
    for (int j = 0; j < bound; ++j)
      dp[j][i] = inf;
  for (int i = 1; i <= m; ++i) {
    cin >> key[i].cost >> cnt;
    for (int j = 0; j < cnt; ++j) {
      cin >> box;
      key[i].status |= (1 << (box - 1));
    }
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j <= bound; ++j)
      if (dp[j][i - 1] != inf) {
        dp[j][i] = min(dp[j][i], dp[j][i - 1]);
        dp[j | key[i].status][i] =
            min(dp[j | key[i].status][i], dp[j][i - 1] + key[i].cost);
      }
    dp[key[i].status][i] = min(dp[key[i].status][i], key[i].cost);
  }
  if (dp[(1 << n) - 1][m] == inf)
    cout << "-1\n";
  else
    cout << dp[bound - 1][m] << '\n';
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

int main() {
  int n, m;
  cin >> n >> m;
  vec a(m), b(m);
  vec c[20] = {};
  rep(i, m) {
    cin >> a[i] >> b[i];
    c[i].resize(b[i]);
    rep(j, b[i]) {
      int x;
      cin >> x;
      x--;
      c[i][j] = x;
    }
  }

  vec key(m);
  rep(i, m) {
    int k = 0;

    rep(j, b[i]) k |= (1 << c[i][j]);

    key[i] = k;
  }

  bool f = true;
  int key_f = 0;
  rep(i, m) { key_f = key_f | key[i]; }
  rep(i, n) if (!((key_f >> i) & 1)) f = false;

  if (!f) {
    cout << -1 << endl;
    exit(0);
  }

  int dp[8000];
  rep(i, 8000) dp[i] = INF;
  dp[0] = 0;

  int mx = 0;
  rep(i, n) mx |= (1 << i);

  rep(i, m) {
    rep(flag, 1 << n) {
      dp[flag | key[i]] = min(dp[flag | key[i]], dp[flag] + a[i]);
    }
  }

  cout << dp[mx] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

int main() {
  int n, m;
  cin >> n >> m;
  vec a(m), b(m);
  vec c[1050] = {};
  rep(i, m) {
    cin >> a[i] >> b[i];
    c[i].resize(b[i]);
    rep(j, b[i]) {
      int x;
      cin >> x;
      x--;
      c[i][j] = x;
    }
  }

  vec key(m);
  rep(i, m) {
    int k = 0;

    rep(j, b[i]) k |= (1 << c[i][j]);

    key[i] = k;
  }

  bool f = true;
  int key_f = 0;
  rep(i, m) { key_f = key_f | key[i]; }
  rep(i, n) if (!((key_f >> i) & 1)) f = false;

  if (!f) {
    cout << -1 << endl;
    exit(0);
  }

  int dp[8000];
  rep(i, 8000) dp[i] = INF;
  dp[0] = 0;

  int mx = 0;
  rep(i, n) mx |= (1 << i);

  rep(i, m) {
    rep(flag, 1 << n) {
      dp[flag | key[i]] = min(dp[flag | key[i]], dp[flag] + a[i]);
    }
  }

  cout << dp[mx] << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <tuple>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int s = 1 << 12;
int n, m, a[20], b[20], c[20], bin[20];
int dp[s];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", a + i, b + i);
    for (int j = 0; j < b[i]; j++)
      scanf("%d", c + j), bin[i] ^= 1 << (c[j] - 1);
  }
  for (int i = 0; i < s; i++)
    dp[i] = 1e9;
  dp[0] = 0;

  for (int i = 0; i < m; i++)
    for (int j = 0; j < s; j++)
      dp[j | bin[i]] = min(dp[j | bin[i]], dp[j] + a[i]);
  if (dp[(1 << n) - 1] == ((int)1e9))
    printf("-1\n");
  else
    printf("%d\n", dp[(1 << n) - 1]);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <tuple>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int s = 1 << 12;
int n, m, a[2010], b[2010], c[20], bin[2010];
int dp[s];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", a + i, b + i);
    for (int j = 0; j < b[i]; j++)
      scanf("%d", c + j), bin[i] ^= 1 << (c[j] - 1);
  }
  for (int i = 0; i < s; i++)
    dp[i] = 1e9;
  dp[0] = 0;

  for (int i = 0; i < m; i++)
    for (int j = 0; j < s; j++)
      dp[j | bin[i]] = min(dp[j | bin[i]], dp[j] + a[i]);
  if (dp[(1 << n) - 1] == ((int)1e9))
    printf("-1\n");
  else
    printf("%d\n", dp[(1 << n) - 1]);
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using ll = long long;

constexpr int N = 12, INF = numeric_limits<int>::max() / 2;
int n, m, a[N], b[N], unlock[N], dp[1 << N];
int go(int msk) {
  // msk shows not yet unlocked locks
  if (dp[msk] != -1)
    return dp[msk];
  dp[msk] = INF;
  for (int i = 0; i < m; ++i) {
    int un = unlock[i] & msk;
    if (!un)
      continue;
    dp[msk] = min(dp[msk], a[i] + go(msk ^ un));
  }
  return dp[msk];
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    for (int j = 0, x; j < b[i]; ++j) {
      cin >> x;
      --x;
      unlock[i] |= 1 << x;
    }
  }
  fill(dp, dp + (1 << N), -1);
  dp[0] = 0;
  int ans = go((1 << n) - 1);
  cout << (ans == INF ? -1 : ans) << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using ll = long long;

constexpr int N = 12, M = 1009, INF = numeric_limits<int>::max() / 2;
int n, m, a[M], b[M], unlock[M], dp[1 << N];
int go(int msk) {
  // msk shows not yet unlocked locks
  if (dp[msk] != -1)
    return dp[msk];
  dp[msk] = INF;
  for (int i = 0; i < m; ++i) {
    int un = unlock[i] & msk;
    if (!un)
      continue;
    dp[msk] = min(dp[msk], a[i] + go(msk ^ un));
  }
  return dp[msk];
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    for (int j = 0, x; j < b[i]; ++j) {
      cin >> x;
      --x;
      unlock[i] |= 1 << x;
    }
  }
  fill(dp, dp + (1 << N), -1);
  dp[0] = 0;
  int ans = go((1 << n) - 1);
  cout << (ans == INF ? -1 : ans) << '\n';
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define endl '\n'
const double INF = 1e9 + 5;

pair<int, int> key[13]; // bitmask,value;
int d[1001][1 << 13];
int n, m;
int go(int here, int state) {
  if (state == (1 << n) - 1)
    return 0;
  if (here == m) // 실수했다.
    return INF;

  int &ret = d[here][state];
  if (ret != -1)
    return ret;
  ret = INF;

  ret = min(ret, go(here + 1, state | key[here].first) + key[here].second);
  ret = min(ret, go(here + 1, state));
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // memset(d, -1, sizeof(d));
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < (1 << 13); j++)
      d[i][j] = -1;

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;

    int total = 0;
    for (int j = 0; j < b; j++) {
      int ci;
      cin >> ci;
      ci--;
      total |= (1 << ci);
    }
    key[i].first = total;
    key[i].second = a;
  }
  int val = go(0, 0);

  if (val == INF)
    cout << -1;
  else
    cout << val;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define endl '\n'
const double INF = 1e9 + 5;

pair<int, int> key[1001]; // bitmask,value;
int d[1001][1 << 13];
int n, m;
int go(int here, int state) {
  if (state == (1 << n) - 1)
    return 0;
  if (here == m) // 실수했다.
    return INF;

  int &ret = d[here][state];
  if (ret != -1)
    return ret;
  ret = INF;

  ret = min(ret, go(here + 1, state | key[here].first) + key[here].second);
  ret = min(ret, go(here + 1, state));
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // memset(d, -1, sizeof(d));
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < (1 << 13); j++)
      d[i][j] = -1;

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;

    int total = 0;
    for (int j = 0; j < b; j++) {
      int ci;
      cin >> ci;
      ci--;
      total |= (1 << ci);
    }
    key[i].first = total;
    key[i].second = a;
  }
  int val = go(0, 0);

  if (val == INF)
    cout << -1;
  else
    cout << val;
}
Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

int n, m;
int a[1010], b[1010], c[1010][15];
int dp[1010][1 << 13];
bool vis[1010][1 << 13];

int solve(int p, int bit) {
  if (vis[p][bit])
    return dp[p][bit];
  if (p == m) {
    if (bit == (1 << n) - 1)
      return dp[p][bit] = 0;
    else
      return dp[p][bit] = INF;
  }
  int res = solve(p + 1, bit);
  int nxt = bit;
  rep(i, b[p]) nxt |= (1 << c[p][i]);
  if (solve(p + 1, nxt) != INF)
    res = min(res, solve(p + 1, nxt) + a[p]);
  return dp[p][bit] = res;
}

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

  cin >> n >> m;
  rep(i, m) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) cin >> c[i][j];
    rep(j, b[i]) c[i][j]--;
  }
  int ans = solve(0, 0);
  cout << (ans == INF ? -1 : ans) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

int n, m;
int a[1010], b[1010], c[1010][15];
int dp[1010][1 << 13];
bool vis[1010][1 << 13];

int solve(int p, int bit) {
  if (vis[p][bit])
    return dp[p][bit];
  vis[p][bit] = true;
  if (p == m) {
    if (bit == (1 << n) - 1)
      return dp[p][bit] = 0;
    else
      return dp[p][bit] = INF;
  }
  int res = solve(p + 1, bit);
  int nxt = bit;
  rep(i, b[p]) nxt |= (1 << c[p][i]);
  if (solve(p + 1, nxt) != INF)
    res = min(res, solve(p + 1, nxt) + a[p]);
  return dp[p][bit] = res;
}

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

  cin >> n >> m;
  rep(i, m) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) cin >> c[i][j];
    rep(j, b[i]) c[i][j]--;
  }
  int ans = solve(0, 0);
  cout << (ans == INF ? -1 : ans) << endl;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, m, a[1005], b[1005], p[13];
  ll cnt = 1;
  for (ll i = 0; i < 13; i++) {
    p[i] = cnt;
    cnt *= 2;
  }
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    a[i] = x;
    ll now = 0;
    for (ll j = 0; j < y; j++) {
      ll next;
      cin >> next;
      next--;
      now = now | p[next];
    }
    b[i] = now;
    //		cout <<now<<endl;
  }
  ll dp[100000];
  for (ll i = 0; i < 100000; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (ll i = 0; i < 100000; i++) {
    for (ll j = 0; j < m; j++) {
      ll cost = a[j];
      ll op = b[j];
      ll next = i | op;
      dp[next] = min(dp[next], dp[i] + cost);
    }
  }
  //	cout << ll(pow(2,n))-1;
  if (dp[ll(pow(2, n)) - 1] == INF) {
    cout << -1;
    return 0;
  }
  cout << dp[ll(pow(2, n)) - 1];
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, m, a[1005], b[1005], p[13];
  ll cnt = 1;
  for (ll i = 0; i < 13; i++) {
    p[i] = cnt;
    cnt *= 2;
  }
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    a[i] = x;
    ll now = 0;
    for (ll j = 0; j < y; j++) {
      ll next;
      cin >> next;
      next--;
      now = now | p[next];
    }
    b[i] = now;
    //		cout <<now<<endl;
  }
  ll dp[100000];
  for (ll i = 0; i < 100000; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (ll i = 0; i < 100000; i++) {
    for (ll j = 0; j < m; j++) {
      ll cost = a[j];
      ll op = b[j];
      ll next = i | op;
      if (next < 100000) {
        dp[next] = min(dp[next], dp[i] + cost);
      }
    }
  }
  //	cout << ll(pow(2,n))-1;
  if (dp[ll(pow(2, n)) - 1] == INF) {
    cout << -1;
    return 0;
  }
  cout << dp[ll(pow(2, n)) - 1];
  // your code goes here
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll dp[4200];
int main() {
  for (ll i = 0; i < 4200; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    ll op = 0;
    for (ll j = 0; j < y; j++) {
      ll now;
      cin >> now;
      now--;
      now = pow(2, now);
      op = op | now;
    }
    for (ll j = 0; j < 4200; j++) {
      dp[j | op] = min(dp[j] + x, dp[j | op]);
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans = ans | ll(pow(2, i));
  }
  //	cout <<ans<<endl;
  if (dp[ans] == INF) {
    cout << -1;
    return 0;
  }
  cout << dp[ans];
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll dp[4200];
int main() {
  for (ll i = 0; i < 4200; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    ll op = 0;
    for (ll j = 0; j < y; j++) {
      ll now;
      cin >> now;
      now--;
      now = pow(2, now);
      op = op | now;
    }
    for (ll j = 0; j < 4200; j++) {
      if (ll(j | op) < 4200) {
        dp[j | op] = min(dp[j] + x, dp[j | op]);
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans = ans | ll(pow(2, i));
  }
  //	cout <<ans<<endl;
  if (dp[ans] == INF) {
    cout << -1;
    return 0;
  }
  cout << dp[ans];
  // your code goes here
  return 0;
}
Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000
#define P pair<int, int>
#define pb push_back
#define fi first
#define se second
int n, m, st, d[1010];
vector<int> v[1010], vg[1010], ans;
P es[1010];
int mid = INF, mi;
queue<int> q;
void dfs(int x) {
  if (d[x] == 0)
    return;
  for (int i : vg[x]) {
    if (d[i] == d[x] - 1) {
      ans.pb(i);
      dfs(i);
      break;
    }
  }
}
int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].pb(b);
    vg[b].pb(a);
    es[i] = {b, a};
  }
  rep(i, m) {
    P e = es[i];
    fill(d, d + n, INF);
    d[e.fi] = 0;
    q.push(e.fi);
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (int j : v[p]) {
        if (d[j] > d[p] + 1) {
          d[j] = d[p] + 1;
          q.push(j);
        }
      }
    }
    if (mid > d[e.se]) {
      mid = d[e.se];
      mi = i;
    }
  }
  if (mid == INF) {
    puts("-1");
    return 0;
  }
  P e = es[mi];
  fill(d, d + n, INF);
  d[e.fi] = 0;
  q.push(e.fi);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int j : v[p]) {
      if (d[j] > d[p] + 1) {
        d[j] = d[p] + 1;
        q.push(j);
      }
    }
  }
  ans.pb(e.se);
  dfs(e.se);
  cout << ans.size() << endl;
  for (int i : ans) {
    cout << i + 1 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000
#define P pair<int, int>
#define pb push_back
#define fi first
#define se second
int n, m, st, d[1010];
vector<int> v[1010], vg[1010], ans;
P es[2010];
int mid = INF, mi;
queue<int> q;
void dfs(int x) {
  if (d[x] == 0)
    return;
  for (int i : vg[x]) {
    if (d[i] == d[x] - 1) {
      ans.pb(i);
      dfs(i);
      break;
    }
  }
}
int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].pb(b);
    vg[b].pb(a);
    es[i] = {b, a};
  }
  rep(i, m) {
    P e = es[i];
    fill(d, d + n, INF);
    d[e.fi] = 0;
    q.push(e.fi);
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (int j : v[p]) {
        if (d[j] > d[p] + 1) {
          d[j] = d[p] + 1;
          q.push(j);
        }
      }
    }
    if (mid > d[e.se]) {
      mid = d[e.se];
      mi = i;
    }
  }
  if (mid == INF) {
    puts("-1");
    return 0;
  }
  P e = es[mi];
  fill(d, d + n, INF);
  d[e.fi] = 0;
  q.push(e.fi);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int j : v[p]) {
      if (d[j] > d[p] + 1) {
        d[j] = d[p] + 1;
        q.push(j);
      }
    }
  }
  ans.pb(e.se);
  dfs(e.se);
  cout << ans.size() << endl;
  for (int i : ans) {
    cout << i + 1 << endl;
  }
  return 0;
}
Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using i64 = int64_t;

struct Edge {
  int from, to;
};

struct Node {
  int num;
  vector<int> visited;
};

using Graph = vector<vector<Edge>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph graph(n + 1, vector<Edge>());
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back({a, b});
  }

  vector<int> ans;
  for (int i = 1; i <= n; ++i) {
    queue<Node> que;
    deque<bool> visited(n + 1);
    for (auto e : graph[i]) {
      que.push({e.to, vector<int>({e.to})});
      visited[e.to] = true;
    }
    while (!que.empty()) {
      auto cur = que.front();
      que.pop();
      if (cur.num == i) {
        if (ans.size() == 0 || cur.visited.size() < ans.size()) {
          ans = cur.visited;
        }
        break;
      }
      for (auto e : graph[cur.num]) {
        if (visited[e.to])
          continue;
        auto v = cur.visited;
        v.push_back(e.to);
        que.push({e.to, v});
      }
    }
  }

  if (ans.size() == 0)
    cout << -1 << endl;
  else {
    cout << ans.size() << endl;
    for (auto e : ans) {
      cout << e << endl;
    }
  }

  return 0;
}


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

using i64 = int64_t;

struct Edge {
  int from, to;
};

struct Node {
  int num;
  vector<int> visited;
};

using Graph = vector<vector<Edge>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph graph(n + 1, vector<Edge>());
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back({a, b});
  }

  vector<int> ans;
  for (int i = 1; i <= n; ++i) {
    queue<Node> que;
    deque<bool> visited(n + 1);
    for (auto e : graph[i]) {
      que.push({e.to, vector<int>({e.to})});
      visited[e.to] = true;
    }
    while (!que.empty()) {
      auto cur = que.front();
      que.pop();
      if (cur.num == i) {
        if (ans.size() == 0 || cur.visited.size() < ans.size()) {
          ans = cur.visited;
        }
        break;
      }
      for (auto e : graph[cur.num]) {
        if (visited[e.to])
          continue;
        auto v = cur.visited;
        v.push_back(e.to);
        que.push({e.to, v});
        visited[e.to] = true;
      }
    }
  }

  if (ans.size() == 0)
    cout << -1 << endl;
  else {
    cout << ans.size() << endl;
    for (auto e : ans) {
      cout << e << endl;
    }
  }

  return 0;
}
Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

const int INF = 1001001001;
int n;
vector<int> to[1005];

vector<int> bfs(int sv) {
  vector<int> dist(n, INF), pre(n, -1);
  queue<int> q;
  dist[sv] = 0;
  q.push(sv);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      pre[u] = v;
      dist[u] = dist[v] + 1;
      q.push(u);
    }
  }
  pair<int, int> best(INF, -1);
  rep(v, n) {
    if (v == sv)
      continue;
    for (int u : to[v]) {
      // svに戻ってくる場合（サイクルとなっている場合）
      if (u == sv) {
        best = min(best, make_pair(dist[v], v));
      }
    }
  }
  // cout<<"Debug"<<best.first<<endl;
  if (best.first == INF)
    return vector<int>(n + 1);
  int v = best.second;
  vector<int> res;
  while (v != -1) {
    res.push_back(v);
    v = pre[v];
  }
  return res;
}

int main() {
  int m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    to[a].push_back(b);
  }

  vector<int> ans(n + 1);
  rep(i, m) {
    auto now = bfs(i);
    if (now.size() < ans.size())
      ans = now;
  }
  if (ans.size() == n + 1) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans.size() << endl;
  for (int v : ans) {
    cout << v + 1 << endl;
  }
  return 0;
}

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

const int INF = 1001001001;
int n;
vector<int> to[1005];

vector<int> bfs(int sv) {
  vector<int> dist(n, INF), pre(n, -1);
  queue<int> q;
  dist[sv] = 0;
  q.push(sv);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != INF)
        continue;
      pre[u] = v;
      dist[u] = dist[v] + 1;
      q.push(u);
    }
  }
  pair<int, int> best(INF, -1);
  rep(v, n) {
    if (v == sv)
      continue;
    for (int u : to[v]) {
      // svに戻ってくる場合（サイクルとなっている場合）
      if (u == sv) {
        best = min(best, make_pair(dist[v], v));
      }
    }
  }
  // cout<<"Debug"<<best.first<<endl;
  if (best.first == INF)
    return vector<int>(n + 1);
  int v = best.second;
  vector<int> res;
  while (v != -1) {
    res.push_back(v);
    v = pre[v];
  }
  return res;
}

int main() {
  int m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    to[a].push_back(b);
  }

  vector<int> ans(n + 1);
  rep(i, n) {
    auto now = bfs(i);
    if (now.size() < ans.size())
      ans = now;
  }
  if (ans.size() == n + 1) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans.size() << endl;
  for (int v : ans) {
    cout << v + 1 << endl;
  }
  return 0;
}
Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
  }
  int len = n + 1;
  vector<int> ans;
  for (int s = 0; s < n; s++) {
    vector<int> d(n, -1);
    vector<int> pre(n, -1);
    queue<int> que;
    d[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int t = que.front();
      que.pop();
      for (auto nxt : G[t]) {
        if (d[nxt] == -1) {
          d[nxt] = d[t] + 1;
          pre[nxt] = t;
          que.push(nxt);
        }
      }
      for (int t = 0; t < n; t++) {
        if (s == t || d[t] == -1)
          continue;
        for (auto p : G[t]) {
          if (p == s) {
            vector<int> v({s});
            int now = t;
            while (now != s) {
              v.push_back(now);
              now = pre[now];
            }
            if (len > v.size()) {
              len = v.size();
              ans = v;
            }
          }
        }
      }
    }
  }
  sort(all(ans));
  if (len == n + 1)
    cout << -1 << endl;
  else {
    cout << len << endl;
    rep(i, len) cout << ans[i] + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
  }
  int len = n + 1;
  vector<int> ans;
  for (int s = 0; s < n; s++) {
    vector<int> d(n, -1);
    vector<int> pre(n, -1);
    queue<int> que;
    d[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int t = que.front();
      que.pop();
      for (auto nxt : G[t]) {
        if (d[nxt] == -1) {
          d[nxt] = d[t] + 1;
          pre[nxt] = t;
          que.push(nxt);
        }
      }
    }
    for (int t = 0; t < n; t++) {
      if (s == t || d[t] == -1)
        continue;
      for (auto p : G[t]) {
        if (p == s) {
          vector<int> v({s});
          int now = t;
          while (now != s) {
            v.push_back(now);
            now = pre[now];
          }
          if (len > v.size()) {
            len = v.size();
            ans = v;
          }
        }
      }
    }
  }
  sort(all(ans));
  if (len == n + 1)
    cout << -1 << endl;
  else {
    cout << len << endl;
    rep(i, len) cout << ans[i] + 1 << endl;
  }
}