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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int v, w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

struct union_find {
  vector<int> v;
  union_find(int n) : v(n, -1) {}
  int find(int x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (-v[x] < -v[y])
        swap(x, y);
      v[x] += v[y];
      v[y] = x;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -v[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[i], &b[i]);
    a[i]--;
    b[i]--;
  }
  vector<bool> rm(n);
  for (;;) {
    vector<int> d(n);
    union_find uf(n);
    for (int i = 0; i < m; i++) {
      d[a[i]]++;
      d[b[i]]++;
      uf.unite(a[i], b[i]);
    }
    vector<vector<int>> v(n);
    for (int u = 0; u < n; u++) {
      if (rm[u])
        continue;
      int p = uf.find(u);
      if (d[u] == uf.size(p) - 1)
        v[p].push_back(u);
    }
    bool updated = false;
    for (int p = 0; p < n; p++) {
      if (rm[p])
        continue;
      if (uf.find(p) != p)
        continue;
      if (v[p].size() == uf.size(p))
        continue;
      updated = true;
      for (int j = 0; j < v[p].size(); j++)
        rm[v[p][j]] = true;
    }
    if (!updated) {
      int x = 0, y = 1;
      for (int p = 0; p < n; p++) {
        if (rm[p])
          continue;
        if (uf.find(p) != p)
          continue;
        x++;
        y = (ll)y * uf.size(p) % _MOD;
      }
      cout << x << endl << y << endl;
      return 0;
    }
    vector<int> _a, _b;
    for (int i = 0; i < m; i++)
      if (!rm[a[i]] && !rm[b[i]]) {
        _a.push_back(a[i]);
        _b.push_back(b[i]);
      }
    m = _a.size();
    a = _a;
    b = _b;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int v, w;
};

ll MOD = 1000000009;
ll _MOD = 1000000009;
double EPS = 1e-10;

struct union_find {
  vector<int> v;
  union_find(int n) : v(n, -1) {}
  int find(int x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (-v[x] < -v[y])
        swap(x, y);
      v[x] += v[y];
      v[y] = x;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -v[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[i], &b[i]);
    a[i]--;
    b[i]--;
  }
  vector<bool> rm(n);
  for (;;) {
    vector<int> d(n);
    union_find uf(n);
    for (int i = 0; i < m; i++) {
      d[a[i]]++;
      d[b[i]]++;
      uf.unite(a[i], b[i]);
    }
    vector<vector<int>> v(n);
    for (int u = 0; u < n; u++) {
      if (rm[u])
        continue;
      int p = uf.find(u);
      if (d[u] == uf.size(p) - 1)
        v[p].push_back(u);
    }
    bool updated = false;
    for (int p = 0; p < n; p++) {
      if (rm[p])
        continue;
      if (uf.find(p) != p)
        continue;
      if (v[p].size() == uf.size(p))
        continue;
      updated = true;
      rm[v[p][0]] = true;
    }
    if (!updated) {
      int x = 0, y = 1;
      for (int p = 0; p < n; p++) {
        if (rm[p])
          continue;
        if (uf.find(p) != p)
          continue;
        x++;
        y = (ll)y * uf.size(p) % MOD;
      }
      cout << x << endl << y << endl;
      return 0;
    }
    vector<int> _a, _b;
    for (int i = 0; i < m; i++)
      if (!rm[a[i]] && !rm[b[i]]) {
        _a.push_back(a[i]);
        _b.push_back(b[i]);
      }
    m = _a.size();
    a = _a;
    b = _b;
  }
}