Problem p00601 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n, m, ans;
vector<vi> e;
bool u[100];

void rec(int c, int use) {
  if (use >= ans)
    return;

  int p = 0;
  for (; p < n; p++) {
    if (u[p])
      continue;
    bool cand = 0;
    fr(i, e[p]) {
      if (u[*i])
        goto NEXT;
      if (*i >= c)
        cand = 1;
    }
    if (!cand)
      return;
    else
      break;
  NEXT:;
  }
  if (p == n) {
    ans = min(ans, use);
    return;
  }
  if (c >= n)
    return;

  rec(c + 1, use);
  u[c] = 1;
  rec(c + 1, use + 1);
  u[c] = 0;
}
int main() {
  while (scanf("%d%d", &n, &m), n) {
    assert(n < 100 && m < 5000);
    e.clear();
    e.resize(n);
    rep(i, n) u[i] = 0;

    rep(i, m) {
      int x, y;
      scanf("%d%d", &x, &y);
      e[x].pb(y);
      e[y].pb(x);
    }
    ans = n;
    rec(0, 0);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n, m;
pi e[5000];

int ans, deg[100];
bool u[100];

void rec(int c, int use) {
  if (use >= ans)
    return;

  int p = 0;
  for (; p < m; p++) {
    if (!u[e[p].first] && !u[e[p].second]) {
      // if(e[p].second<c)return;
      break;
    }
  }
  if (p == m) {
    ans = min(ans, use);
    return;
  }
  if (c >= n)
    return;

  rec(c + 1, use);
  u[c] = 1;
  rec(c + 1, use + 1);
  u[c] = 0;
}
int main() {
  while (scanf("%d%d", &n, &m), n) {
    assert(n < 100 && m < 5000);
    rep(i, n) u[i] = deg[i] = 0;

    rep(i, m) {
      int x, y;
      scanf("%d%d", &x, &y);
      if (x > y)
        swap(x, y);
      e[i] = mp(x, y);
      deg[x]++;
      deg[y]++;
    }
    sort(e, e + m);

    int iso = 0;
    rep(i, n) if (!deg[i]) iso++;
    ans = n;
    rec(0, 0);
    printf("%d\\n", ans + iso);
  }
  return 0;
}