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

// Template {{{
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

#ifdef LOCAL
#include "contest.h"
#else
#define dump(x)
#endif

class range {
  struct Iterator {
    int val, inc;
    int operator*() { return val; }
    bool operator!=(Iterator &rhs) { return val < rhs.val; }
    void operator++() { val += inc; }
  };
  Iterator i, n;

public:
  range(int e) : i({0, 1}), n({e, 1}) {}
  range(int b, int e) : i({b, 1}), n({e, 1}) {}
  range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(12);
}
//}}}

int N, M, K;
int deg[3030];
bool used[3030];
int ans;
vector<pair<int, int>> edges;
vector<vector<int>> nodes;
const int TLE = 7.9 * CLOCKS_PER_SEC;
clock_t start_time;
int h() {
  bool used2[3030];
  REP(i, N) used2[i] = used[i];
  int res = 0;
  for (auto p : edges) {
    if (used2[p.first] || used2[p.second])
      continue;
    used2[p.first] = used2[p.second] = true;
    res++;
  }
  return res;
}
void dfs(int idx, int cnt) {
  int hr = h();
  if (cnt + hr >= ans)
    return;
  if (hr == 0) {
    ans = cnt;
    return;
  }

  clock_t diff = clock() - start_time;
  if (diff > TLE) {
    return;
  }

  const int E = nodes[idx].size();
  vector<bool> back(E);
  bool nouse_ok = true;

  for (int i = 0; i < E; i++) {
    const int v = nodes[idx][i];
    back[i] = used[v];
    deg[v]--;

    if (v < idx && !used[v])
      nouse_ok = false;
  }

  if (deg[idx] > 0) {
    used[idx] = true;
    dfs(idx + 1, cnt + 1);
    used[idx] = false;
  }

  for (int i = 0; i < E; i++) {
    const int v = nodes[idx][i];
    used[v] = back[i];
    deg[v]++;
  }

  if (nouse_ok) {
    dfs(idx + 1, cnt);
  }
}
int main() {
  iostream_init();
  start_time = clock();
  while (cin >> N >> M >> K) {
    memset(deg, 0, sizeof(deg));
    memset(used, 0, sizeof(used));
    edges.resize(M);
    nodes.assign(N, vector<int>());
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      if (a > b)
        swap(a, b);
      nodes[a].push_back(b);
      nodes[b].push_back(a);
      edges[i] = make_pair(a, b);
      deg[a]++;
      deg[b]++;
    }

    vector<int> perm(N);
    REP(i, N) perm[i] = i;
    REP(j, N) REP(i, j) if (deg[perm[i]] < deg[perm[j]]) swap(perm[i], perm[j]);
    map<int, int> to;
    REP(i, N) to[perm[i]] = i;

    int deg_tmp[3030];
    REP(i, N) deg_tmp[i] = deg[i];
    REP(i, N) deg[i] = deg_tmp[perm[i]];

    auto nodes_tmp = nodes;
    REP(i, N) nodes[i] = nodes_tmp[perm[i]];
    REP(i, N) REP(j, nodes[i].size()) nodes[i][j] = to[nodes[i][j]];

    REP(i, M) edges[i] = make_pair(to[edges[i].first], to[edges[i].second]);

    ans = K + 1;
    dfs(0, 0);
    if (ans == K + 1) {
      cout << "Impossible" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

/* vim:set foldmethod=marker: */

Error: TLE

Fixed submission:

// Template {{{
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

#ifdef LOCAL
#include "contest.h"
#else
#define dump(x)
#endif

class range {
  struct Iterator {
    int val, inc;
    int operator*() { return val; }
    bool operator!=(Iterator &rhs) { return val < rhs.val; }
    void operator++() { val += inc; }
  };
  Iterator i, n;

public:
  range(int e) : i({0, 1}), n({e, 1}) {}
  range(int b, int e) : i({b, 1}), n({e, 1}) {}
  range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(12);
}
//}}}

int N, M, K;
int deg[3030];
bool used[3030];
int ans;
vector<pair<int, int>> edges;
vector<vector<int>> nodes;
int h() {
  bool used2[3030];
  REP(i, N) used2[i] = used[i];
  int res = 0;
  for (auto p : edges) {
    if (used2[p.first] || used2[p.second])
      continue;
    used2[p.first] = used2[p.second] = true;
    res++;
  }
  return res;
}
void dfs(int idx, int cnt) {
  int hr = h();
  if (cnt + hr >= ans)
    return;
  if (hr == 0) {
    ans = cnt;
    return;
  }

  const int E = nodes[idx].size();
  vector<bool> back(E);
  bool nouse_ok = true;

  for (int i = 0; i < E; i++) {
    const int v = nodes[idx][i];
    back[i] = used[v];
    deg[v]--;

    if (v < idx && !used[v])
      nouse_ok = false;
  }

  if (deg[idx] > 0) {
    used[idx] = true;
    dfs(idx + 1, cnt + 1);
    used[idx] = false;
  }

  for (int i = 0; i < E; i++) {
    const int v = nodes[idx][i];
    used[v] = back[i];
    deg[v]++;
  }

  if (nouse_ok) {
    dfs(idx + 1, cnt);
  }
}
int main() {
  iostream_init();
  while (cin >> N >> M >> K) {
    memset(deg, 0, sizeof(deg));
    memset(used, 0, sizeof(used));
    edges.resize(M);
    nodes.assign(N, vector<int>());
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      if (a > b)
        swap(a, b);
      nodes[a].push_back(b);
      nodes[b].push_back(a);
      edges[i] = make_pair(a, b);
      deg[a]++;
      deg[b]++;
    }

    vector<int> perm(N);
    REP(i, N) perm[i] = i;
    REP(j, N) REP(i, j) if (deg[perm[i]] < deg[perm[j]]) swap(perm[i], perm[j]);
    map<int, int> to;
    REP(i, N) to[perm[i]] = i;

    int deg_tmp[3030];
    REP(i, N) deg_tmp[i] = deg[i];
    REP(i, N) deg[i] = deg_tmp[perm[i]];

    auto nodes_tmp = nodes;
    REP(i, N) nodes[i] = nodes_tmp[perm[i]];
    REP(i, N) REP(j, nodes[i].size()) nodes[i][j] = to[nodes[i][j]];

    REP(i, M) edges[i] = make_pair(to[edges[i].first], to[edges[i].second]);

    ans = K + 1;
    dfs(0, 0);
    if (ans == K + 1) {
      cout << "Impossible" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

/* vim:set foldmethod=marker: */