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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int n, m, v, u, p[1 << 17];
vector<int> G[1 << 17];
queue<int> q;
int main() {
  cin >> n >> m;
  rep(i, m) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  q.push(1);
  p[1] = 1;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int e : G[u]) {
      if (!p[e]) {
        q.push(e);
        p[e] = u;
      }
    }
  }
  puts("Yes");
  rep(i, n - 1) cout << p[i + 2] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace boost::multiprecision;
using namespace std;
#define BEGIN                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define END return EXIT_SUCCESS
#define up(I, A, B) for (auto I = (A); I <= (B); ++I)
#define dw(I, A, B) for (auto I = (A); I >= (B); --I)
#define rep(I, N) up(I, 0, N - 1)
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define ft first
#define sd second
#define mp make_pair
#define mt make_tuple
#define pf push_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rs resize
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
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 T> inline void in(T &p) { cin >> p; }
template <class T1, class T2> inline void in(T1 &p1, T2 &p2) {
  cin >> p1 >> p2;
}
template <class T1, class T2, class T3> inline void in(T1 &p1, T2 &p2, T3 &p3) {
  cin >> p1 >> p2 >> p3;
}
template <class T1, class T2, class T3, class T4>
inline void in(T1 &p1, T2 &p2, T3 &p3, T4 &p4) {
  cin >> p1 >> p2 >> p3 >> p4;
}
template <class T1, class T2, class T3, class T4, class T5>
inline void in(T1 &p1, T2 &p2, T3 &p3, T4 &p4, T5 &p5) {
  cin >> p1 >> p2 >> p3 >> p4 >> p5;
}
template <class T> inline void ins(T &p) {
  for_each(all(p), in<decltype(p[0])>);
}
template <class T> inline void out(T p) { cout << p << "\\n"; }
template <class T1, class T2> inline void out(T1 p1, T2 p2) {
  cout << p1 << " " << p2 << "\\n";
}
template <class T1, class T2, class T3> inline void out(T1 p1, T2 p2, T3 p3) {
  cout << p1 << " " << p2 << " " << p3 << "\\n";
}
template <class T1, class T2, class T3, class T4>
inline void out(T1 p1, T2 p2, T3 p3, T4 p4) {
  cout << p1 << " " << p2 << " " << p3 << " " << p4 << "\\n";
}
inline void out(ld p) { printf("%.12Lf\\n", p); }
template <class T> inline void outml(T p) {
  for_each(all(p), out<decltype(p[0])>);
} // ml=multi line
template <class T> inline void outol(T V) {
  auto n = V.size();
  rep(i, n) cout << V[i] << " \\n"[i == n - 1];
} // ol=one line
const int INF = 1 << 30;
int N, M, A, B, pre[1 << 18], dis[1 << 18];
vector<int> G[1 << 18];
void dfs(int now, int d) {
  dis[now] = d;
  for (auto e : G[now]) {
    if (dis[e] > dis[now] + 1) {
      pre[e] = now;
      dfs(e, d + 1);
    }
  }
}
inline void solve(void) {
  in(N, M);
  rep(i, M) {
    in(A, B);
    A--;
    B--;
    G[A].pb(B);
    G[B].pb(A);
  }
  fill(dis, dis + N, INF);
  fill(pre, pre + N, -1);
  dfs(0, 0);
  rep(i, N) {
    if (dis[i] == INF) {
      out("No");
      return;
    }
  }
  out("Yes");
  up(i, 1, N - 1) { out(pre[i] + 1); }
}
int main(void) {
  BEGIN;
  solve();
  END;
}