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

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

// 比較演算子を自動生成
#define CAT(a, b) a##b
#define COMP_1(v1) return lhs.v1 < rhs.v1
#define COMP_2(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_1(__VA_ARGS__)
#define COMP_3(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_2(__VA_ARGS__)
#define COMP_4(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_3(__VA_ARGS__)
#define COMP_5(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_4(__VA_ARGS__)
#define LESS_THAN(A, n, ...)                                                   \\
  bool operator<(const A &rhs) const {                                         \\
    auto &lhs = *this;                                                         \\
    CAT(COMP_, n)(__VA_ARGS__);                                                \\
  }

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define FOR(i, s, n) for (ll i = (s); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()

// 10^x (by math)
#define TEN(x) ((ll)1e##x)

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;

void solution(istream &cin, ostream &cout) {
  ll n, m;
  cin >> n >> m;
  vvl g(n);
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  auto dijk = [](vvl &g) {
    struct P {
      ll dist, prev_id;
    };
    struct Q {
      ll dist, id, prev_id;
      bool operator<(const Q &rhs) const {
        if (dist != rhs.dist)
          return dist > rhs.dist;
        if (id != rhs.id)
          return id > rhs.id;
        return prev_id > rhs.prev_id;
      }
    };

    vector<P> ret(g.size(), P{-1, -1});
    ret[0] = P{0, -1};
    priority_queue<Q> q;
    q.push(Q{0, 0, -1});
    while (!q.empty()) {
      auto cur = q.top();
      q.pop();
      if (ret[cur.id].prev_id != cur.prev_id)
        continue;
      for (auto next : g[cur.id]) {
        if (ret[next].dist == -1 || ret[next].dist > cur.dist + 1) {
          ret[next] = P{cur.dist + 1, cur.id};
          q.push(Q{cur.dist + 1, next, cur.id});
        }
      }
    }

    return ret;
  };
  auto dij = dijk(g);
  FOR(i, 1, n) {
    if (dij[i].dist == -1) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes\\n";
  FOR(i, 1, n) { cout << (dij[i].prev_id + 1) << "\\n"; }
}

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);

  solution(cin, cout);
}


Error: TLE

Fixed submission:

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

// 比較演算子を自動生成
#define CAT(a, b) a##b
#define COMP_1(v1) return lhs.v1 < rhs.v1
#define COMP_2(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_1(__VA_ARGS__)
#define COMP_3(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_2(__VA_ARGS__)
#define COMP_4(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_3(__VA_ARGS__)
#define COMP_5(v1, ...)                                                        \\
  if (lhs.v1 != rhs.v1)                                                        \\
    COMP_1(v1);                                                                \\
  COMP_4(__VA_ARGS__)
#define LESS_THAN(A, n, ...)                                                   \\
  bool operator<(const A &rhs) const {                                         \\
    auto &lhs = *this;                                                         \\
    CAT(COMP_, n)(__VA_ARGS__);                                                \\
  }

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define FOR(i, s, n) for (ll i = (s); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()

// 10^x (by math)
#define TEN(x) ((ll)1e##x)

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;

void solution(istream &cin, ostream &cout) {
  ll n, m;
  cin >> n >> m;
  vvl g(n);
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  auto dijk = [](vvl &g) {
    struct P {
      ll dist, prev_id;
    };
    struct Q {
      ll dist, id, prev_id;
      bool operator<(const Q &rhs) const {
        if (dist != rhs.dist)
          return dist < rhs.dist;
        if (id != rhs.id)
          return id < rhs.id;
        return prev_id < rhs.prev_id;
      }
    };

    vector<P> ret(g.size(), P{-1, -1});
    ret[0] = P{0, -1};
    priority_queue<Q> q;
    q.push(Q{0, 0, -1});
    while (!q.empty()) {
      auto cur = q.top();
      q.pop();
      if (ret[cur.id].prev_id != cur.prev_id)
        continue;
      for (auto next : g[cur.id]) {
        if (ret[next].dist == -1 || ret[next].dist > cur.dist + 1) {
          ret[next] = P{cur.dist + 1, cur.id};
          q.push(Q{cur.dist + 1, next, cur.id});
        }
      }
    }

    return ret;
  };
  auto dij = dijk(g);
  FOR(i, 1, n) {
    if (dij[i].dist == -1) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes\\n";
  FOR(i, 1, n) { cout << (dij[i].prev_id + 1) << "\\n"; }
}

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);

  solution(cin, cout);
}