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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef long long ll;
typedef pair<int, int> P;
ll mod = 1e9 + 9;
int N, M, deg[100001];
int ans = 0;
ll num = 1;
vector<int> G[100001]; // id,to
bool done[100001] = {};
bool comp(const int &x, const int &y) { return deg[x] > deg[y]; }
void add_edge(int x, int y) {
  G[x].pb(y);
  G[y].pb(x);
  deg[x]++, deg[y]++;
}
int main() {
  cin >> N >> M;
  if ((ll)N * (N - 1) / 2 == M) {
    cout << 1 << endl << N << endl;
    return 0;
  }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    add_edge(x, y);
  }
  rep1(i, N) add_edge(0, i);
  vector<int> vc;
  rep(i, N + 1) vc.pb(i);
  sort(all(vc), comp);
  for (int a : vc) {
    if (done[a])
      continue;
    done[a] = true;
    bool cl = true;
    for (int v : G[a]) {
      if (done[v])
        continue;
      if (deg[v] != deg[a])
        cl = false;
      deg[v]--;
    }
    if (cl) {
      int cnt = 1;
      done[a] = true;
      for (int v : G[a]) {
        if (done[v])
          continue;
        done[v] = true;
        cnt++;
      }
      ans++;
      num = num * cnt % mod;
    }
  }
  cout << ans << endl << num << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef long long ll;
ll mod = 1e9 + 9;
int N, M;
int ans = 0;
ll cnt = 1;
set<int> G[100001], tmp;
bool visited[100001] = {};
void dfs2(int v) {
  visited[v] = 1;
  tmp.insert(v);
  for (int u : G[v]) {
    if (!visited[u])
      dfs2(u);
  }
}
void dfs(set<int> vc) {
  //	cout<<"vc   ";
  //	for(int a:vc) cout<<a<<" ";
  //	puts("");
  ll s = vc.size();
  if (s == 1) {
    ans++;
    return;
  }
  if (s == 2) {
    ans++;
    cnt *= 2;
    if (cnt >= mod)
      cnt -= mod;
    return;
  }
  int u = -1;
  bool cl = true;
  for (int v : vc) {
    if (G[v].size() == s - 1 && u == -1)
      u = v;
    if (G[v].size() < s - 1)
      cl = false;
  }
  if (cl) {
    ans++;
    cnt *= s;
    cnt %= mod;
    return;
  }
  assert(u != -1);
  for (int v : G[u])
    G[v].erase(u);
  G[u].clear();
  vc.erase(u);
  vector<set<int>> sets;
  for (int v : vc)
    visited[v] = 0;
  for (int v : vc) {
    if (visited[v])
      continue;
    dfs2(v);
    sets.pb(tmp);
    tmp.clear();
  }
  for (auto it : sets)
    dfs(it);
}
int main() {
  cin >> N >> M;
  if ((ll)N * (N - 1) / 2 == M) {
    cout << 1 << endl << N << endl;
    return 0;
  }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    G[x].insert(y);
    G[y].insert(x);
  }
  rep1(i, N) {
    G[0].insert(i);
    G[i].insert(0);
  }
  set<int> be;
  rep(i, N + 1) be.insert(i);
  dfs(be);
  cout << ans << endl << cnt << endl;
}