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 <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef vector<int> Node;
typedef vector<Node> Graph;
int min_ans;
int N, M;
Graph G;
int dfs(int k, int cur, vector<bool> adj) {
  if (min_ans <= cur)
    return INF;
  if (k == N) {
    REP(i, N) if (!adj[i]) return INF;
    return min_ans = cur;
  }
  int res = INF;
  res = min(res, dfs(k + 1, cur, adj));
  REP(i, G[k].size()) {
    int u = G[k][i];
    adj[u] = true;
  }
  adj[k] = true;
  res = min(res, dfs(k + 1, cur + 1, adj));
  return res;
}
int main() {
  while (cin >> N >> M && N) {
    G = Graph(N);
    vector<int> perm(N);
    REP(i, N) perm[i] = i;
    random_shuffle(perm.begin(), perm.end());
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      a = perm[a];
      b = perm[b];
      G[a].push_back(b);
      G[b].push_back(a);
    }
    min_ans = INF;
    vector<bool> adj(N, false);
    cout << dfs(0, 0, adj) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef vector<int> Node;
typedef vector<Node> Graph;
int min_ans;
int N, M;
Graph G;
int dfs(int k, int cur, vector<bool> adj) {
  if (min_ans <= cur)
    return INF;
  if (k == N) {
    REP(i, N) if (!adj[i]) return INF;
    return min_ans = cur;
  }
  int res = INF;
  res = min(res, dfs(k + 1, cur, adj));
  REP(i, G[k].size()) {
    int u = G[k][i];
    adj[u] = true;
  }
  adj[k] = true;
  res = min(res, dfs(k + 1, cur + 1, adj));
  return res;
}
int main() {
  while (cin >> N >> M && N) {
    G = Graph(N);
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    min_ans = INF;
    vector<bool> adj(N, false);
    cout << dfs(0, 0, adj) << endl;
  }
  return 0;
}