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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template <class T> inline T sqr(T x) { return x * x; }

void solve() {
  int n, m;
  cin >> n >> m;
  VVI es(n);
  int a, b;
  REP(i, 0, m) {
    cin >> a >> b;
    --a;
    --b;
    es[a].push_back(b);
    es[b].push_back(a);
  }
  VI par(n, -1);
  deque<PII> q;
  q.pb(0, -1);
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    int idx = p.first, prev = p.second;
    // cerr << idx << ", " << prev << endl;
    // if (par[idx] == -1) par[idx] = prev;
    for (int dst : es[idx]) {
      if (dst == prev)
        continue;
      if (par[dst] > -1)
        continue;
      par[dst] = idx;
      q.pb(dst, idx);
    }
  }
  bool bo = false;
  REP(i, 1, n) if (par[i] == -1) {
    bo = true;
    break;
  }
  if (bo) {
    cout << "No" << endl;
    return;
  }
  cout << "Yes" << endl;
  REP(i, 1, n) cout << par[i] + 1 << endl;
}

signed main() {
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template <class T> inline T sqr(T x) { return x * x; }

void solve() {
  int n, m;
  cin >> n >> m;
  VVI es(n);
  int a, b;
  REP(i, 0, m) {
    cin >> a >> b;
    --a;
    --b;
    es[a].push_back(b);
    es[b].push_back(a);
  }
  VI par(n, -1);
  deque<PII> q;
  q.pb(0, -1);
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    int idx = p.first, prev = p.second;
    // cerr << idx << ", " << prev << endl;
    if (par[idx] == -1)
      par[idx] = prev;
    for (int dst : es[idx]) {
      if (dst == prev)
        continue;
      if (par[dst] > -1)
        continue;
      q.pb(dst, idx);
    }
  }
  bool bo = false;
  REP(i, 1, n) if (par[i] == -1) {
    bo = true;
    break;
  }
  if (bo) {
    cout << "No" << endl;
    return;
  }
  cout << "Yes" << endl;
  REP(i, 1, n) cout << par[i] + 1 << endl;
}

signed main() {
  solve();

  return 0;
}