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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef pair<int, int> P;
int N, M, K;
vector<int> G[3000];
vector<P> es;
int d[3000], use[30000];
int dfs(int x, int k) {
  for (int i = x; i < M; i++) {
    int a = es[i].fs, b = es[i].sc;
    if (use[a] != 1 && use[b] != 1) {
      if (k == 0)
        return -1;
      if (d[a] < d[b])
        swap(a, b);
      int best = -1;
      // use a
      if (use[a] < 0) { // use[a]==-1
        use[a] = 1;
        vector<int> vc;
        for (int v : G[a]) {
          if (use[v] < 0) {
            vc.pb(v);
            d[v]--;
          }
        }
        chmax(best, dfs(x + 1, k - 1));
        for (int v : vc)
          d[v]++;
        use[a] = -1;
      }
      // not use a
      if (d[b] > 1) {
        use[a] = 0;
        vector<int> vc;
        int nk = k;
        for (int v : G[a]) {
          // assert(use[v]!=0);
          if (use[v] != 1) {
            use[v] = 1;
            nk--;
            vc.pb(v);
            d[v]--;
          }
        }
        chmax(best, dfs(x + 1, nk));
        for (int v : vc)
          use[v] = -1, d[v]++;
        use[b] = -1;
      }
      return best;
    }
  }
  return k;
}
int main() {
  cin >> N >> M >> K;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].pb(b);
    G[b].pb(a);
    d[a]++, d[b]++;
    es.pb(P(a, b));
  }
  rep(i, N) use[i] = -1;
  rep(i, N) if (d[i] == 0) use[i] = 0;
  int k = K;
  while (true) {
    int v = -1;
    rep(i, N) if (d[i] > k) {
      v = i;
      break;
    }
    if (v < 0)
      break;
    use[v] = 1;
    for (int u : G[v]) {
      if (d[u])
        d[u]--;
    }
    d[v] = 0;
    k--;
    if (k < 0) {
      puts("Impossible");
      return 0;
    }
  }
  int Mc = M;
  M = 0;
  rep(i, Mc) if (use[es[i].fs] != 1 && use[es[i].sc] != 1) es[M++] = es[i];
  if (M > k * k) {
    puts("Impossible");
    return 0;
  }
  rep(v, N) {
    int j = 0;
    rep(i, G[v].size()) {
      int u = G[v][i];
      if (use[v] != 1 && use[u] != 1)
        G[v][j++] = u;
    }
    G[v].resize(j);
  }
  //	show(K);
  //	show(k);
  //	show(M);
  int ans = dfs(0, k);
  if (ans < 0) {
    puts("Impossible");
    return 0;
  }
  cout << K - ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef pair<int, int> P;
int N, M, K;
vector<int> G[3000];
vector<P> es;
int d[3000], use[30000];
int dfs(int x, int k) {
  for (int i = x; i < M; i++) {
    int a = es[i].fs, b = es[i].sc;
    if (use[a] != 1 && use[b] != 1) {
      if (k == 0)
        return -1;
      if (d[a] < d[b])
        swap(a, b);
      int best = -1;
      // use a
      if (use[a] < 0) {
        use[a] = 1;
        vector<int> vc;
        for (int v : G[a]) {
          if (use[v] < 0) {
            vc.pb(v);
            d[v]--;
          }
        }
        chmax(best, dfs(x + 1, k - 1));
        for (int v : vc)
          d[v]++;
        use[a] = -1;
      }
      // use b
      if (use[b] < 0 && d[b] > 1) {
        use[b] = 1;
        vector<int> vc;
        for (int v : G[b]) {
          if (use[v] < 0) {
            vc.pb(v);
            d[v]--;
          }
        }
        chmax(best, dfs(x + 1, k - 1));
        for (int v : vc)
          d[v]++;
        use[b] = -1;
      }
      return best;
    }
  }
  return k;
}
int main() {
  cin >> N >> M >> K;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].pb(b);
    G[b].pb(a);
    d[a]++, d[b]++;
    es.pb(P(a, b));
  }
  rep(i, N) use[i] = -1;
  rep(i, N) if (d[i] == 0) use[i] = 0;
  int k = K;
  while (true) {
    int v = -1;
    rep(i, N) if (d[i] > k) {
      v = i;
      break;
    }
    if (v < 0)
      break;
    use[v] = 1;
    for (int u : G[v]) {
      if (d[u])
        d[u]--;
    }
    d[v] = 0;
    k--;
    if (k < 0) {
      puts("Impossible");
      return 0;
    }
  }
  int Mc = M;
  M = 0;
  rep(i, Mc) if (use[es[i].fs] != 1 && use[es[i].sc] != 1) es[M++] = es[i];
  if (M > k * k) {
    puts("Impossible");
    return 0;
  }
  rep(v, N) {
    int j = 0;
    rep(i, G[v].size()) {
      int u = G[v][i];
      if (use[v] != 1 && use[u] != 1)
        G[v][j++] = u;
    }
    G[v].resize(j);
  }
  int ans = dfs(0, k);
  if (ans < 0) {
    puts("Impossible");
    return 0;
  }
  cout << K - ans << endl;
}