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

#include <bits/stdc++.h>
using namespace std;

int n, m, k, a, b, banyak[3005], hasil = 0, temp, pos, satu;
vector<int> pulau[3005];

int main() {
  memset(banyak, 0, sizeof banyak);
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    pulau[a].push_back(b);
    pulau[b].push_back(a);
    banyak[a]++;
    banyak[b]++;
  }
  while (true) {
    temp = pos = satu = 0;
    for (int i = 1; i <= n; i++) {
      if (banyak[i] > temp) {
        temp = banyak[i];
        pos = i;
      }
      if (banyak[i] == 1)
        satu = i;
    }
    if (temp == 0)
      break;
    hasil++;
    if (satu != 0) {
      for (int i = 0; i < pulau[satu].size(); i++)
        if (banyak[pulau[satu][i]] > 0) {
          pos = pulau[satu][i];
          break;
        }
    }
    for (int i = 0; i < pulau[pos].size(); i++)
      if (banyak[pulau[pos][i]] > 0)
        banyak[pulau[pos][i]]--;
    banyak[pos] = 0;
  }
  if (hasil <= k)
    printf("%d\\n", hasil);
  else
    printf("Impossible\\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;

void rek(int edgpos, int tmp_ans) {
  // cout << "rek " << edgpos << ' ' << tmp_ans << '\\n';
  if (found && tmp_ans > ans)
    return;
  if (tmp_ans > k)
    return;
  while (edgpos < sz &&
         (cov[edglist[edgpos].first] || cov[edglist[edgpos].second]))
    ++edgpos;
  // cout << "whileok\\n";
  if (edgpos == sz) {
    found = true;
    ans = min(tmp_ans, ans);
    return;
  }
  // cout << "check ok\\n";
  cov[edglist[edgpos].first] = true;
  rek(edgpos + 1, tmp_ans + 1);
  vector<int> act_neigh;
  cov[edglist[edgpos].first] = false;
  for (int i = 0; i < adjlist[edglist[edgpos].first].size(); ++i) {
    if (!cov[adjlist[edglist[edgpos].first][i]]) {
      act_neigh.push_back(adjlist[edglist[edgpos].first][i]);
      cov[adjlist[edglist[edgpos].first][i]] = true;
    }
  }
  rek(edgpos + 1, tmp_ans + act_neigh.size());
  for (int i = 0; i < act_neigh.size(); ++i)
    cov[act_neigh[i]] = false;
}

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, 0);
  // cout << "FIX " << fixuse << '\\n';
  if (!found)
    cout << "Impossible\\n";
  else
    cout << ans + fixuse << '\\n';
  return 0;
}