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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
int N, M, K;

int getans(const vector<vector<int>> &edges, const int now, const int use,
           vector<int> &used) {
  if (now == edges.size() || edges[now].empty())
    return use;
  int ans = 33;

  if (used[now]) {
    return getans(edges, now + 1, use, used);
  } else {
    if (use + 1 <= K) {
      used[now] = true;
      ans = min(ans, getans(edges, now + 1, use + 1, used));
      used[now] = false;
    }
    int nextuse = use;
    vector<int> newused;
    for (auto e : edges[now]) {
      if (!used[e]) {
        used[e] = true;
        newused.push_back(e);
      }
    }
    if (use + newused.size() <= K) {
      ans = min(ans, getans(edges, now + 1, use + newused.size(), used));
    }
    for (auto u : newused) {
      used[u] = false;
    }
    // while(edges[num].first==)
  }
  return ans;
}
int main() {
  cin >> N >> M >> K;
  vector<pair<int, int>> ps(N);
  for (int i = 0; i < ps.size(); ++i) {
    ps[i] = make_pair(0, i);
  }
  vector<vector<int>> aedges(N);
  set<int> aset;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    aedges[a].push_back(b);
    aedges[b].push_back(a);
    ps[a].first++;
    ps[b].first++;
    aset.emplace(a);
    aset.emplace(b);
  }
  for (auto &p : ps) {
    if (p.first == 0)
      p.first = -1e8;
  }

  map<int, int> mp;
  for (int i = 0; i < aset.size(); ++i) {
    auto it = max_element(ps.begin(), ps.end(),
                          [](const pair<int, int> &l, const pair<int, int> &r) {
                            return l.first < r.first;
                          });
    for (auto e : aedges[it->second]) {
      ps[e].first--;
    }
    it->first = -1;
    mp[it->second] = i;
  }
  vector<vector<int>> realedges(mp.size());
  for (int i = 0; i < aedges.size(); ++i) {
    for (auto e : aedges[i]) {
      if (mp[i] < mp[e]) {
        realedges[mp[i]].push_back(mp[e]);
      }
    }
  }
  vector<int> used(mp.size());
  int ans = getans(realedges, 0, 0, used);
  if (ans == 33) {
    cout << "Impossible" << endl;
  } else {

    cout << ans << endl;
  }
  // for()
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
int N, M, K;

int getans(const vector<vector<int>> &edges, const int now, const int use,
           vector<int> &used) {
  if (now == edges.size())
    return use;
  int ans = 33;

  if (used[now] || edges[now].empty()) {
    return getans(edges, now + 1, use, used);
  } else {
    if (use + 1 <= K) {
      used[now] = true;
      ans = min(ans, getans(edges, now + 1, use + 1, used));
      used[now] = false;
    }
    int nextuse = use;
    vector<int> newused;
    for (auto e : edges[now]) {
      if (!used[e]) {
        used[e] = true;
        newused.push_back(e);
      }
    }
    if (use + newused.size() <= K) {
      ans = min(ans, getans(edges, now + 1, use + newused.size(), used));
    }
    for (auto u : newused) {
      used[u] = false;
    }
    // while(edges[num].first==)
  }
  return ans;
}
int main() {
  cin >> N >> M >> K;
  vector<pair<int, int>> edges;
  vector<pair<int, int>> ps(N);
  for (int i = 0; i < ps.size(); ++i) {
    ps[i] = make_pair(0, i);
  }
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges.emplace_back(a, b);
    edges.emplace_back(b, a);
    ps[a].first++;
    ps[b].first++;
  }

  sort(ps.begin(), ps.end(), greater<pair<int, int>>());
  map<int, int> mp;
  for (int i = 0; i < ps.size(); ++i) {
    if (ps[i].first == 0)
      break;
    mp[ps[i].second] = i;
  }
  vector<vector<int>> realedges(mp.size());
  for (int i = 0; i < edges.size(); ++i) {
    int f = mp[edges[i].first];
    int t = mp[edges[i].second];
    if (f < t)
      realedges[f].push_back(t);
  }
  vector<int> used(mp.size());
  int ans = getans(realedges, 0, 0, used);
  if (ans == 33) {
    cout << "Impossible" << endl;
  } else {

    cout << ans << endl;
  }
  // for()
  return 0;
}