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++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
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;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
struct edge {
  long long to, cost;
};
using P = pair<int, int>;
int V;
const int MAX_V = 1e5 + 1;

vector<int> G[MAX_V];
vector<long long> d(MAX_V, INT_MAX);
int N, M;
vint ans(MAX_V, -1);
vint to(MAX_V, -1);
void dijkstra(int start) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    for (int i = 0; i < G[v].size(); i++) {
      int e = G[v][i];
      if (d[e] > d[v] + 1) {
        d[e] = d[v] + 1;
        to[e] = v;
        que.push(P(d[e], e));
      }
    }
  }
}
void dfs(int now) {
  if (ans[now] != -1)
    return;
  for (auto &&i : G[now]) {
    if (d[now] > d[i]) {
      ans[now] = i;
    }
  }
}
int main() {

  cin >> N >> M;
  //    vvint graph(N);
  int a, b;
  for (size_t i = 0; i < M; i++) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dijkstra(0);
  for (size_t i = 0; i < N; i++) {
    //       dfs(i);
  }
  cout << "Yes" << endl;
  for (size_t i = 1; i < N; i++) {
    cout << to[i] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
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;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
struct edge {
  long long to, cost;
};
using P = pair<int, int>;
int V;
const int MAX_V = 1e5 + 1;

vector<int> G[MAX_V];
vector<long long> d(MAX_V, INT_MAX);
int N, M;
vint ans(MAX_V, -1);
void dijkstra(int start) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    for (int i = 0; i < G[v].size(); i++) {
      int e = G[v][i];
      if (d[e] > d[v] + 1) {
        d[e] = d[v] + 1;
        que.push(P(d[e], e));
      }
    }
  }
}
void dfs(int now, int to = -1) {
  if (ans[now] != -1)
    return;
  if (to == -1)
    ans[now] = 0;
  else
    ans[now] = to;

  for (auto &&i : G[now]) {
    if (to == -1 or d[now] > d[to])
      dfs(now, i);
  }
}
void dfs2(int now) {
  if (ans[now] != -1)
    return;
  int to = 0;

  for (auto &&i : G[now]) {
    if (d[now] > d[i]) {
      ans[now] = i;
      dfs2(i);
    }
  }
}
int main() {

  cin >> N >> M;
  //    vvint graph(N);
  int a, b;
  for (size_t i = 0; i < M; i++) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dijkstra(0);
  for (size_t i = 0; i < N; i++) {
    dfs2(i);
  }
  cout << "Yes" << endl;
  for (size_t i = 1; i < N; i++) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}