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

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(A) A.begin(), A.end()
bool _predicate(int a, const int &element) {
  return (
      element ==
      a); // This will cause all elements to be deleted that are larger than 3
}
int main() {
  int n, m;
  cin >> n >> m;
  // vector<P> path(m);
  vector<vector<int>> path(n, vector<int>());
  int a, b;

  rep(i, m) {
    // cin >> path[i].first >> path[i].second;
    cin >> a >> b;
    a--;
    b--;
    path[a].push_back(b);
    path[b].push_back(a);
    // cout << a << b << endl;
  }
  /*rep(i, n) {
          cout << i << " ;" << endl;
          for (int j : path[i]) {
                  cout << j << " ";
          }
          cout << endl;
  }*/
  a = 0;
  // vector<bool> hasgone(m, false);
  vector<P> hasgone(n, make_pair(-1, INT_MAX));
  hasgone[0] = make_pair(-1, 0);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    a = p.second;
    // cout << a << endl;
    if (hasgone[a].second < p.first)
      continue;
    for (int i : path[a]) {
      // if (hasgone[i].first == -1)visited.push_back(i);
      if (hasgone[i].second > hasgone[a].second + 1) {

        // cout <<i<<" "<<a<<" "<< hasgone[i].second <<" "<< hasgone[a].second +
        // 1 << endl;
        hasgone[i] = make_pair(a, hasgone[a].second + 1);
        que.push(P(hasgone[i].second, i));
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << hasgone[i].first + 1 << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(A) A.begin(), A.end()
bool _predicate(int a, const int &element) {
  return (
      element ==
      a); // This will cause all elements to be deleted that are larger than 3
}
int main() {
  int n, m;
  cin >> n >> m;
  // vector<P> path(m);
  vector<vector<int>> path(m, vector<int>());
  int a, b;

  rep(i, m) {
    // cin >> path[i].first >> path[i].second;
    cin >> a >> b;
    a--;
    b--;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  a = 0;
  // vector<bool> hasgone(m, false);
  vector<P> hasgone(m, make_pair(-1, INT_MAX));
  hasgone[0] = make_pair(-1, 0);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    a = p.second;
    if (hasgone[a].second < p.first)
      continue;
    for (int i : path[a]) {
      // if (hasgone[i].first == -1)visited.push_back(i);
      if (hasgone[i].second > hasgone[a].second + 1) {

        // cout <<i<<" "<<a<<" "<< hasgone[i].second <<" "<< hasgone[a].second +
        // 1 << endl;
        hasgone[i] = make_pair(a, hasgone[a].second + 1);
        que.push(P(hasgone[i].second, i));
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << hasgone[i].first + 1 << endl;
  }
}