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

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#define _GLIBCXX_DEBUG
#define INF 100000010
#define PI 3.14159265359

using namespace std;
const int mod = 1000000007;
const int tmpMod = 998244353;

int main() {

  int64_t N, M;
  cin >> N >> M;
  vector<vector<int>> A(N + 1, vector<int>(0));
  vector<int> B(N + 1, -1), next(0), empty(0);
  int visited = 0;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    A[a].push_back(b);
    A[b].push_back(a);
    // 1に繋がってるやつをnextに入れる
    if (a == 1 || b == 1) {

      if (a != 1 && B[a] == -1) {
        B[a] = 1;
        next.push_back(a);
        visited++;
      } else if (B[b] == -1) {
        B[b] = 1;
        next.push_back(b);
        visited++;
      }
    }
  }

  while (visited < N) {
    vector<int> next2 = next;
    next = empty;
    // nextに入ってた洞窟に繋がってる洞窟にその数字をつけてnextにいれる
    for (int i = 0; i < next2.size(); i++) {
      for (int j = 0; j < A[next2[i]].size(); j++) {
        if (B[A[next2[i]][j]] == -1) {
          B[A[next2[i]][j]] = next2[i];
          // nextに入れる
          next.push_back(A[next2[i]][j]);
          visited++;
        }
      }
      if (visited == N)
        goto end;
    }
  }
end:
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << B[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#define _GLIBCXX_DEBUG
#define INF 100000010
#define PI 3.14159265359

using namespace std;
const int mod = 1000000007;
const int tmpMod = 998244353;

int main() {

  int64_t N, M;
  cin >> N >> M;
  vector<vector<int>> A(N + 1, vector<int>(0));
  vector<int> B(N + 1, -1);
  vector<bool> C(N + 1, false);
  vector<int> next(0);
  vector<int> emp(0);
  int visited = 0;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    A[a].push_back(b);
    A[b].push_back(a);
    if (a == 1 || b == 1) {
      if (a != 1) {
        B[a] = 1;
        next.push_back(a);
      } else {
        B[b] = 1;
        next.push_back(b);
      }
      visited++;
    }
  }

  while (visited < N) {
    vector<int> next2 = next;
    next = emp;
    for (int i = 0; i <= next2.size(); i++) {
      for (int j = 0; j < A[next2[i]].size(); j++) {
        if (B[A[next2[i]][j]] == -1) {
          B[A[next2[i]][j]] = next2[i];
          next.push_back(A[next2[i]][j]);
          visited++;
        } // cout<<"dddd"<<visited<<endl;
      }
      if (visited == N)
        goto end;
    }
  }
end:
  // for(int i=0; i<N; i++){for(int j=0; j<M; j++){cin>>A[i][j];}}
  // sort(A.begin(),A.end());
  // for (int i = 0; i < N; i++) { maximum = max(maximum , A[i]);}
  // for (int i = 0; i < N; i++) { minimum = min(minimum , A[i]);}
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << B[i] << endl;
  }
}