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

#include <bits/stdc++.h>
using namespace std;

template <typename T> class Dijkstra {
public:
  struct edge {
    int to;
    T cost;
  };
  int V;
  vector<vector<edge>> G;
  vector<T> dist;
  using pti = pair<T, int>;
  Dijkstra(int n) : V(n), G(V), dist(V, numeric_limits<T>::max()) {}
  void add(int u, int v, T cost) {
    G.at(u).push_back((edge){v, cost});
    G.at(v).push_back((edge){u, cost});
  }
  void solve(int s) {
    priority_queue<pti, vector<pti>, greater<pti>> Q;
    dist.at(s) = 0;
    Q.push(pti(0, s));
    while (!Q.empty()) {
      pti p = Q.top();
      Q.pop();
      int v = p.second;
      if (dist.at(v) < p.first)
        continue;
      for (auto &w : G.at(v)) {
        if (dist.at(w.to) > dist.at(v) + w.cost) {
          dist.at(w.to) = dist.at(v) + w.cost;
          Q.push(pti(dist.at(w.to), w.to));
        }
      }
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  Dijkstra<long> D(N);
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    D.add(--a, --b, 1);
    G.at(a).push_back(b);
    G.at(b).push_back(a);
  }
  D.solve(0);
  for (int i = 0; i < N; i++) {
    if (D.dist.at(i) == LONG_MAX)
      return cout << "No"
                  << "\\n",
             0;
  }
  cout << "Yes"
       << "\\n";
  for (int i = 1; i < N; i++) {
    int res = G.at(i).at(0);
    for (auto g : G.at(i))
      if (D.dist.at(g) < D.dist.at(res))
        res = g;
    cout << res + 1 << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T> class Dijkstra {
public:
  struct edge {
    int to;
    T cost;
  };
  int V;
  vector<vector<edge>> G;
  vector<T> dist;
  using pti = pair<T, int>;
  Dijkstra(int n) : V(n), G(V), dist(V, numeric_limits<T>::max()) {}
  void add(int u, int v, T cost) {
    G.at(u).push_back((edge){v, cost});
    G.at(v).push_back((edge){u, cost});
  }
  void solve(int s) {
    priority_queue<pti, vector<pti>, greater<pti>> Q;
    dist.at(s) = 0;
    Q.push(pti(0, s));
    while (!Q.empty()) {
      pti p = Q.top();
      Q.pop();
      int v = p.second;
      if (dist.at(v) < p.first)
        continue;
      for (auto &w : G.at(v)) {
        if (dist.at(w.to) > dist.at(v) + w.cost) {
          dist.at(w.to) = dist.at(v) + w.cost;
          Q.push(pti(dist.at(w.to), w.to));
        }
      }
    }
  }
};

// Dijkstra<long> D(N)  … N: ノード数。ジェネリクスの指定をする
// D.add(a, b, c) … 閉区間で指定。無向グラフの場合↑のコメント解除
// D.solve(a) … a から各ノード(i)への最短距離が D.dist.at(i) に格納される
//  → 辿り着けないなら LONG_MAX

int main() {
  int N, M;
  cin >> N >> M;
  Dijkstra<long> D(N);
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    D.add(--a, --b, 1);
    G.at(a).push_back(b);
    G.at(b).push_back(a);
  }
  vector<long> ans(N);
  for (int i = 1; i < N; i++) {
    Dijkstra<long> tmp = D;
    tmp.solve(i);
    if (tmp.dist.at(0) == LONG_MAX)
      return cout << "No"
                  << "\\n",
             0;
    ans.at(i) = tmp.dist.at(0);
  }
  cout << "Yes"
       << "\\n";
  for (int i = 1; i < N; i++) {
    int res = G.at(i).at(0);
    for (auto g : G.at(i)) {
      if (ans.at(g) < ans.at(res))
        res = g;
    }
    cout << res + 1 << "\\n";
  }
}