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

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n, m;

  cin >> n >> m;
  vector<int> a(m), b(m);
  vector<int> rooms(n);
  vector<vector<int>> routes(n, vector<int>(0));

  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    routes[a[i] - 1].push_back(b[i] - 1);
    routes[b[i] - 1].push_back(a[i] - 1);
  }

  vector<int> dist(n, -1);
  queue<int> que;

  dist[0] = 0;
  que.push(0);

  while (!que.empty()) {
    int current = que.front();
    que.pop();

    int cnCnt = routes[current].size();
    for (int i = 0; i < cnCnt; i++) {
      int to = routes[current][i];
      if (dist[to] != -1)
        continue;

      dist[to] = dist[current] + 1;
      que.push(to);
    }
  }

  vector<vector<int>> depthMap(n, vector<int>(0));
  for (int i = 0; i < n; i++) {
    if (dist[i] >= 0) {
      depthMap[dist[i]].push_back(i);
    }
  }

  bool success = true;
  vector<int> result(n - 1);
  for (int i = 1; i < n; i++) {
    vector<int> upper = depthMap[dist[i] - 1];
    if (upper.size() == 0) {
      success = false;
      break;
    }
    for (auto v : routes[i]) {
      if (find(upper.begin(), upper.end(), v) != upper.end()) {
        result[i - 1] = upper[0];
        break;
      }
    }
  }

  if (success) {
    cout << "Yes\\n";
    for (auto i : result) {
      cout << i + 1 << endl;
    }
  } else {
    cout << "No\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
// cost, index;
using P = pair<int, int>;

// start, goal [0, n-1]
// returns successive indices vector[0]=start, vector[v-1] = goal
// return 0 length vector if no path is found
// search path using dijkstra.
vector<int> dijkstraSearch(vector<vector<int>> &routes, int start, int goal) {
  int indices = routes.size();
  vector<int> d(indices);
  // greater使えるのか？
  priority_queue<P, vector<P>, greater<P>> queue;
  fill(d.begin(), d.end(), 1e+9);
  d[start] = 0;
  queue.push(P(0, start));

  vector<int> cameFrom(indices);

  cameFrom[start] = start;
  int current = start;
  while (!queue.empty()) {
    P p = queue.top();
    queue.pop();

    current = p.second;
    if (d[current] < p.first)
      continue;

    if (current == goal) {
      break;
    }

    int cnCnt = routes[current].size();
    for (int i = 0; i < cnCnt; i++) {
      int to = routes[current][i];
      if (d[current] + 1 < d[to]) {
        d[to] = d[current] + 1;
        queue.push(P(d[to], to));
        cameFrom[to] = current;
      }
    }
  }

  if (current == goal) {
    vector<int> resultPath(d[goal] + 1);
    int to = goal;
    int from = cameFrom[goal];
    resultPath[0] = goal;
    int i = 1;
    while (to != start) {
      resultPath[i] = from;
      i++;
      to = from;
      from = cameFrom[to];
    }
    return resultPath;
  } else {
    vector<int> result(0);
    return result;
  }
}

int main(void) {
  int n, m;

  cin >> n >> m;
  vector<int> a(m), b(m);
  vector<int> rooms(n);
  vector<vector<int>> routes(n, vector<int>(0));
  priority_queue<P, vector<P>, greater<P>> queue;

  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    routes[a[i] - 1].push_back(b[i] - 1);
    routes[b[i] - 1].push_back(a[i] - 1);
  }

  bool success = true;
  vector<int> waypoints(n - 1);
  for (int i = 0; i < n; i++) {
    vector<int> path = dijkstraSearch(routes, 0, i);
    if (path.size() == 0) {
      success = false;
      break;
    }
    if (path.size() > 1) {
      for (int j = 0; j < path.size() - 1; j++) {
        waypoints[path[j] - 1] = path[j + 1];
      }
    }
  }

  if (success) {
    cout << "Yes\\n";
    for (int i = 0; i < n - 1; i++) {
      cout << waypoints[i] + 1 << endl;
    }
  } else {
    cout << "No\\n";
  }
  return 0;
}