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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;

void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  int sum = 0;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
    sum++;
    if (sum == 2) {
      break;
    }
  }

  return path;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  graph = AdjList(n);
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  dijkstra(n, 0);
  vector<int> anss;
  bool ok = true;
  for (int i = 1; i < n; i++) {
    vector<int> ans = get_path(i);
    if (ans.size() < 2) {
      ok = false;
      break;
    }
    anss.push_back(ans[1]);
  }
  if (ok) {
    cout << "Yes" << endl;
    for (int i = 0; i < n - 1; i++) {
      cout << anss[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;

void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }

  reverse(path.begin(), path.end());
  return path;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  graph = AdjList(n);
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  dijkstra(n, 0);
  vector<int> anss;
  bool ok = true;
  for (int i = 1; i < n; i++) {
    vector<int> ans = get_path(i);
    if (ans.size() < 2) {
      ok = false;
      break;
    }
    anss.push_back(ans[ans.size() - 2]);
  }
  if (ok) {
    cout << "Yes" << endl;
    for (int i = 0; i < n - 1; i++) {
      cout << anss[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
}