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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void debug_out() { cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

template <typename T> struct Dijkstra {
private:
  struct edge {
    int to;
    T cost;
    edge(int to, T cost) : to(to), cost(cost) {}
    bool operator<(const edge &e) const { return cost > e.cost; }
  };
  int n;
  vector<vector<edge>> G;
  vector<T> dist;
  vector<int> preV;

public:
  Dijkstra(int n_) : n(n_), G(n_){};
  void add_edge(int u, int v, T c) {
    assert(u < n);
    assert(v < n);
    G[u].emplace_back(v, c);
  }
  void build(int s) {
    dist.assign(n, numeric_limits<T>::max());
    preV.assign(n, -1);
    dist[s] = 0;
    priority_queue<edge> que;
    que.push(edge(s, 0LL));
    while (!que.empty()) {
      auto p = que.top();
      que.pop();
      int v = p.to;
      if (dist[v] < p.cost)
        continue;
      for (auto e : G[v]) {
        if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = p.cost + e.cost;
          preV[e.to] = v;
          que.push(edge(e.to, dist[e.to]));
        }
      }
    }
  }
  int get_path(int to) { // 経路復元
    // vector<int> path;
    // for(; to != -1; to = preV[to]){
    //   path.emplace_back(to);
    // }
    // reverse(path.begin(), path.end());
    // return path;
    return preV[to];
  }
  T operator[](int k) {
    assert(k < n);
    return dist[k];
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int n, m;
  cin >> n >> m;
  Dijkstra<int> dijkstra(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    dijkstra.add_edge(a, b, 1);
    dijkstra.add_edge(b, a, 1);
  }
  dijkstra.build(0);
  bool ok = true;
  vector<int> ans(n);
  for (int i = 1; i < n; ++i) {
    if (dijkstra[i] == numeric_limits<int>::max()) {
      ok = false;
      break;
    }
    ans[i] = dijkstra.get_path(i) + 1;
  }
  cout << (ok ? "Yes" : "No") << endl;
  if (ok) {
    for (int i = 1; i < n; ++i) {
      cout << ans[i] << "\\n";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void debug_out() { cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

template <typename T> struct Dijkstra {
private:
  struct edge {
    int to;
    T cost;
    edge(int to, T cost) : to(to), cost(cost) {}
    bool operator<(const edge &e) const { return cost > e.cost; }
  };
  int n;
  vector<vector<edge>> G;
  vector<T> dist;
  vector<int> preV;

public:
  Dijkstra(int n_) : n(n_), G(n_){};
  void add_edge(int u, int v, T c) {
    assert(u < n);
    assert(v < n);
    G[u].emplace_back(v, c);
  }
  void build(int s) {
    dist.assign(n, numeric_limits<T>::max());
    preV.assign(n, -1);
    dist[s] = 0;
    priority_queue<edge> que;
    que.push(edge(s, 0LL));
    while (!que.empty()) {
      auto p = que.top();
      que.pop();
      int v = p.to;
      if (dist[v] < p.cost)
        continue;
      for (auto e : G[v]) {
        if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = p.cost + e.cost;
          preV[e.to] = v;
          que.push(edge(e.to, dist[e.to]));
        }
      }
    }
  }
  vector<int> get_path(int to) { // 経路復元
    vector<int> path;
    for (; to != -1; to = preV[to]) {
      path.emplace_back(to);
    }
    reverse(path.begin(), path.end());
    return path;
  }
  T operator[](int k) {
    assert(k < n);
    return dist[k];
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int n, m;
  cin >> n >> m;
  Dijkstra<int> dijkstra(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    dijkstra.add_edge(a, b, 1);
    dijkstra.add_edge(b, a, 1);
  }
  dijkstra.build(0);
  bool ok = true;
  vector<int> ans(n);
  for (int i = 1; i < n; ++i) {
    if (dijkstra[i] == numeric_limits<int>::max()) {
      ok = false;
      break;
    }
    auto path = dijkstra.get_path(i);
    // debug(path[path.size()-2]);
    ans[i] = path[path.size() - 2] + 1;
  }
  cout << (ok ? "Yes" : "No") << endl;
  if (ok) {
    for (int i = 1; i < n; ++i) {
      cout << ans[i] << "\\n";
    }
  }
  return 0;
}