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

#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
struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int MAX_V = 1 << 18, INF = 1 << 30;
int V, E, d[MAX_V], r, s, t, c, pre[MAX_V];
vector<edge> G[MAX_V];
priority_queue<P, vector<P>, greater<P>> q;

void dijkstra(int s) {
  fill(d, d + V, INF);
  fill(pre, pre + V, -1);
  d[s] = 0;
  q.push(P(0, s));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < (int)G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pre[e.to] = v;
        q.push(P(d[e.to], e.to));
      }
    }
  }
}
vector<int> get_path(int t) {
  vector<int> path;
  for (; t != -1; t = pre[t])
    path.push_back(t);
  return path;
}
inline void solve(void) {
  scanf("%d %d", &V, &E);
  for (int i = 0; i < E; i++) {
    scanf("%d %d", &s, &t);
    edge e;
    s--;
    t--;
    if (s < t)
      swap(s, t);
    e.to = t;
    e.cost = 1;
    G[s].push_back(e);
    e.to = s;
    G[t].push_back(e);
  }
  vector<int> ans;
  dijkstra(0);
  up(i, 1, V - 1) {
    if (d[0] != INF) {
      ans.pb(pre[i]);
    } else {
      out("No");
      return;
    }
  }
  out("Yes");
  for (auto e : ans)
    out(e + 1);
}
int main(void) {
  BEGIN;
  solve();
  END;
}


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
struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int MAX_V = 1 << 18, INF = 1 << 30;
int V, E, d[MAX_V], r, s, t, c, pre[MAX_V];
vector<edge> G[MAX_V];
priority_queue<P, vector<P>, greater<P>> q;

void dijkstra(int s) {
  fill(d, d + V, INF);
  fill(pre, pre + V, -1);
  d[s] = 0;
  q.push(P(0, s));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < (int)G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pre[e.to] = v;
        q.push(P(d[e.to], e.to));
      }
    }
  }
}
vector<int> get_path(int t) {
  vector<int> path;
  for (; t != -1; t = pre[t])
    path.push_back(t);
  return path;
}
inline void solve(void) {
  scanf("%d %d", &V, &E);
  for (int i = 0; i < E; i++) {
    scanf("%d %d", &s, &t);
    edge e;
    s--;
    t--;
    if (s < t)
      swap(s, t);
    e.to = t;
    e.cost = 1;
    G[s].push_back(e);
    e.to = s;
    G[t].push_back(e);
  }
  vector<int> ans;
  dijkstra(0);
  up(i, 1, V - 1) {
    if (d[0] != INF) {
      auto path = get_path(i);
      ans.pb(path[1]);
    } else {
      out("No");
      return;
    }
  }
  out("Yes");
  for (auto e : ans)
    out(e + 1);
}
int main(void) {
  BEGIN;
  solve();
  END;
}