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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(pachico, n) cout << " " << a[pachico];                                   \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(pachico, v.size()) cout << " " << v[pachico];                            \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(pachico, s) cout << " " << pachico;                                     \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template <typename T> class ListIterator;

// 常に不変なイテレーターが欲しい場合に別のコンテナにこれをかぶせる形で使うとよい
template <typename T> class List {
public:
  using iterator = ListIterator<T>;

private:
  int N, sz;
  vector<int> prev, next;
  vector<T> container;
  // ListIterator<T> からのアクセスを許可する
  friend ListIterator<T>;

public:
  // 添字 next_index の要素の前に添字 now_index の要素を挿入
  iterator insert(int next_index, int now_index) {
    sz++;
    prev[now_index] = prev[next_index], next[now_index] = next_index;
    next[prev[next_index]] = now_index, prev[next_index] = now_index;
    return iterator(this, now_index);
  }
  // 添字 index の値を削除 → 次の要素の添字を返す
  iterator erase(int index) {
    sz--;
    next[prev[index]] = next[index], prev[next[index]] = prev[index];
    return iterator(this, next[index]);
  }

public:
  friend ostream &operator<<(ostream &os, List &ls) {
    for (auto &val : ls) {
      os << val << ' ';
    }
    return os;
  }
  const T &operator[](size_t index) const { return container[index]; }
  T &operator[](size_t index) { return container[index]; }
  size_t size() { return sz; }
  bool empty() { return size() == 0; }
  T &front() { return container[next[N]]; }
  T &back() { return container[prev[N]]; }
  iterator begin() { return iterator(this, next[N]); }
  iterator end() { return iterator(this, N); }
  iterator insert(const iterator &itr, int index) {
    return insert(itr.index, index);
  }
  iterator erase(const iterator &itr) { return erase(itr.index); }
  iterator push_back() { return insert(N, prev[N] + 1); }
  void pop_back() { erase(prev[N]); }
  void clear() {
    N = sz = 0;
    prev = next = {0};
    container.clear();
  }

public:
  List() : N(0), sz(0), prev({0}), next({0}) {}
  List(int _N) : container(_N) { build(); }
  List(int _N, T val) : container(_N, val) { build(); }
  List(const List &ls)
      : N(ls.N), sz(ls.sz), prev(ls.prev), next(ls.next),
        container(ls.container) {}
  List(const vector<T> &vec) : container(vec) { build(); }
  List &operator=(const List &ls) {
    container = ls.container;
    build();
    return *this;
  }
  List &operator=(const vector<T> &vec) {
    container = vec;
    build();
    return *this;
  }
  List &operator=(const List &&ls) noexcept {
    container = ls.container;
    build();
    return *this;
  }
  List(initializer_list<T> vals) : container(vals.begin(), vals.end()) {
    build();
  }
  iterator push_back(T val) {
    N++, sz++;
    prev.push_back(N - 1), prev[0] = N;
    next.back() = N, next.push_back(0);
    container.push_back(val);
    return iterator(this, N - 1);
  }
  void build() {
    N = sz = (int)container.size();
    prev.resize(N + 1), next.resize(N + 1);
    iota(prev.begin(), prev.end(), -1), iota(next.begin(), next.end(), 1);
    prev[0] = N, next[N] = 0;
  }
};

template <typename T>
class ListIterator
    : public std::iterator<bidirectional_iterator_tag, T, ptrdiff_t, T *, T &> {
private:
  friend List<T>;
  List<T> *ls_ptr;

private:
  ListIterator(List<T> *ls, int _index) : ls_ptr(ls), index(_index) {}

public:
  int index;
  ListIterator() {}
  ListIterator(const ListIterator &itr) {
    ls_ptr = itr.ls_ptr, index = itr.index;
  }
  ListIterator &operator=(const ListIterator &itr) & {
    ls_ptr = itr.ls_ptr, index = itr.index;
    return *this;
  }
  ListIterator &operator=(ListIterator &&itr) &noexcept {
    ls_ptr = itr.ls_ptr, index = itr.index;
    return *this;
  }
  T &operator*() const { return ls_ptr->container[index]; }
  T *operator->() const { return &ls_ptr->container[index]; }
  ListIterator &operator++() {
    index = ls_ptr->next[index];
    return *this;
  }
  ListIterator operator++(int) {
    ListIterator res = *this;
    index = ls_ptr->next[index];
    return res;
  }
  ListIterator &operator--() {
    index = ls_ptr->prev[index];
    return *this;
  };
  ListIterator operator--(int) {
    ListIterator res = *this;
    index = ls_ptr->prev[index];
    return res;
  };
  bool operator==(const ListIterator &itr) const { return !(*this != itr); };
  bool operator!=(const ListIterator &itr) const {
    return this->ls_ptr != itr.ls_ptr || this->index != itr.index;
  }
  friend void swap(const ListIterator<T> &itr1, const ListIterator<T> &itr2) {
    ListIterator<T> tmp = itr1;
    itr1 = itr2, itr2 = itr1;
  };
};

