Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 998244353;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;
// typedef boost::multiprecision::cpp_int bigint;

template <class T> struct Gedge {
  LL src, to;
  T cost;
  Gedge(LL s, LL t, T c) : src(s), to(t), cost(c) {}
  Gedge(LL t, T c) : src(-1), to(t), cost(c) {}
};

template <class T> using WeightedGraph = vector<vector<Gedge<T>>>;
using UnweightedGraph = vector<vector<LL>>;
template <class T> using Gedges = vector<Gedge<T>>;

struct TNode {
  LL parent;
  VLL childs;
  TNode() : parent(-1){};
};
using Tree = vector<TNode>;

// 到達不可能->INF
template <class T>
void dijkstra(WeightedGraph<T> &G, LL start, vector<T> &res) {
  res.resize(G.size(), INF);
  res[start] = 0;
  typedef pair<T, LL> P;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, start));
  while (!q.empty()) {
    LL cur = q.top().second;
    T cost = q.top().first;
    q.pop();
    if (res[cur] < cost)
      continue;
    for (Gedge<T> e : G[cur]) {
      if (res[e.to] > cost + e.cost) {
        res[e.to] = cost + e.cost;
        q.push(P(res[e.to], e.to));
      }
    }
  }
  return;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  LL N, M;
  cin >> N >> M;
  WeightedGraph<LL> G(N);
  for (LL n = 0; n < M; n++) {
    LL a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back({a, b, 1});
    G[b].push_back({b, a, 1});
  }
  VLL res;
  dijkstra(G, 0, res);
  VLL path(N, -1);
  bool flag = false;
  for (LL n = 1; n < N; n++) {
    LL d = res[n];
    if (d == INF) {
      flag = true;
      break;
    }
    for (auto edge : G[n]) {
      if (res[edge.to] == d - 1) {
        path[n] = edge.to;
        break;
      }
    }
  }
  if (flag)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    for (LL n = 1; n < N; n++) {
      cout << path[n] + 1 << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 998244353;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;
// typedef boost::multiprecision::cpp_int bigint;

template <class T> struct Gedge {
  LL src, to;
  T cost;
  Gedge(LL s, LL t, T c) : src(s), to(t), cost(c) {}
  Gedge(LL t, T c) : src(-1), to(t), cost(c) {}
};

template <class T> using WeightedGraph = vector<vector<Gedge<T>>>;
using UnweightedGraph = vector<vector<LL>>;
template <class T> using Gedges = vector<Gedge<T>>;

struct TNode {
  LL parent;
  VLL childs;
  TNode() : parent(-1){};
};
using Tree = vector<TNode>;

// 到達不可能->INF
template <class T>
void dijkstra(WeightedGraph<T> &G, LL start, vector<T> &res) {
  res.resize(G.size(), INF);
  res[start] = 0;
  typedef pair<T, LL> P;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, start));
  while (!q.empty()) {
    LL cur = q.top().second;
    T cost = q.top().first;
    q.pop();
    if (res[cur] < cost)
      continue;
    for (Gedge<T> e : G[cur]) {
      if (res[e.to] > cost + e.cost) {
        res[e.to] = cost + e.cost;
        q.push(P(res[e.to], e.to));
      }
    }
  }
  return;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  LL N, M;
  cin >> N >> M;
  WeightedGraph<LL> G(N);
  for (LL n = 0; n < N; n++) {
    LL a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back({a, b, 1});
    G[b].push_back({b, a, 1});
  }
  VLL res;
  dijkstra(G, 0, res);
  VLL path(N, -1);
  bool flag = false;
  for (LL n = 1; n < N; n++) {
    LL d = res[n];
    if (d == INF) {
      flag = true;
      break;
    }
    for (auto edge : G[n]) {
      if (res[edge.to] == d - 1) {
        path[n] = edge.to;
        break;
      }
    }
  }
  if (flag)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    for (LL n = 1; n < N; n++) {
      cout << path[n] + 1 << "\\n";
    }
  }
  return 0;
}