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

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

int main() {
  int n;
  while (cin >> n) {
    vector<int> g[7];
    int a;
    set<int> se;
    for (int i = 0; i < n; ++i) {
      cin >> a;
      g[a / 10].push_back(a % 10);
      g[a % 10].push_back(a / 10);
      se.insert(a / 10);
      se.insert(a % 10);
    }
    queue<int> Q;
    Q.push(a / 10);
    bool visited[7] = {};
    int ct = 0;
    while (!Q.empty()) {
      int now = Q.front();
      Q.pop();
      if (visited[now])
        continue;
      visited[now] = 1;
      ct++;
      for (int i = 0; i < g[now].size(); ++i) {
        if (!visited[g[now][i]])
          Q.push(g[now][i]);
      }
    }
    int cnt = 0;
    for (int i = 0; i < 7; ++i) {
      if (g[i].size() % 2)
        cnt++;
    }
    //    cout << cnt << " "  << ct << endl;
    if (ct == se.size() && (cnt == 0 || cnt == 2)) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    int hoge[7] = {};
    for (int i = 0; i < n; ++i) {
      int a;
      cin >> a;
      hoge[a / 10]++;
      hoge[a % 10]++;
    }
    int cnt = 0;
    for (int i = 0; i < 7; ++i) {
      if (hoge[i] % 2)
        cnt++;
    }
    if (cnt == 0 || cnt == 2) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  }
}
Problem p00596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;
typedef pair<int, int> P;
int n;
int memo[(1 << 25)][10];

bool rec(P *G, int used, int prev) {
  if (__builtin_popcount(used) == n)
    return true;
  if (__builtin_popcount(used) >= n)
    return false;
  if (memo[used][prev] != -1)
    return memo[used][prev];

  rep(i, n) {
    if ((used >> i) & 1)
      continue;
    if (prev == G[i].first)
      if (rec(G, used | (1 << i), G[i].second)) {
        memo[used | (1 << i)][G[i].second] = true;
        return true;
      }
    if (prev == G[i].second)
      if (rec(G, used | (1 << i), G[i].first)) {
        memo[used | (1 << i)][G[i].first] = true;
        return true;
      }
  }
  memo[used][prev] = false;
  return false;
}