// 多重辺はなし
class MVC {
private:
  struct edge {
    int to, rev;
  };
  struct info {
    // from から to 番目に出ている辺が(を) next の前から消えた(に入れる)
    int from, to, next;
    info(int ag1, int ag2, int ag3) : from(ag1), to(ag2), next(ag3) {}
  };
  int V, rem_edge_size;
  vector<List<edge>> G;
  List<int> rem_ver;
  vector<int> cand, deg;
  vector<vector<info>> edge_info;
  set<int> small_deg_ver, use_ver;

  bool erase(int u, bool use, bool alr_use, int add_size,
             vector<pair<int, int>> &erase_ver) {
    if (use && !alr_use)
      add_size++, cand.push_back(u);
    // ans より良い解が得られないと分かった
    if ((int)cand.size() >= ans_size)
      return false;
    erase_ver.emplace_back(u, rem_ver.erase(u).index);
    for (auto &e : G[u]) {
      edge_info[u].emplace_back(e.to, e.rev, G[e.to].erase(e.rev).index);
      deg[e.to]--, rem_edge_size--;
      if (deg[e.to] == 0)
        erase_ver.emplace_back(e.to, rem_ver.erase(e.to).index);
      if (!use) {
        if (use_ver.count(e.to) == 1)
          continue;
        add_size++, cand.push_back(e.to);
        // ans より良い解が得られないと分かった
        if ((int)cand.size() >= ans_size)
          return false;
        if (deg[e.to] > 0)
          use_ver.insert(e.to);
      } else {
        if (deg[e.to] == 1)
          small_deg_ver.insert(e.to);
      }
    }
    return true;
  }
  bool erase_rec(int u, bool use, int add_size,
                 vector<pair<int, int>> &erase_ver) {
    if (!erase(u, use, false, add_size, erase_ver))
      return false;
    // cout << u << endl;
    // cout << rem_ver << endl;
    // sset(small_deg_ver), sset(use_ver);
    // svec(cand);
    while (!small_deg_ver.empty() || !use_ver.empty()) {
      while (!small_deg_ver.empty()) {
        int v = *small_deg_ver.begin();
        small_deg_ver.erase(v);
        if (deg[v] == 0)
          continue;
        if (!erase(v, false, false, add_size, erase_ver))
          return false;
        // cout << "--------------------------------------" << endl;
        // cout << v << endl;
        // cout << rem_ver << endl;
        // sset(small_deg_ver), sset(use_ver);
        // svec(cand);
      }
      while (!use_ver.empty()) {
        int v = *use_ver.begin();
        use_ver.erase(v);
        if (deg[v] == 0)
          continue;
        if (!erase(v, true, true, add_size, erase_ver))
          return false;
        // cout << "======================================" << endl;
        // cout << v << endl;
        // cout << rem_ver << endl;
        // sset(small_deg_ver), sset(use_ver);
        // svec(cand);
      }
    }
    return true;
  }
  bool preprocessing(int K, int add_size, vector<pair<int, int>> &erase_ver) {
    for (int u : rem_ver) {
      if (deg[u] > K) {
        if (!erase_rec(u, true, add_size, erase_ver))
          return false;
      }
    }
    return true;
  }
  void postprocessing(int add_size, vector<pair<int, int>> &erase_ver) {
    for (int i = 0; i < add_size; i++) {
      cand.pop_back();
    }
    for (auto &p : erase_ver) {
      for (int i = (int)edge_info[p.first].size() - 1; i >= 0; i--) {
        auto &dat = edge_info[p.first][i];
        G[dat.from].insert(dat.next, dat.to), deg[dat.from]++, rem_edge_size++;
      }
      rem_ver.insert(p.second, p.first);
    }
  }
  bool check() {
    if (rem_edge_size == 0) {
      if ((int)cand.size() < ans_size) {
        ans_size = (int)cand.size(), ans_set = cand;
      }
      return false;
    }
    return (int)rem_ver.size() > 0;
  }
  void transition(int K, int next_ver, bool use) {
    int pl_add_size = 0;
    vector<pair<int, int>> pl_erase_ver;
    if (!erase_rec(next_ver, use, pl_add_size, pl_erase_ver)) {
      return postprocessing(pl_add_size, pl_erase_ver);
    }
    if (check())
      judge(K - 1);
    return postprocessing(pl_add_size, pl_erase_ver);
  }
  void judge(int K) {
    int add_size = 0;
    vector<pair<int, int>> erase_ver;
    if (!preprocessing(K, add_size, erase_ver))
      return postprocessing(add_size, erase_ver);
    if (!check())
      return postprocessing(add_size, erase_ver);
    // 次数の高い頂点から探索する
    int max_deg = -1, next_ver = -1;
    for (int v : rem_ver) {
      if (deg[v] > max_deg)
        max_deg = deg[v], next_ver = v;
    }
    if (2 * rem_edge_size > (long long)max_deg * (int)rem_ver.size()) {
      return postprocessing(add_size, erase_ver);
    }
    // show(rem_ver);
    // show(next_ver);
    // 使う場合
    transition(K, next_ver, true);
    // 使わない場合
    if ((int)cand.size() + (int)G[next_ver].size() >= ans_size) {
      transition(K, next_ver, false);
    }
    return postprocessing(add_size, erase_ver);
  }

public:
  vector<int> ans_set;
  int ans_size;

