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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;

void read();
template <typename First, typename... Rest>
void read(First &first, Rest &...rest);
void write();
template <typename First, typename... Rest>
void write(First first, Rest... rest);
template <typename T> void write_vec(vector<T> &v, char divider);

int main() {
  int N, M;
  read(N, M);
  vector<int> A(M);
  vector<int> B(M);
  vector<vector<int>> v(N);
  for (int i = 0; i < M; i++) {
    read(A[i], B[i]);
    A[i]--;
    B[i]--;
    v[A[i]].push_back(B[i]);
    v[B[i]].push_back(A[i]);
  }
  vector<int> prev(N, -1);
  vector<int> d(N, INT_MAX);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  d[0] = 0;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int x = p.second;
    if (d[x] < p.first)
      continue;
    for (int i = 0; i < v[x].size(); i++) {
      if (d[v[x][i]] > d[x] + 1) {
        d[v[x][i]] = d[x] + 1;
        prev[v[x][i]] = x;
        q.push(make_pair(d[v[x][i]], v[x][i]));
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << prev[i] + 1 << endl;
  }
  return 0;
}

// 配列以外の入力を受け取る
void read() {}
template <typename First, typename... Rest>
void read(First &first, Rest &...rest) {
  cin >> first;
  read(rest...);
}
//
// 配列以外を出力する
void write() {}
template <typename First, typename... Rest>
void write(First first, Rest... rest) {
  cout << first << " ";
  write(rest...);
}
//
// 配列を区切って出力する
template <typename T> void write_vec(vector<T> &v, char divider) {
  for (size_t i = 0; i < v.size(); i++) {
    cout << v[i] << divider;
  }
}
//

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;

void read();
template <typename First, typename... Rest>
void read(First &first, Rest &...rest);
void write();
template <typename First, typename... Rest>
void write(First first, Rest... rest);
template <typename T> void write_vec(vector<T> &v, char divider);

int main() {
  int N, M;
  read(N, M);
  vector<int> A(M);
  vector<int> B(M);
  for (int i = 0; i < M; i++) {
    read(A[i], B[i]);
    A[i]--;
    B[i]--;
  }
  vector<vector<int>> v(N);
  for (int i = 0; i < M; i++) {
    v[A[i]].push_back(B[i]);
    v[B[i]].push_back(A[i]);
  }
  vector<int> prev(N, -1);
  vector<int> d(N, INT_MAX);
  priority_queue<pair<int, int>> q;
  d[0] = 0;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int x = p.second;
    if (d[x] < p.first)
      continue;
    for (int i = 0; i < v[x].size(); i++) {
      if (d[v[x][i]] > d[x] + 1) {
        d[v[x][i]] = d[x] + 1;
        prev[v[x][i]] = x;
        q.push(make_pair(d[v[x][i]], v[x][i]));
      }
    }
  }
  bool f = true;
  for (int i = 1; i < N; i++) {
    if (prev[i] == -1)
      f = false;
  }

  if (f) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << prev[i] + 1 << endl;
    }
  } else {
    cout << "No";
  }
  return 0;
}

// 配列以外の入力を受け取る
void read() {}
template <typename First, typename... Rest>
void read(First &first, Rest &...rest) {
  cin >> first;
  read(rest...);
}
//
// 配列以外を出力する
void write() {}
template <typename First, typename... Rest>
void write(First first, Rest... rest) {
  cout << first << " ";
  write(rest...);
}
//
// 配列を区切って出力する
template <typename T> void write_vec(vector<T> &v, char divider) {
  for (size_t i = 0; i < v.size(); i++) {
    cout << v[i] << divider;
  }
}
//