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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>

using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (LL i = 0; i < (LL)(n); i++)

void checkRoute(int step, int start, VI &route,
                map<int, unordered_set<int>> AB) {
  for (const auto elem : AB[start]) {
    if (elem < 2 || step >= route[elem - 2]) {
      continue;
    }
    route[elem - 2] = start;
    checkRoute(step + 1, elem, route, AB);
  }
  return;
}

int main(void) {
  int N, M;
  cin >> N >> M;
  map<int, unordered_set<int>> AB;
  VI route(N, -1);

  rep(i, M) {
    int j, k;
    cin >> j >> k;
    j--;
    k--;
    AB[j].insert(k);
    AB[k].insert(j);
  }

  cout << "Yes" << endl;
  queue<pair<int, int>> que;
  que.emplace(0, -2);
  while (!que.empty()) {
    auto [v, p] = que.front();
    que.pop();
    if (route[v] != -1)
      continue;
    route[v] = p;
    for (auto to : AB[v]) {
      if (to == p)
        continue;
      if (route[to] != -1)
        continue;
      que.emplace(to, v);
    }
  }

  for (int i = 1; i < N; i++) {
    cout << route[i] + 1 << endl;
  }

  return 0;
};

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>

using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (LL i = 0; i < (LL)(n); i++)

void checkRoute(int step, int start, vector<PII> &route,
                map<int, unordered_set<int>> AB) {
  for (const auto elem : AB[start]) {
    if (elem < 2 || step >= route[elem - 2].first) {
      continue;
    }
    route[elem - 2].first = step;
    route[elem - 2].second = start;
    checkRoute(step + 1, elem, route, AB);
  }
  return;
}

int main(void) {
  int N, M;
  cin >> N >> M;
  map<int, unordered_set<int>> AB;
  vector<PII> route(N - 1, make_pair(N + 1, 0));

  rep(i, M) {
    int j, k;
    cin >> j >> k;
    AB[j].insert(k);
    AB[k].insert(j);
  }

  cout << "Yes" << endl;
  int start = 1;
  int step = 1;
  checkRoute(step, start, route, AB);

  rep(i, N - 1) { cout << route[i].second << endl; }

  return 0;
};