  MVC(int node_size)
      : V(node_size), rem_edge_size(0), G(V), rem_ver(V), deg(V, 0),
        edge_info(V) {
    for (int i = 0; i < V; i++)
      rem_ver[i] = i;
  }
  void add_edge(int u, int v) {
    G[u].push_back((edge){v, (int)G[v].size()});
    G[v].push_back((edge){u, (int)G[u].size() - 1});
    deg[u]++, deg[v]++, rem_edge_size++;
  }
  bool solve(int K) {
    ans_size = K + 1;
    judge(K);
    return ans_size <= K;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, K;
  cin >> n >> m >> K;
  MVC mvc(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    mvc.add_edge(a - 1, b - 1);
  }
  if (mvc.solve(K)) {
    cout << mvc.ans_size << "\\n";
  } else {
    cout << "Impossible\\n";
  }
  // svec(mvc.ans_set);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(pachico, n) cout << " " << a[pachico];                                   \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(pachico, v.size()) cout << " " << v[pachico];                            \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(pachico, s) cout << " " << pachico;                                     \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template <typename T> class ListIterator;

// 常に不変なイテレーターが欲しい場合に別のコンテナにこれをかぶせる形で使うとよい
template <typename T> class List {
public:
  using iterator = ListIterator<T>;
  int N, sz;
  vector<int> prev, next;

private:
  vector<T> container;
  // ListIterator<T> からのアクセスを許可する
  friend ListIterator<T>;
  // 添字 index の要素の前に添字 の要素を挿入
  iterator insert(int next_index, int now_index) {
    sz++;
    prev[now_index] = prev[next_index], next[now_index] = next_index;
    next[prev[next_index]] = now_index, prev[next_index] = now_index;
    return iterator(this, now_index);
  }
  // 添字 index の値を削除 → 次の要素の添字を返す
  iterator erase(int index) {
    sz--;
    next[prev[index]] = next[index], prev[next[index]] = prev[index];
    return iterator(this, next[index]);
  }

public:
  friend ostream &operator<<(ostream &os, List &ls) {
    for (auto &val : ls) {
      os << val << ' ';
    }
    return os;
  }
  const T &operator[](size_t index) const { return container[index]; }
  T &operator[](size_t index) { return container[index]; }
  size_t size() { return sz; }
  bool empty() { return size() == 0; }
  T &front() { return container[next[N]]; }
  T &back() { return container[prev[N]]; }
  iterator begin() { return iterator(this, next[N]); }
  iterator end() { return iterator(this, N); }
  iterator insert(const iterator &itr, int index) {
    return insert(itr.index, index);
  }
  iterator erase(const iterator &itr) { return erase(itr.index); }
  iterator push_back() { return insert(N, prev[N] + 1); }
  void pop_back() { erase(prev[N]); }
  void clear() { prev.clear(), next.clear(), container.clear(); }

public:
  List() : N(0), sz(0), prev({0}), next({0}) {}
  List(int _N) : container(_N) { build(); }
  List(int _N, T val) : container(_N, val) { build(); }
  List(const List &ls)
      : N(ls.N), sz(ls.sz), prev(ls.prev), next(ls.next),
        container(ls.container) {}
  List(const vector<T> &vec) : container(vec) { build(); }
  List &operator=(const List &ls) {
    container = ls.container;
    build();
    return *this;
  }
  List &operator=(const vector<T> &vec) {
    container = vec;
    build();
    return *this;
  }
  List &operator=(const List &&ls) noexcept {
    container = ls.container;
    build();
    return *this;
  }
  List(initializer_list<T> vals) : container(vals.begin(), vals.end()) {
    build();
  }
  iterator push_back(T val) {
    N++, sz++;
    prev.push_back(N - 1), prev[0] = N;
    next.back() = N, next.push_back(0);
    container.push_back(val);
    return iterator(this, N - 1);
  }
  void build() {
    N = sz = (int)container.size();
    prev.resize(N + 1), next.resize(N + 1);
    iota(prev.begin(), prev.end(), -1), iota(next.begin(), next.end(), 1);
    prev[0] = N, next[N] = 0;
  }
};

template <typename T>
class ListIterator
    : public std::iterator<bidirectional_iterator_tag, T, ptrdiff_t, T *, T &> {
private:
  friend List<T>;
  List<T> *ls_ptr;

private:
  ListIterator(List<T> *ls, int _index) : ls_ptr(ls), index(_index) {}

public:
  int index;
  ListIterator() {}
  ListIterator(const ListIterator &itr) {
    ls_ptr = itr.ls_ptr, index = itr.index;
  }
  ListIterator &operator=(const ListIterator &itr) & {
    ls_ptr = itr.ls_ptr, index = itr.index;
    return *this;
  }
  ListIterator &operator=(ListIterator &&itr) &noexcept {
    ls_ptr = itr.ls_ptr, index = itr.index;
    return *this;
  }
  T &operator*() const { return ls_ptr->container[index]; }
  T *operator->() const { return &ls_ptr->container[index]; }
  ListIterator &operator++() {
    index = ls_ptr->next[index];
    return *this;
  }
  ListIterator operator++(int) {
    ListIterator res = *this;
    index = ls_ptr->next[index];
    return res;
  }
  ListIterator &operator--() {
    index = ls_ptr->prev[index];
    return *this;
  };
  ListIterator operator--(int) {
    ListIterator res = *this;
    index = ls_ptr->prev[index];
    return res;
  };
  bool operator==(const ListIterator &itr) const { return !(*this != itr); };
  bool operator!=(const ListIterator &itr) const {
    return this->ls_ptr != itr.ls_ptr || this->index != itr.index;
  }
  friend void swap(const ListIterator<T> &itr1, const ListIterator<T> &itr2) {
    ListIterator<T> tmp = itr1;
    itr1 = itr2, itr2 = itr1;
  };
};

// 多重辺を含まないとする
class MVC {
private:
  struct edge {
    int to;
    List<edge>::iterator rev;
    edge(int _to) : to(_to) {}
  };

  int V, rem_ver, rem_edge;
  bool ok;
  vector<List<edge>> G;
  vector<pair<int, int>> es;
  vector<int> deg, ver;
  vector<bool> rem, use;

  void delete_edge(const int u, vector<pair<int, int>> &ch,
                   vector<List<edge>::iterator> &ech, vector<int> &add,
                   bool _use) {
    for (auto &e : G[u]) {
      if (_use)
        use[e.to] = true, add.push_back(e.to);
      ech.push_back(G[e.to].erase(e.rev)), deg[e.to]--, rem_edge--,
          ch.emplace_back(e.to, e.rev.index);
      // if(e.to == 2){
      //     svec(G[2].prev);
      //     svec(G[2].next);
      //     int hoge = 0;
      //     for(auto& e : G[2]){
      //         cout << e.to << " ";
      //         hoge++;
      //         if(hoge >= 3){
      //             break;
      //         }
      //     }
      //     cout << "\\n";
      // }
      if (deg[e.to] == 0)
        rem[e.to] = false, rem_ver--;
    }
  }
  void inv_delete_edge(const int u, vector<pair<int, int>> &ch,
                       vector<List<edge>::iterator> &ech, vector<int> &add,
                       bool _use) {
    for (int i = (int)ch.size() - 1; i >= 0; i--) {
      int v = ch[i].first;
      if (_use)
        use[v] = false, add.pop_back();
      G[v].insert(ech[i], ch[i].second), deg[v]++, rem_edge++;
      // if(u == 2 && v == 3){
      //     cout << "================================\\n";
      //     show(ech[i].index);
      //     svec(G[v].prev);
      //     svec(G[v].next);
      // }
      if (deg[v] == 1)
        rem[v] = true, rem_ver++;
    }
  }
  void preprocessing(const int K, vector<int> &ans) {
    for (int i = 0; i < V; i++) {
      if (deg[i] > K) {
        ans.push_back(i), rem[i] = false;
      } else {
        ver.push_back(i), rem_ver++;
      }
    }
    for (auto &e : es) {
      if (rem[e.first] && rem[e.second]) {
        rem_edge++;
      } else {
        if (rem[e.first])
          deg[e.first]--;
        if (rem[e.second])
          deg[e.second]--;
      }
    }
    for (auto &e : es) {
      if (rem[e.first] && rem[e.second]) {
        auto it1 = G[e.first].push_back(edge(e.second));
        auto it2 = G[e.second].push_back(edge(e.first));
        G[e.first].back().rev = it2, G[e.second].back().rev = it1;
      }
    }
  }
  void rec(const int K, vector<int> &add, vector<int> &add_opt) {
    // show(rem_edge);
    // svec(rem);
    if (rem_edge == 0) {
      ok = true;
      if (add_opt.empty() || (int)add.size() < (int)add_opt.size())
        add_opt = add;
      return;
    }
    // 最大次数の頂点から探索
    int max_deg = numeric_limits<int>::min(), pos = -1;
    for (int v : ver) {
      if (rem[v] && deg[v] > max_deg) {
        max_deg = deg[v], pos = v;
      }
    }
    // int hoge = 0;
    // for(auto& e : G[2]){
    //     cout << e.to << " ";
    //     hoge++;
    //     if(hoge >= 3){
    //         break;
    //     }
    // }
    // cout << "\\n";
    // 枝数の条件
    if (rem_edge > max_deg * rem_ver)
      return;
    // show("BUE");
    rem[pos] = false, rem_ver--;
    // show(pos), show(max_deg);
    if (use[pos] || max_deg >= 2) {
      // 使う場合
      vector<pair<int, int>> ch;
      vector<List<edge>::iterator> ech;
      // show(pos);
      // int hoge = 0;
      // for(auto& e : G[2]){
      //     cout << e.to << " ";
      //     hoge++;
      //     if(hoge >= 3){
      //         break;
      //     }
      // }
      // cout << "\\n";
      // svec(G[2].next);
      delete_edge(pos, ch, ech, add, false);
      // hoge = 0;
      // for(auto& e : G[2]){
      //     cout << e.to << " ";
      //     hoge++;
      //     if(hoge >= 3){
      //         break;
      //     }
      // }
      // cout << "\\n";
      if (!use[pos])
        add.push_back(pos);
      if ((int)add.size() + (int)ans.size() <= K)
        rec(K, add, add_opt);
      // show("OK");
      if (!use[pos])
        add.pop_back();
      inv_delete_edge(pos, ch, ech, add, false);
    }
    if (use[pos])
      return;
    // show("KITA");
    // 使わない場合
    vector<pair<int, int>> ch;
    vector<List<edge>::iterator> ech;
    // hoge = 0;
    // for(auto& e : G[2]){
    //     cout << e.to << " ";
    //     hoge++;
    //     if(hoge >= 3){
    //         break;
    //     }
    // }
    // cout << "\\n";
    delete_edge(pos, ch, ech, add, true);
    // show(ech[0].index);
    if ((int)add.size() + (int)ans.size() <= K)
      rec(K, add, add_opt);
    // svec(G[3].prev);
    // svec(G[3].next);
    inv_delete_edge(pos, ch, ech, add, true);
    rem[pos] = true, rem_ver++;
    return;
  }

public:
  vector<int> ans;

  MVC(int node_size)
      : V(node_size), rem_ver(0), rem_edge(0), ok(false), G(V), deg(V, 0),
        rem(V, true), use(V, false) {}
  void add_edge(const int u, const int v) {
    es.emplace_back(u, v);
    deg[u]++, deg[v]++;
  }
  bool judge(const int K) {
    preprocessing(K, ans);
    vector<int> add, add_opt;
    rec(K, add, add_opt);
    for (int v : add_opt) {
      ans.push_back(v);
    }
    sort(ans.begin(), ans.end());
    return ok && (int)ans.size() <= K;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, K;
  cin >> n >> m >> K;
  MVC mvc(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    mvc.add_edge(a - 1, b - 1);
  }
  if (mvc.judge(K)) {
    cout << len(mvc.ans) << "\\n";
  } else {
    cout << "Impossible\\n";
  }
  return 0;
}