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

#include <bits/stdc++.h>
#define cincout
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -(x) : (x))
using namespace std;

int cov[3005];
vector<int> adjlist[3005];
int n, m, k, ans;
int inp_a, inp_b;
int get_deg(int u) {
  int ret = 0;
  for (int i = 0; i < adjlist[u].size(); ++i) {
    if (!cov[adjlist[u][i]])
      ++ret;
  }
  return ret;
}

void rek(int tmp_ans) {
  if (tmp_ans > k)
    return;
  int deg_u;
  for (int i = 1; i <= n; ++i) {
    if (cov[i] || (deg_u = get_deg(i)) == 0)
      continue;
    else if (deg_u == 1) {
      int neigh;
      for (int j = 0; j < adjlist[i].size(); ++j) {
        if (!cov[adjlist[i][j]] && get_deg(adjlist[i][j]) > 0) {
          neigh = adjlist[i][j];
          break;
        }
      }
      cov[neigh] = true;
      rek(tmp_ans + 1);
      cov[neigh] = false;
      return;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (cov[i] || (deg_u = get_deg(i)) == 0)
      continue;
    else {
      cov[i] = true;
      rek(tmp_ans + 1);
      cov[i] = false;
      vector<int> dat;
      int cnt = 0;
      for (int j = 0; j < adjlist[i].size(); ++j) {
        if (!cov[adjlist[i][j]] && get_deg(adjlist[i][j]) > 0) {
          dat.push_back(adjlist[i][j]);
          ++cnt;
        }
      }
      for (int j = 0; j < cnt; ++j)
        cov[dat[j]] = true;
      rek(tmp_ans + cnt);
      for (int j = 0; j < cnt; ++j)
        cov[dat[j]] = false;
      return;
    }
  }
  ans = min(ans, tmp_ans);
}

int main() {
#ifdef cincout
  cin.sync_with_stdio(0);
  cin.tie(0);
#endif
  memset(cov, false, sizeof(cov));
  cin >> n >> m >> k;
  for (int i = 0; i < m; ++i) {
    cin >> inp_a >> inp_b;
    adjlist[inp_a].push_back(inp_b);
    adjlist[inp_b].push_back(inp_a);
  }
  ans = 1000000;
  rek(0);
  if (ans > k)
    cout << "Impossible\\n";
  else
    cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define cincout
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -(x) : (x))
using namespace std;
typedef pair<int, int> pii;
int n, m, k, ans, tmppos, fixuse, sz;
bool cov[3005], found;
int a, b, tmp, edgs;
int deg[3005];
pair<int, int> edg[30005];
vector<int> adjlist[3005];
vector<pii> edglist;
priority_queue<pii, vector<pii>, greater<pii>> pq;

int get_deg(int u) {
  int ret = 0;
  for (int i = 0; i < adjlist[u].size(); ++i)
    if (!cov[adjlist[u][i]])
      ++ret;
  return ret;
}

void rek(int tmp_ans) {
  if (tmp_ans > k)
    return;
  if (found && tmp_ans > ans)
    return;
  bool ok = true;
  int deg_u, tmp_deg = 1000000, target;
  for (int i = 1; i <= n; ++i) {
    if (cov[i] || ((deg_u = get_deg(i)) == 0))
      continue;
    else {
      ok = false;
      cov[i] = true;
      rek(tmp_ans + 1);
      cov[i] = false;
    }
  }
  if (ok) {
    found = true;
    ans = min(ans, tmp_ans);
    return;
  }
}

int main() {
#ifdef cincout
  cin.sync_with_stdio(0);
  cin.tie(0);
#endif
  cin >> n >> m >> k;
  fixuse = 0;
  found = false;
  ans = 1000000;
  edgs = m;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    edglist.push_back(make_pair(a, b));
    edglist.push_back(make_pair(b, a));
    adjlist[a].push_back(b);
    adjlist[b].push_back(a);
    ++deg[a];
    ++deg[b];
  }
  sz = edglist.size();
  // cout << "Sz " << sz << '\\n';
  for (int i = 1; i <= n; ++i)
    pq.push(make_pair(deg[i], i));
  while (!pq.empty()) {
    tmp = pq.top().second;
    pq.pop();
    if (cov[tmp])
      continue;
    else {
      if (deg[tmp] > k) {
        cov[tmp] = true;
        deg[tmp] = 0;
        ++fixuse;
        --k;
        for (int i = 0; i < adjlist[tmp].size(); ++i) {
          if (!cov[adjlist[tmp][i]]) {
            pq.push(make_pair(--deg[adjlist[tmp][i]], adjlist[tmp][i]));
            --edgs;
          }
        }
      } else if (deg[tmp] == 1) {
        ++fixuse;
        cov[tmp] = true;
        --k;
        for (int i = 0; i < adjlist[tmp].size(); ++i) {
          if (!cov[adjlist[tmp][i]]) {
            cov[adjlist[tmp][i]] = true;
            int tk = adjlist[tmp][i];
            deg[tmp] = 0;
            for (int j = 0; j < adjlist[tk].size(); ++j)
              if (!cov[adjlist[tk][j]]) {
                pq.push(make_pair(--deg[adjlist[tk][j]], adjlist[tk][j]));
                --edgs;
              }
            break;
          }
        }
      }
    }
  }
  // cout << "ok\\n";
  rek(0);
  // cout << "FIX " << fixuse << '\\n';
  if (!found)
    cout << "Impossible\\n";
  else
    cout << ans + fixuse << '\\n';
  return 0;
}