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

#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

template <class T> void print_vect(std::vector<T> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}

void func_to_create() {
  uint32_t N, M;
  cin >> N >> M;
  std::vector<std::vector<int>> passage(N);
  std::vector<uint32_t> ans(N, 0);
  std::vector<bool> visit(N, false);
  visit[0] = true;
  uint32_t count_visit = 0;
  std::vector<uint32_t> current_seed;
  for (uint32_t i = 0; i < M; i++) {
    uint32_t A, B;
    cin >> A >> B;
    passage[A - 1].push_back(B);
    passage[B - 1].push_back(A);
    if (A == 1) {
      ans[B - 1] = 1;
      current_seed.push_back(B);
      visit[B - 1] = true;
    } else if (B == 1) {
      ans[A - 1] = 1;
      current_seed.push_back(A);
      visit[A - 1] = true;
    }
  }
  while (true) {
    // cout << "current seed ";
    // print_vect(current_seed);
    std::vector<uint32_t> next_seed;
    for (const auto &seed : current_seed) {
      for (const auto &city : passage[seed - 1]) {
        if (visit[city - 1] == false) {
          visit[city - 1] = true;
          next_seed.push_back(city);
          ans[city - 1] = seed;
        }
      }
    }
    if (next_seed.size() == 0) {
      break;
    }
    current_seed = next_seed;
  }

  for (uint32_t i = 0; i < visit.size(); i++) {
    count_visit += visit[i];
  }

  if (count_visit == N) {
    cout << "Yes" << endl;
    for (uint32_t i = 1; i < N; i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  /****************************/

#ifdef DEBUG
  int TEST_CASES = 2;
  while (TEST_CASES--) {
#endif // DEBBUG

    func_to_create();

#ifdef DEBUG
  }
#endif // DEBUG

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

template <class T> void print_vect(std::vector<T> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}

void func_to_create() {
  uint32_t N, M;
  cin >> N >> M;
  std::vector<std::bitset<100000>> hardmap(N);
  std::vector<uint32_t> ans(N, 0);
  std::vector<uint32_t> current_seed;
  std::list<uint32_t> remain_city;
  for (uint32_t i = 0; i < M; i++) {
    uint32_t A, B;
    cin >> A >> B;
    hardmap[A - 1].set(B - 1);
    hardmap[B - 1].set(A - 1);
    if (A == 1) {
      ans[B - 1] = 1;
      current_seed.push_back(B);
    } else if (B == 1) {
      ans[A - 1] = 1;
      current_seed.push_back(A);
    }
  }
  for (uint32_t i = 1; i < N; i++) {
    if (ans[i] == 0) {
      remain_city.push_back(i + 1);
    }
  }
  // TO-DO: find out why this implementation is so slow!!
  while (true) {
    std::vector<uint32_t> next_seed;
    for (auto it_city = remain_city.begin(); it_city != remain_city.end();) {
      auto city = *it_city;
      bool found = false;
      for (int i = 0; i < current_seed.size(); i++) {
        if (hardmap[city - 1][current_seed[i] - 1]) {
          ans[city - 1] = current_seed[i];
          next_seed.push_back(city);
          remain_city.erase(it_city++);
          found = true;
          break;
        }
      }
      if (found == false) {
        ++it_city;
      }
    }
    if (next_seed.size() == 0) {
      break;
    }
    current_seed.swap(next_seed);
  }
  if (remain_city.size() == 0) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  /****************************/

#ifdef DEBUG
  int TEST_CASES = 2;
  while (TEST_CASES--) {
#endif // DEBBUG

    func_to_create();

#ifdef DEBUG
  }
#endif // DEBUG

  return 0;
}