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

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;
using p = std::pair<int, int>;
ll n, m;
std::vector<std::vector<int>> f;
std::set<p> check;

void dfs(int u, std::vector<int> &cnt, int color, int pre = -1) {
  cnt[u] = color;
  for (int i = 0; i < f[u].size(); i++) {
    int next = f[u][i];
    if (next != pre && cnt[next] == -1) {
      dfs(next, cnt, color, u);
    }
  }
}

int main() {
  std::cin >> n >> m;
  f.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    auto iter = check.find(std::make_pair(a, b));
    if (iter == check.end()) {
      check.emplace(a, b);
      check.emplace(b, a);
      f[a].push_back(b);
      f[b].push_back(a);
    }
  }

  int color = 0;
  std::vector<int> cnt(n, -1);
  for (int i = 0; i < n; i++) {
    if (cnt[i] == -1) {
      dfs(i, cnt, color);
      color++;
    }
  }

  std::vector<std::queue<int>> group(color);
  for (int i = 0; i < n; i++) {
    group[cnt[i]].push(i);
  }

  int ans = 0;
  /*
  while(1){
      int num_group = 0;
      for(int i = 0; i < color; i++){
          if(!group[i].empty()){
              num_group++;
              group[i].pop();
          }
      }

      if(num_group == 0) break;
      ans++;
  }
  */

  while (1) {
    int num_group = 0;
    int not_zero_min = n;
    for (int i = 0; i < color; i++) {
      if (!group[i].empty()) {
        num_group++;
        not_zero_min = std::min(not_zero_min, (int)group[i].size());
      }
    }

    if (num_group == 0)
      break;
    ans += not_zero_min;
    if (num_group == 1) {
      break;
    }
    // 削りだし
    for (int i = 0; i < color; i++) {
      if (!group[i].empty()) {
        for (int j = 0; j < not_zero_min; j++) {
          group[i].pop();
        }
      }
    }
  }

  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;
using p = std::pair<int, int>;
ll n, m;
std::vector<std::vector<int>> f;
std::set<p> check;

void dfs(int u, std::vector<int> &cnt, int color, int pre = -1) {
  cnt[u] = color;
  for (int i = 0; i < f[u].size(); i++) {
    int next = f[u][i];
    if (next != pre && cnt[next] == -1) {
      dfs(next, cnt, color, u);
    }
  }
}

int main() {
  std::cin >> n >> m;
  f.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    auto iter = check.find(std::make_pair(a, b));
    if (iter == check.end()) {
      check.emplace(a, b);
      check.emplace(b, a);
      f[a].push_back(b);
      f[b].push_back(a);
    }
  }

  int color = 0;
  std::vector<int> cnt(n, -1);
  for (int i = 0; i < n; i++) {
    if (cnt[i] == -1) {
      dfs(i, cnt, color);
      color++;
    }
  }

  std::vector<std::queue<int>> group(color);
  for (int i = 0; i < n; i++) {
    group[cnt[i]].push(i);
  }

  int ans = 0;
  while (1) {
    int num_group = 0;
    for (int i = 0; i < color; i++) {
      if (!group[i].empty()) {
        num_group++;
        group[i].pop();
      }
    }

    if (num_group == 0)
      break;
    ans++;
  }

  std::cout << ans << std::endl;
  return 0;
}