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

// #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>

using namespace std;

class room {
public:
  vector<int> m_conectedRooms;
  int m_aite;
  int m_step;

  room() {
    m_aite = -1;
    m_step = 999999;
  }
};

void BFS(vector<room> *rooms) {
  queue<int> roomQue;

  roomQue.push(1);
  while (!roomQue.empty()) {
    // Queを探って、depthとaiteを入れます
    int curRoomID = roomQue.front();
    roomQue.pop();
    room curRoom = (*rooms).at(curRoomID);
    for (int j = 0; j < curRoom.m_conectedRooms.size(); j++) {
      if ((*rooms).at(curRoom.m_conectedRooms[j]).m_step > curRoom.m_step + 1) {
        roomQue.push(curRoom.m_conectedRooms[j]);
        (*rooms).at(curRoom.m_conectedRooms[j]).m_step = curRoom.m_step + 1;
        (*rooms).at(curRoom.m_conectedRooms[j]).m_aite = curRoomID;
      }
    }
  }
}

int main(void) {
  int N, M;

  cin >> N >> M;

  vector<room> rooms(N + 1);

  // マップを作る
  for (int i = 0; i < M; i++) {
    int roomA, roomB;
    cin >> roomA >> roomB;
    rooms[roomA].m_conectedRooms.push_back(roomB);
    rooms[roomB].m_conectedRooms.push_back(roomA);
  }

  // 部屋１からたどれるかフラグを付ける
  rooms[1].m_step = 0;
  rooms[1].m_aite = 0;

  //	gotoNext(&rooms, 1);
  BFS(&rooms);

#ifdef _DBG_
  for (int i = 1; i <= N; i++) {
    cout << "room:" << i << " " << (int)(rooms[i].m_conectedRooms.size()) << " "
         << rooms[i].m_aite << endl;
    for (int j = 0; j < (int)(rooms[i].m_conectedRooms.size()); j++) {
      cout << "  " << rooms[i].m_conectedRooms[j] << endl;
    }
  }
#endif

  // 相手を調べる
  bool flgOK = true;
  for (int i = 2; i <= N; i++) {
    if (rooms[i].m_aite == -1) {
      flgOK = false;
      break;
    }
  }

  // 最終出力
  if (flgOK) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << rooms[i].m_aite << endl;
    }
  } else {
    cout << "No" << endl;
  }

#ifdef _DBG_
  int CC;
  cin >> CC;
#endif

  return 0;
}


Error: 0

Fixed submission:

// #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>

using namespace std;

class room {
public:
  vector<int> m_conectedRooms;
  int m_aite;
  int m_step;

  room() {
    m_aite = -1;
    m_step = 999999;
  }
};

void gotoNext(vector<room> *rooms, int i) {
  vector<int> nextRooms = (*rooms).at(i).m_conectedRooms;
  int step = (*rooms).at(i).m_step;

  for (int j = 0; j < nextRooms.size(); j++) {
    if ((*rooms).at(nextRooms[j]).m_step > step + 1) {
      (*rooms).at(nextRooms[j]).m_step = step + 1;
      (*rooms).at(nextRooms[j]).m_aite = i;
      gotoNext(rooms, nextRooms[j]);
    }
  }
}

int main(void) {
  int N, M;

  vector<room> rooms(10005);

  cin >> N >> M;

  //	vector<room> rooms(N+1);

  // マップを作る
  for (int i = 0; i < M; i++) {
    int roomA, roomB;
    cin >> roomA >> roomB;
    rooms[roomA].m_conectedRooms.push_back(roomB);
    rooms[roomB].m_conectedRooms.push_back(roomA);
  }

  // 部屋１からたどれるかフラグを付ける
  rooms[1].m_step = 0;
  rooms[1].m_aite = 0;

  gotoNext(&rooms, 1);

#ifdef _DBG_
  for (int i = 1; i <= N; i++) {
    cout << "room:" << i << " " << (int)(rooms[i].m_conectedRooms.size()) << " "
         << rooms[i].m_aite << endl;
    for (int j = 0; j < (int)(rooms[i].m_conectedRooms.size()); j++) {
      cout << "  " << rooms[i].m_conectedRooms[j] << endl;
    }
  }
#endif

  // 相手を調べる
  bool flgOK = true;
  for (int i = 2; i <= N; i++) {
    if (rooms[i].m_aite == -1) {
      flgOK = false;
      break;
    }
  }

  // 最終出力
  if (flgOK) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << rooms[i].m_aite << endl;
    }
  } else {
    cout << "No" << endl;
  }

#ifdef _DBG_
  int CC;
  cin >> CC;
#endif

  return 0;
}