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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define REP(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl
#define mp make_pair
#define pb push_back

const long long MOD = 1000000007;
const long long INF = 1LL << 60;

vector<vector<int>> graph;
vector<ll> dis;
vector<int> before;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(12);

  int n, m;
  //    cin >> n >> m;
  scanf("%d%d", &n, &m);
  graph.assign(n + 1, vector<int>());
  dis.assign(n + 1, INF);
  before.assign(n + 1, -1);
  rep(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  queue<int> q;
  dis[1] = 0;
  q.push(1);
  while (!q.empty()) {
    auto e = q.front();
    q.pop();
    for (auto next : graph[e]) {
      if (before[next] == -1) {
        before[next] = e;
        q.push(next);
      }
    }
  }

  //    priority_queue<pair<int, int>> p;
  //    dis[1] = 0;
  //    p.push(mp(0,1));
  //    while(!p.empty()){
  //        auto e = p.top();
  //        p.pop();
  //        int v = e.second;
  //        if(dis[v] < e.first){
  //            continue;
  //        }
  //        for(auto next: graph[v]){
  //            if(dis[next] > dis[v]+1){
  //                dis[next] = dis[v]+1;
  //                before[next] = v;
  //                p.push(mp(dis[next], next));
  //            }
  //        }
  //    }

  bool can = true;
  for (int i = 2; i < n + 1; i++) {
    if (before[i] == -1) {
      can = false;
    }
  }
  if (can) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++) {
      printf("%d\\n", before[i]);
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define REP(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl
#define mp make_pair
#define pb push_back

const long long MOD = 1000000007;
const long long INF = 1LL << 60;

vector<vector<int>> graph;
vector<ll> dis;
vector<int> before;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(12);

  int n, m;
  //    cin >> n >> m;
  scanf("%d%d", &n, &m);
  graph.assign(n + 1, vector<int>());
  dis.assign(n + 1, INF);
  before.assign(n + 1, -1);
  rep(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  priority_queue<pair<int, int>> p;
  dis[1] = 0;
  p.push(mp(0, 1));
  while (!p.empty()) {
    auto e = p.top();
    p.pop();
    int v = e.second;
    if (dis[v] < e.first) {
      continue;
    }
    for (auto next : graph[v]) {
      if (dis[next] > dis[v] + 1) {
        dis[next] = dis[v] + 1;
        before[next] = v;
        p.push(mp(dis[next], next));
      }
    }
  }

  bool can = true;
  for (int i = 2; i < n + 1; i++) {
    if (before[i] == -1) {
      can = false;
    }
  }
  if (can) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++) {
      printf("%d\\n", before[i]);
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}