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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N, M;
std::vector<std::vector<ll>> G(100010, std::vector<ll>());
bool used[100010];
ll prv[100010], dprv[100010], d[100010];

int main() {
  std::cin >> N >> M;

  rep(i, M) {
    ll A, B;
    std::cin >> A >> B;
    --A;
    --B;

    G[A].emplace_back(B);
    G[B].emplace_back(A);
  }

  std::queue<P> que;
  que.emplace(0, 0);

  rep(i, N + 5) prv[i] = -1, d[i] = -1;

  rep(i, N + 5) used[i] = false;

  d[0] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    ll v = p.first, dist = p.second;

    // std::cout << p << std::endl;

    for (auto u : G[v])
      if (d[u] == -1) {
        d[u] = d[v] + 1;
        que.emplace(u, dist + 1);

        if (prv[u] == -1)
          prv[u] = v, dprv[u] = dist;
      }
  }

  repi(i, 1, N) if (prv[i] == -1) {
    std::cout << "No" << std::endl;

    return 0;
  }

  std::cout << "Yes" << std::endl;

  repi(i, 1, N) std::cout << prv[i] + 1 << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N, M;
std::vector<std::vector<ll>> G(100010, std::vector<ll>());
bool used[100010];
ll prv[100010], dprv[100010];

int main() {
  std::cin >> N >> M;

  rep(i, M) {
    ll A, B;
    std::cin >> A >> B;
    --A;
    --B;

    G[A].emplace_back(B);
    G[B].emplace_back(A);
  }

  std::queue<P> que;
  que.emplace(0, 0);

  rep(i, N + 5) prv[i] = -1;

  rep(i, N + 5) used[i] = false;

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    ll v = p.first, dist = p.second;

    used[v] = true;

    // std::cout << p << std::endl;

    for (auto u : G[v])
      if (!used[u]) {
        que.emplace(u, dist + 1);

        if (prv[u] == -1)
          prv[u] = v, dprv[u] = dist;
      }
  }

  repi(i, 1, N) if (prv[i] == -1) {
    std::cout << "No" << std::endl;

    return 0;
  }

  std::cout << "Yes" << std::endl;

  repi(i, 1, N) std::cout << prv[i] + 1 << std::endl;

  return 0;
}