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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

ull N, M;

const ull RMAX = 100001;

const ull PMAX = 200001;

typedef struct {
  ull min_dist = RMAX;
  ull to;
  vector<ull> path;
} Room;
Room room[RMAX];

void output() {
  for (int i = 2; i <= N; i++) {
    cout << room[i].to << endl;
    // cout << i << ":" << room[i].to << " dist=" << room[i].min_dist << endl;
  }
  // cout << endl;
}

void output_debug() {
  for (int i = 2; i <= N; i++) {
    cout << i << ":" << room[i].to << " dist=" << room[i].min_dist << endl;
  }
  cout << endl;
}

queue<ull> rooms_to_check;

void set_minpath(ull from) {

  ull cmin = room[from].min_dist;

  // cout << "from:" << from  << " cmin=" << cmin << endl;
  // output();

  bool update = false;

  for (auto itr : room[from].path) {
    // cout << "   checking room=" << itr << endl;

    if (room[itr].min_dist > cmin + 1) {
      room[itr].min_dist = cmin + 1;
      room[itr].to = from;

      rooms_to_check.push(itr);

      // cout << "       set itr=" << itr << " cmin=" << cmin+1 << endl;

      // output();
    }
  }
}

int main() {
  cin >> N >> M;

  ull i, j, k;
  ull from, to;

  for (i = 1; i <= M; i++) {

    cin >> from >> to;

    room[from].path.push_back(to);
    room[to].path.push_back(from);
  }

  room[1].min_dist = 0;
  room[1].to = 0;
  // set_minpath_dfs(1);

  rooms_to_check.push(1);

  while (!rooms_to_check.empty()) {
    ull itr = rooms_to_check.front();
    rooms_to_check.pop();
    set_minpath(itr);
  }

  set_minpath(1);

  // output_debug();

  bool check = true;

#ifdef NEVER
  for (i = 2; i <= N; i++) {
    if (room[i].min_dist == RMAX || room[i].to == 0) {
      // cout << i << " min_dist=" << room[i].min_dist << " to=" << room[i].to
      // << endl;
      check = false;
      break;
    }
  }
#endif

  if (check) {
    cout << "Yes" << endl;
    output();
  } else {
    cout << "No" << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

ull N, M;

const ull RMAX = 100001;

const ull PMAX = 200001;

typedef struct {
  ull min_dist = RMAX;
  ull to;
  vector<ull> path;
} Room;
Room room[RMAX];

void output() {
  for (int i = 2; i <= N; i++) {
    cout << room[i].to << endl;
    // cout << i << ":" << room[i].to << " dist=" << room[i].min_dist << endl;
  }
  // cout << endl;
}

void output_debug() {
  for (int i = 2; i <= N; i++) {
    cout << i << ":" << room[i].to << " dist=" << room[i].min_dist << endl;
  }
  cout << endl;
}

void set_minpath(ull from) {

  ull cmin = room[from].min_dist;

  queue<ull> rooms_to_check;

  // cout << "from:" << from  << " cmin=" << cmin << endl;
  // output();

  bool update = false;

  for (auto itr : room[from].path) {
    // cout << "   checking room=" << itr << endl;

    if (room[itr].min_dist > cmin + 1) {
      room[itr].min_dist = cmin + 1;
      room[itr].to = from;

      rooms_to_check.push(itr);

      // cout << "       set itr=" << itr << " cmin=" << cmin+1 << endl;

      // output();
    }
  }

  while (!rooms_to_check.empty()) {
    ull itr = rooms_to_check.front();
    set_minpath(itr);
    rooms_to_check.pop();
  }
}

int main() {
  cin >> N >> M;

  ull i, j, k;
  ull from, to;

  for (i = 1; i <= M; i++) {

    cin >> from >> to;

    room[from].path.push_back(to);
    room[to].path.push_back(from);
  }

  room[1].min_dist = 0;
  room[1].to = 0;
  // set_minpath_dfs(1);

  set_minpath(1);

  // output_debug();

  bool check = true;

#ifdef NEVER
  for (i = 2; i <= N; i++) {
    if (room[i].min_dist == RMAX || room[i].to == 0) {
      // cout << i << " min_dist=" << room[i].min_dist << " to=" << room[i].to
      // << endl;
      check = false;
      break;
    }
  }
#endif

  if (check) {
    cout << "Yes" << endl;
    output();
  } else {
    cout << "No" << endl;
  }
}