Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

void Calculate_Depth(vector<vector<int>> &edge, vector<int> &depth,
                     int stnode) {
  depth[stnode] = 0;
  queue<int> Q;
  Q.push(stnode);
  while (!Q.empty()) {
    int cn = Q.front();
    Q.pop();
    for (auto i : edge[cn]) {
      if (depth[i] > depth[cn] + 1) {
        depth[i] = depth[cn] + 1;
        Q.push(i);
      }
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> M;
  vector<vector<int>> edge(N);
  for (int i = 0; i < M; i++) {
    cin >> L >> R;
    L--, R--;
    edge[L].push_back(R);
    edge[R].push_back(L);
  }
  vector<int> dis(N, MOD);
  Calculate_Depth(edge, dis, 0);
  cout << "Yes\\n";
  for (int i = 1; i < N; i++) {
    for (auto j : edge[i]) {
      if (dis[j] == dis[i] - 1) {
        cout << j + 1 << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

// #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#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;

  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);

  /*
  for (int s = 0; s < N; s++) {
          int count = 0;
          for (int i = 1; i <= N; i++) {
                  if (rooms[i].m_step == s ) {
                          for (int j = 0; j < rooms[i].m_conectedRooms.size();
  j++) { int aiteNo = rooms[i].m_conectedRooms[j]; if (rooms[aiteNo].m_step > s
  + 1) { rooms[aiteNo].m_aite = i; rooms[aiteNo].m_step = s + 1; count++;
                                  }
                          }

                  }
          }
          if (count == 0)break;
  }
  */

#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;
}