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

#include <bits/stdc++.h>

using namespace std;

bool operator<(const pair<int, int> a, const pair<int, int> b) {
  return a.first < b.first;
};
// pair の1つ目の要素に従って昇順に処理したい
bool operator>(const pair<int, int> a, const pair<int, int> b) {
  return a.first > b.first;
};
// pair の2つ目の要素に従って降順に処理したい
//  bool operator< (const pair<int, int> a, const pair<int, int> b){return
//  a.second < b.second;};
//  //pair の2つ目の要素に従って昇順に処理したい
//  bool operator> (const pair<int, int> a, const pair<int, int> b){return
//  a.second > b.second;};

void dijkstra(int initial, vector<bool> &visit, vector<int> &D,
              vector<int> &mother, vector<vector<int>> A) {

  D[initial] = 0;
  visit[initial] = 1;

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      S;

  pair<int, int> p = make_pair(D[initial], initial);

  S.push(p);

  while (!S.empty()) {
    int w = S.top().second;

    S.pop();

    if (A[w].size() > 0) {
      for (int v : A[w]) {

        if (visit[v] == 0) {

          visit[v] = 1;
        }

        if (D[w] + 1 < D[v]) {

          D[v] = D[w] + 1;

          pair<int, int> new_pair = make_pair(D[v], v);
          S.push(new_pair);

          mother[v] = w;
        }
      }
    }
  }
}

int main() {

  const int INF = 1001001001;

  int N, M;

  cin >> N >> M;

  // graph
  vector<vector<int>> A(N, vector<int>(0));

  // aint eady visit?
  vector<bool> visit(N, 0);

  // mother room of the room
  vector<int> mother(N);

  // cost array
  vector<int> D(N, INF);

  for (int i = 0; i < M; i++) {

    int b, c;

    cin >> b >> c;

    A[b - 1].push_back(c - 1);
    A[c - 1].push_back(b - 1);
  }
  // initialization
  dijkstra(0, visit, D, mother, A);

  for (int i = 0; i < N; i++) {
    if (visit[i] == 0) {
      cout << "No" << endl;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {

    cout << mother[i] + 1 << endl;
  }

  return 0;
}

// #include <bits/stdc++.h>
// #define rep(i,n) for (int i = 0; i < (n); ++i)
// using namespace std;
// using ll = long long;
// using P = pair<int,int>;

// const int INF = 1001001001;
// vector<int> to[100005];

// int main() {
//   int n, m;
//   cin >> n >> m;
//   rep(i,m) {
//     int a, b;
//     cin >> a >> b;
//     --a; --b;
//     to[a].push_back(b);
//     to[b].push_back(a);
//   }

//   queue<int> q;
//   vector<int> dist(n, INF);
//   vector<int> pre(n, -1);
//   dist[0] = 0;
//   q.push(0);
//   while (!q.empty()) {
//     int v = q.front(); q.pop();
//     for (int u : to[v]) {
//       if (dist[u] != INF) continue;
//       dist[u] = dist[v]+1;
//       pre[u] = v;
//       q.push(u);
//     }
//   }

//   cout << "Yes" << endl;
//   rep(i,n) {
//     if (i == 0) continue;
//     int ans = pre[i];
//     ++ans;
//     cout << ans << endl;
//   }
//   return 0;
// }


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

class CompareFirst_koh {
public:
  bool operator()(pair<int, int> n1, pair<int, int> n2) {
    return n1.first > n2.first; //>:降順　pairのfirst同士を比較する
  }
};

class CompareFirst_shoh {
public:
  bool operator()(pair<int, int> n1, pair<int, int> n2) {
    return n1.first < n2.first; //>:降順　pairのfirst同士を比較する
  }
};

void dijkstra(int initial, vector<bool> &visit, vector<int> &D,
              vector<int> &mother, vector<vector<int>> A) {

  D[initial] = 0;
  visit[initial] = 1;

  priority_queue<pair<int, int>, vector<pair<int, int>>, CompareFirst_koh> S;

  pair<int, int> p = make_pair(D[initial], initial);

  S.push(p);

  while (!S.empty()) {

    int costs = INFINITY;
    int w;

    w = S.top().second;

    S.pop();

    if (A.size() > 0) {
      for (int i = 0; i < A.size(); i++) {

        if (visit[A[w][i]] == 0) {
          pair<int, int> new_pair = make_pair(D[A[w][i]], A[w][i]);
          S.push(new_pair);
          visit[A[w][i]] = 1;
        }

        if (D[w] + 1 < D[A[w][i]]) {

          D[A[w][i]] = D[A[w][i]] + 1;

          mother[A[w][i]] = w;
        }
      }
    }
  }
}

int main() {

  int N, M;

  cin >> N >> M;

  // graph
  vector<vector<int>> A(N, vector<int>(0));

  // aint eady visit?
  vector<bool> visit(N, 0);

  // mother room of the room
  vector<int> mother(N);

  // cost array
  vector<int> D(N, INFINITY);

  for (int i = 0; i < M; i++) {

    int b, c;

    cin >> b >> c;

    A[b - 1].push_back(c - 1);
    A[c - 1].push_back(b - 1);
  }

  // initialization

  dijkstra(0, visit, D, mother, A);

  for (int i = 0; i < N; i++) {

    if (visit[i] == 0) {

      cout << "No" << endl;

      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {

    cout << mother[i] + 1 << endl;
  }

  return 0;
}