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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = (ll)(n - 1); i >= 0; i--)
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define RFOR(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

int N, M;
ll dp[111111];
ll ans[111111];

auto comp = [](int a, int b) { return dp[a] > dp[b]; };

void defdp(vector<vector<int>> A) {
  priority_queue<int, vector<int>, decltype(comp)> W(comp);
  W.push(1);
  while (!W.empty()) {
    int now = W.top();
    W.pop();
    for (auto v : A[now]) {
      // if(H.find(v) == H.end()){
      // dp[v] = min(dp[v], dp[now]+1);
      // W.push(v);
      //}
      if (dp[v] != -1)
        continue;
      dp[v] = dp[now] + 1;
      ans[v] = now;
      W.push(v);
    }
  }
}

signed main() {
  cin >> N >> M;
  vector<vector<int>> A(N + 1);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    A[a].PB(b);
    A[b].PB(a);
  }
  REP(i, N + 1) { dp[i] = -1; }
  dp[1] = 0;
  defdp(A);
  cout << "Yes"
       << "\\n";
  for (int i = 2; i < N + 1; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = (ll)(n - 1); i >= 0; i--)
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define RFOR(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

int N, M;
ll dp[111111];

auto comp = [](int a, int b) { return dp[a] > dp[b]; };

void defdp(vector<vector<int>> A) {
  priority_queue<int, vector<int>, decltype(comp)> W(comp);
  set<int> H;
  W.push(1);
  while (!W.empty()) {
    int now = W.top();
    W.pop();
    for (auto v : A[now]) {
      if (H.find(v) == H.end()) {
        dp[v] = min(dp[v], dp[now] + 1);
        W.push(v);
      }
    }
    H.insert(now);
  }
}

signed main() {
  cin >> N >> M;
  vector<vector<int>> A(N + 1);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    A[a].PB(b);
    A[b].PB(a);
  }
  REP(i, N + 1) { dp[i] = INF; }
  dp[1] = 0;
  defdp(A);
  cout << "Yes"
       << "\\n";
  for (int i = 2; i < N + 1; i++) {
    ll min = INF;
    int mind;
    for (auto v : A[i]) {
      if (dp[v] < min) {
        min = dp[v];
        mind = v;
      }
    }
    cout << mind << endl;
  }
}