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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

map<int, int> depth_ans;
map<int, int> ans;
map<int, int> visited;
queue<int> que;
void saiki(vector<vector<int>> &room, int nowRoom, int depth) {
  visited[nowRoom] = 1;
  // cout << nowRoom << endl;
  for (int i = 0; i < room[nowRoom].size(); ++i) {
    int nextRoom = room[nowRoom][i];
    if (visited[nextRoom] == 0) {
      que.push(nextRoom);
      visited[nextRoom] = 1;
    }
    if (depth < depth_ans[nextRoom] || depth_ans[nextRoom] == 0) {
      ans[nextRoom] = nowRoom;
      depth_ans[nextRoom] = depth;
      // cout << nextRoom << "  " << nowRoom << endl;
    }
  }
  while (que.size() != 0) {
    int i = que.front();
    // cout << "room" << i << endl;
    que.pop();
    saiki(room, i, depth_ans[i]);
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> room(n + 1);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    room[a].push_back(b);
    room[b].push_back(a);
  }

  int nowRoom = 1;
  visited[1] = 1;
  saiki(room, nowRoom, 1);
  bool check = true;
  for (int i = 2; i < n + 1; ++i) {
    if (ans[i] == 0)
      check = false;
  }
  if (check) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; ++i) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

map<int, int> depth_ans;
map<int, int> ans;
map<int, int> visited;
queue<int> que;
void saiki(vector<vector<int>> &room, int nowRoom, int depth) {
  visited[nowRoom] = 1;
  // cout << nowRoom << endl;
  for (int i = 0; i < room[nowRoom].size(); ++i) {
    int nextRoom = room[nowRoom][i];
    if (depth < depth_ans[nextRoom] || depth_ans[nextRoom] == 0) {
      ans[nextRoom] = nowRoom;
      depth_ans[nextRoom] = depth;
      // cout << nextRoom << "  " << nowRoom << endl;
    }
    if (visited[nextRoom] == 0) {
      que.push(nextRoom);
    }
  }
  while (que.size() != 0) {
    int i = que.front();
    que.pop();
    saiki(room, i, depth_ans[i]);
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> room(n + 1);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    room[a].push_back(b);
    room[b].push_back(a);
  }

  int nowRoom = 1;
  saiki(room, nowRoom, 1);
  bool check = true;
  for (int i = 2; i < n + 1; ++i) {
    if (ans[i] == 0)
      check = false;
  }
  if (check) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; ++i) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}