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

#include <bits/stdc++.h>

#pragma region Macro
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define SORT(v) sort(v.begin(), v.end())
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define vvi vector<vector<int>>
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#pragma endregion

static const int MAX = 100007;
static const int INF = 1e9 + 7;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, m;
vi adj[MAX];
int shirube[MAX];

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> pq;
  int color[MAX];
  int d[MAX];
  rep(i, n) {
    d[i] = INF;
    color[i] = WHITE;
    shirube[i] = -1;
  }

  d[0] = 0;
  pq.push(make_pair(0, 0)); // (部屋0からの距離, 部屋番号)
  color[0] = GRAY;

  while (!pq.empty()) {
    P f = pq.top();
    pq.pop();
    int u = f.second;
    color[u] = BLACK;

    if (d[u] < f.first)
      continue;

    rep(j, adj[u].size()) {
      int v = adj[u][j];
      // cout << v << "," << d[v] << endl;
      if (color[v] == BLACK) {
        continue;
      }
      if (d[v] > d[u] + 1) {
        d[v] = d[u] + 1;
        shirube[v] = u;
        pq.push(make_pair(d[v], v));
        color[v] = GRAY;
      }
    }
  }
}

void solve() {
#define int ll
  // ここに処理を記述
  // コンパイル時はエイリアス「gpp」--> c++17でコンパイル
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].eb(b);
    adj[b].eb(a);
  }

  dijkstra();

  FOR(i, 1, n) {
    if (shirube[i] == -1) {
      cout << "No" << endl;
      return;
    }
  }

  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << shirube[i] + 1 << endl; }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#pragma region Macro
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define SORT(v) sort(v.begin(), v.end())
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define vvi vector<vector<int>>
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#pragma endregion

static const int MAX = 100007;
static const int INF = 1e9 + 7;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, m;
vi adj[MAX];
int shirube[MAX];

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> pq;
  int color[MAX];
  int d[MAX];
  rep(i, n) {
    d[i] = INF;
    color[i] = WHITE;
    shirube[i] = -1;
  }

  d[0] = 0;
  pq.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!pq.empty()) {
    P f = pq.top();
    pq.pop();
    int u = f.second;
    color[u] = BLACK;

    if (d[u] < f.first)
      continue;

    rep(j, adj[u].size()) {
      int v = adj[u][j];
      // cout << v << "," << d[v] << endl;
      if (color[v] == BLACK) {
        continue;
      }
      if (d[v] > d[u] + 1) {
        d[v] = d[u] + 1;
        shirube[v] = u;
      }
      pq.push(make_pair(d[v], v));
      color[v] = GRAY;
    }
  }
}

void solve() {
#define int ll
  // ここに処理を記述
  // コンパイル時はエイリアス「gpp」--> c++17でコンパイル
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].eb(b);
    adj[b].eb(a);
  }

  dijkstra();

  FOR(i, 1, n) {
    if (shirube[i] == -1) {
      cout << "No" << endl;
      return;
    }
  }

  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << shirube[i] + 1 << endl; }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  solve();
  return 0;
}