int main() {
  while (cin >> n) {
    cin.ignore();
    rep(i, (1 << n)) rep(j, 10) memo[i][j] = -1;
    P G[n];
    rep(i, n) {
      char a, b, c;
      scanf("%c%c%c", &a, &b, &c);
      G[i].first = a - '0';
      G[i].second = b - '0';
    }

    rep(i, n) {
      if (rec(G, (1 << i), G[i].second) || rec(G, (1 << i), G[i].first)) {
        cout << "Yes" << endl;
        goto F;
      }
    }
    cout << "No" << endl;
  F:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;
typedef pair<int, int> P;
int n;

bool rec(P *G, int used, int prev) {
  if (__builtin_popcount(used) == n)
    return true;
  if (__builtin_popcount(used) >= n)
    return false;

  rep(i, n) {
    if ((used >> i) & 1)
      continue;
    if (prev == G[i].first)
      if (rec(G, used | (1 << i), G[i].second))
        return true;
    if (prev == G[i].second)
      if (rec(G, used | (1 << i), G[i].first))
        return true;
  }
  return false;
}

int main() {
  while (cin >> n) {
    cin.ignore();
    P G[n];
    rep(i, n) {
      char a, b, c;
      scanf("%c%c%c", &a, &b, &c);
      G[i].first = a - '0';
      G[i].second = b - '0';
    }

    rep(i, n) {
      if (rec(G, (1 << i), G[i].second) || rec(G, (1 << i), G[i].first)) {
        cout << "Yes" << endl;
        goto F;
      }
    }
    cout << "No" << endl;
  F:;
  }
  return 0;
}
Problem p00596 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
typedef pair<int, int> pii;

int main() {
  ios_base::sync_with_stdio(false);
  int N;
  while (cin >> N) {
    vector<pii> p(N);
    bool found[10] = {false};
    for (int i = 0; i < N; ++i) {
      int x;
      cin >> x;
      p[i] = pii(x / 10, x % 10);
      found[p[i].first] = found[p[i].second] = true;
    }
    bool answer = false;
    vector<int> dp(1 << N);
    for (int begin = 0; !answer && begin < 10; ++begin) {
      if (!found[begin]) {
        continue;
      }
      fill(dp.begin(), dp.end(), 0);
      dp[0] |= (1 << begin);
      for (int i = 0; i < (1 << N); ++i) {
        for (int j = 0; j < 10; ++j) {
          if (!(dp[i] & (1 << j))) {
            continue;
          }
          for (int k = 0; k < N; ++k) {
            if (i & (1 << k)) {
              continue;
            }
            int m = i | (1 << k);
            if (p[k].first == j) {
              dp[m] |= (1 << p[k].second);
            }
            if (p[k].second == j) {
              dp[m] |= (1 << p[k].first);
            }
          }
        }
      }
      if (dp[(1 << N) - 1]) {
        answer = true;
      }
    }
    cout << (answer ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> pii;
const int INF = 1000000000;

int main() {
  ios_base::sync_with_stdio(false);
  int N;
  while (cin >> N) {
    vector<pii> p(N);
    for (int i = 0; i < N; ++i) {
      int x;
      cin >> x;
      p[i] = pii(x / 10, x % 10);
    }
    bool answer = false;
    for (int begin = 0; !answer && begin < 10; ++begin) {
      vector<vector<bool>> dp(1 << N, vector<bool>(10));
      dp[0][begin] = true;
      for (int i = 0; i < (1 << N); ++i) {
        for (int j = 0; j < 10; ++j) {
          if (!dp[i][j]) {
            continue;
          }
          for (int k = 0; k < N; ++k) {
            if (i & (1 << k)) {
              continue;
            }
            int m = i | (1 << k);
            if (p[k].first == j) {
              dp[m][p[k].second] = true;
            }
            if (p[k].second == j) {
              dp[m][p[k].first] = true;
            }
          }
        }
      }
      for (int i = 0; i < 10; ++i) {
        if (dp[(1 << N) - 1][i]) {
          answer = true;
        }
      }
    }
    cout << (answer ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p00596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <set>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

char d[30][3];
int n;

set<int> ss[7];

bool solve(int cnt, char prev, int f) {
  if (cnt == n)
    return true;
  if (ss[prev - '0'].count(f))
    return false;

  ss[prev - '0'].insert(f);

  REP(i, n) if (f & (1 << i)) {
    if (prev == d[i][0]) {
      if (solve(cnt + 1, d[i][1], f ^ (1 << i)))
        return true;
    } else if (prev == d[i][1]) {
      if (solve(cnt + 1, d[i][0], f ^ (1 << i)))
        return true;
    }
  }
  return false;
}

int main() {
  while (~scanf("%d", &n)) {
    REP(i, n) scanf("%s", d[i]);

    bool ans = false;
    REP(i, 7) ss[i].clear();

    REP(i, 7) if (solve(0, i + '0', (1 << n) - 1)) {
      ans = true;
      break;
    }

    puts(ans ? "Yes" : "No");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

char d[30][3];
int n;
bool f[30];

bool solve(int cnt, char prev) {
  if (cnt == n)
    return true;
  REP(i, n) if (!f[i]) {
    f[i] = true;
    if (prev == d[i][0]) {
      if (solve(cnt + 1, d[i][1]))
        return true;
    } else if (prev == d[i][1]) {
      if (solve(cnt + 1, d[i][0]))
        return true;
    }
    f[i] = false;
  }
  return false;
}

int main() {
  while (~scanf("%d", &n)) {
    REP(i, n) scanf("%s", d[i]);

    bool ans = false;

    REP(i, 7) if (solve(0, i + '0')) {
      ans = true;
      break;
    }

    puts(ans ? "Yes" : "No");
  }
  return 0;
}
Problem p00597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int out[40] = {1, 2, 5, 8};
  loop(i, 4, 40) out[i] = out[i - 2] + (out[i - 2] - out[i - 4]) * 3;
  int n;
  while (cin >> n)
    cout << out[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int out[40] = {1, 2, 5, 8};
  loop(i, 4, 40) out[i] = out[i - 2] + (out[i - 2] - out[i - 4]) * 3;
  int n;
  while (cin >> n, n)
    cout << out[n - 1] << endl;
}
Problem p00597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

int pow(int x, int y) {
  int r = x;

  for (int i = 1; i < y; i++)
    r *= x;

  return r;
}

int main() {
  int n;
  int dp[31];

  memset(dp, 0, sizeof(dp));
  dp[1] = 1;
  dp[2] = 2;
  for (int i = 3; i <= 30; i++) {
    if (i % 2 == 1)
      dp[i] = dp[i - 1] + pow(3, (i - 1) / 2);
    else if (i % 2 == 0)
      dp[i] = dp[i - 2] + 2 * pow(3, (i - 2) / 2);
  }

  while (~scanf("%d", &n)) {
    std::cout << dp[n] << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

int main() {
  int n;
  int dp[31];

  memset(dp, 0, sizeof(dp));
  dp[1] = 1;
  dp[2] = 2;
  for (int i = 3; i <= 30; i++) {
    if (i % 2 == 1)
      dp[i] = dp[i - 1] + i;
    if (i % 2 == 0)
      dp[i] = dp[i - 1] + i - 1;
  }

  while (scanf("%d", &n)) {
    cout << dp[n] << endl;
  }

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

#include <iostream>
using namespace std;
int n, f[33];
int main() {
  f[1] = 1, f[2] = 2, f[3] = 5;
  for (int i = 4; i <= 30; i++) {
    if (i & 1) {
      f[i] = 4;
      for (int j = 1; j < i / 2; j++)
        f[i] *= 3;
      f[i] += f[i - 2];
    } else {
      f[i] = 1;
      for (int j = 1; j < i / 2; j++)
        f[i] *= 3;
      f[i] += f[i - 1];
    }
  }
  while (cin >> n) {
    cout << f[n] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int n, f[33];
int main() {
  f[1] = 1, f[2] = 2, f[3] = 5;
  for (int i = 4; i <= 30; i++) {
    if (i & 1) {
      f[i] = 4;
      for (int j = 1; j < i / 2; j++)
        f[i] *= 3;
      f[i] += f[i - 2];
    } else {
      f[i] = 1;
      for (int j = 1; j < i / 2; i++)
        f[i] *= 3;
      f[i] += f[i - 1];
    }
  }
  while (cin >> n) {
    cout << f[n] << endl;
  }
  return 0;
}
Problem p00600 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <cstdio>
#include <functional>
#include <map>
#include <queue>
using namespace std;
int s, d, a[100][100];
int F() {
  bool f[100] = {0};
  int r = 0, i;
  typedef pair<int, int> P;
  P p;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 0));
  while (!q.empty()) {
    p = q.top();
    q.pop();
    if (f[p.second])
      continue;
    f[p.second] = 1;
    r += p.first;
    for (i = 0; i < s + d; ++i)
      if (a[p.second][i] != INT_MAX)
        q.push(P(a[p.second][i], i));
  }
  return r;
}
int main() {
  int i, j, x;
  while (scanf("%d%d", &s, &d), s) {
    for (i = 0; i < s; ++i) {
      for (j = 0; j < d; ++j)
        scanf("%d", &x), a[i][s + j] = a[s + j][i] = (x > 0 ? x : INT_MAX);
      for (j = 0; j < s; ++j)
        a[i][j] = 0, a[i][i] = INT_MAX;
    }
    for (i = 0; i < d; ++i)
      for (j = 0; j < d; ++j)
        a[i + s][j + s] = INT_MAX;
    for (i = 0; i < d - 1; ++i)
      for (j = 0; j < d - i - 1; ++j)
        scanf("%d", &x), a[s + i + j + 1][s + i] = a[s + i][s + i + j + 1] =
                             (x > 0 ? x : INT_MAX);
    printf("%d\\n", F());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <climits>
#include <cstdio>
#include <functional>
#include <map>
#include <queue>
using namespace std;
int s, d, a[50][50];
int F() {
  bool f[50] = {0};
  int r = 0, i;
  typedef pair<int, int> P;
  P p;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 0));
  while (!q.empty()) {
    p = q.top();
    q.pop();
    if (f[p.second])
      continue;
    f[p.second] = 1;
    r += p.first;
    for (i = 0; i < s + d; ++i)
      if (a[p.second][i] != INT_MAX)
        q.push(P(a[p.second][i], i));
  }
  return r;
}
int main() {
  int i, j, x;
  while (scanf("%d%d", &s, &d), s) {
    for (i = 0; i < s; ++i) {
      for (j = 0; j < d; ++j)
        scanf("%d", &x), a[i][s + j] = a[s + j][i] = (x > 0 ? x : INT_MAX);
      for (j = 0; j < s; ++j)
        a[i][j] = 0, a[i][i] = INT_MAX;
    }
    for (i = 0; i < d; ++i)
      for (j = 0; j < d; ++j)
        a[i + s][j + s] = INT_MAX;
    for (i = 0; i < d - 1; ++i)
      for (j = 0; j < d - i - 1; ++j)
        scanf("%d", &x), a[s + i + j + 1][s + i] = a[s + i][s + i + j + 1] =
                             (x > 0 ? x : INT_MAX);
    printf("%d\\n", F());
  }
  return 0;
}
Problem p00600 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 1 << 27;
const int MAX = 51;

int dis_d[MAX][MAX], in, s, d, G[MAX];
int min_d[MAX];
bool used[MAX];

int prim() {
  for (int i = 0; i < d; i++) {
    min_d[i] = INF;
    used[i] = false;
  }

  min_d[0] = 0;
  int res = 0;

  while (true) {
    int v = -1;

    for (int u = 0; u < d; u++) {
      if (!used[u] && (v == -1 || min_d[u] < min_d[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += min_d[v];

    for (int u = 0; u < d; u++) {
      min_d[u] = min(min_d[u], dis_d[v][u]);
    }
  }

  return res;
}

int main() {
  while (cin >> s >> d, s + d) {
    fill(G, G + MAX, INF);

    for (int i = 0; i < s; i++) {
      for (int j = 0; j < d; j++) {
        cin >> in;
        if (!in)
          continue;
        G[j] = min(G[j], in);
      }
    }
    fill(dis_d[0], dis_d[MAX], INF);

    for (int i = 0; i < d; i++) {
      dis_d[i][d] = dis_d[d][i] = G[i];
    }

    for (int i = 0; i < d - 1; i++) {
      for (int j = i + 1; j < d; j++) {
        cin >> in;
        if (!in)
          continue;
        dis_d[i][j] = dis_d[j][i] = in;
      }
    }
    d++;

    cout << prim() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 1 << 29;
const int MAX = 51;

int dis_d[MAX][MAX], in, s, d, G[MAX][MAX], ans;
int min_d[MAX];
bool used[MAX];

void prim() {
  for (int i = 0; i < d; i++) {
    min_d[i] = INF;
    used[i] = false;
  }

  min_d[0] = 0;
  int res = 0;

  while (true) {
    int v = -1;

    for (int u = 0; u < d; u++) {
      if (!used[u] && (v == -1 || min_d[u] < min_d[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += min_d[v];

    for (int u = 0; u < d; u++) {
      if (u - v < s)
        min_d[u] = min(min_d[u], G[u - v][v] + dis_d[v][u]);
      else
        min_d[u] = min(min_d[u], dis_d[v][u]);
    }
  }
  ans = res - 1;
}

int main() {
  while (cin >> s >> d, s + d) {
    memset(G, 0, sizeof(G));

    for (int i = 0; i < s; i++) {
      for (int j = 0; j < d; j++) {
        cin >> G[i][j];
      }
    }

    memset(dis_d, 0, sizeof(dis_d));
    for (int i = 0; i < d - 1; i++) {
      for (int j = i + 1; j < d; j++) {
        cin >> in;
        dis_d[i][j] = dis_d[j][i] = in;
      }
    }
    prim();

    cout << ans << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int N = 30;
int g[N];
int n;
int ans;
void dfs(int now, int cover, int cost) {
  if (ans <= cost)
    return;
  if (cover == (1 << n) - 1) {
    ans = cost;
    return;
  }
  if (now == n)
    return;
  dfs(now + 1, cover | g[now], cost + 1);
  dfs(now + 1, cover, cost);
}

int main() {
  int m;
  while (cin >> n >> m, n || m) {
    REP(i, n) g[i] = 1 << i;
    REP(i, m) {
      int x, y;
      cin >> x >> y;
      g[x] |= 1 << y;
      g[y] |= 1 << x;
    }
    ans = n;
    dfs(0, 0, 0);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int N = 30;
int g[N];
int n;

int dfs(int now, int cover, int cost) {
  if (now == n)
    return (cover == (1 << n) - 1) ? cost : INF;
  return min(dfs(now + 1, cover | g[now], cost + 1), dfs(now + 1, cover, cost));
}

int main() {
  int m;
  while (cin >> n >> m, n || m) {
    memset(g, 0, sizeof(g));
    REP(i, n) g[i] |= 1 << i;
    REP(i, m) {
      int x, y;
      cin >> x >> y;
      g[x] |= 1 << y;
      g[y] |= 1 << x;
    }
    cout << dfs(0, 0, 0) << endl;
  }
}
Problem p00601 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
vector<bitset<30>> neighbor;

int solve(int i, bitset<30> bs) {
  if (i == n) {
    if (bs.to_ulong() == (1 << n) - 1)
      return 0;
    else
      return INT_MAX / 2;
  }

  int ret = solve(i + 1, bs);
  if ((~bs & neighbor[i]).any())
    ret = min(ret, solve(i + 1, bs | neighbor[i]) + 1);

  return ret;
}

int main() {
  for (;;) {
    int m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    neighbor.assign(n, 0);
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      neighbor[a][b] = true;
      neighbor[b][a] = true;
    }
    for (int i = 0; i < n; ++i)
      neighbor[i][i] = true;

    cout << solve(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
vector<bitset<30>> neighbor;

int solve(int i, bitset<30> bs) {
  if (i == n) {
    if (bs.to_ulong() == (1 << n) - 1)
      return 0;
    else
      return INT_MAX / 2;
  }

  int ret = solve(i + 1, bs | neighbor[i]) + 1;
  ret = min(ret, solve(i + 1, bs));

  return ret;
}

int main() {
  for (;;) {
    int m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    neighbor.assign(n, 0);
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      neighbor[a][b] = true;
      neighbor[b][a] = true;
    }
    for (int i = 0; i < n; ++i)
      neighbor[i][i] = true;

    cout << solve(0, 0) << endl;
  }
}
Problem p00601 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
const int MAX_V = 10001;
int n, m;
vector<int> G[MAX_V];
bool used[MAX_V];
int mark[MAX_V];
int res;

void dfs(int s, int cnt) {
  if (cnt >= res)
    return;
  if (s == n) {
    bool ok = true;
    // 全てがmarkされているか
    for (int i = 0; i < n; i++) {
      if (!mark[i]) {
        ok = false;
        break;
      }
    }
    if (ok)
      res = min(res, cnt);
  } else {
    dfs(s + 1, cnt);
    // markする
    mark[s]++;
    for (int j = 0; j < G[s].size(); j++) {
      int to = G[s][j];
      mark[to]++;
    }
    dfs(s + 1, cnt + 1);
    mark[s]--;
    for (int j = 0; j < G[s].size(); j++) {
      int to = G[s][j];
      mark[to]--;
    }
  }
}

int main() {
  while (cin >> n >> m && (n | m)) {
    for (int i = 0; i < n; i++)
      G[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    res = 1000000000;
    memset(used, 0, sizeof(used));
    memset(mark, 0, sizeof(mark));
    dfs(0, 0);
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;
const int MAX_V = 10001;
int n, m;
vector<int> G[MAX_V];
bool used[MAX_V];
int mark[MAX_V];
int res;

void dfs(int s, int cnt) {
  if (s == n) {
    bool ok = true;
    // 全てがmarkされているか
    for (int i = 0; i < n; i++) {
      if (!mark[i]) {
        ok = false;
        break;
      }
    }
    if (ok)
      res = min(res, cnt);
  } else {
    dfs(s + 1, cnt);
    // markする
    mark[s]++;
    for (int j = 0; j < G[s].size(); j++) {
      int to = G[s][j];
      mark[to]++;
    }
    dfs(s + 1, cnt + 1);
    mark[s]--;
    for (int j = 0; j < G[s].size(); j++) {
      int to = G[s][j];
      mark[to]--;
    }
  }
}

int main() {
  while (cin >> n >> m && (n | m)) {
    for (int i = 0; i < n; i++)
      G[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    res = 1000000000;
    memset(used, 0, sizeof(used));
    memset(mark, 0, sizeof(mark));
    dfs(0, 0);
    cout << res << endl;
  }

  return 0;
}