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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

template <class T> struct Dijkstra {
  const T inf = numeric_limits<T>::max() / 2 - 1;
  vector<T> dist;
  vector<int> path;

  Dijkstra() {}
  Dijkstra(int n) {
    graph.resize(n);
    dist.resize(n);
    prev.resize(n);
  }

  void add_edge(int from, int to, T cost) {
    graph[from].emplace_back(to, cost);
  }

  void get_distance(int from) {
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>>
        pq;
    fill(dist.begin(), dist.end(), inf);
    fill(prev.begin(), prev.end(), -1);
    dist[from] = 0;
    pq.push(make_pair(0, from));
    while (!pq.empty()) {
      pair<T, int> p = pq.top();
      pq.pop();
      if (dist[p.second] < p.first)
        continue;
      for (auto e : graph[p.second]) {
        if (dist[e.first] > (dist[p.second] + e.second)) {
          dist[e.first] = dist[p.second] + e.second;
          prev[e.first] = p.second;
          pq.push(make_pair(dist[e.first], e.first));
        }
      }
    }
  }

  void get_path(int to) {
    path = vector<int>(0);
    for (; to != -1; to = prev[to]) {
      path.push_back(to);
    }
    reverse(path.begin(), path.end());
  }

private:
  vector<vector<pair<int, T>>> graph;
  vector<T> prev;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n, m;
  cin >> n >> m;
  Dijkstra<ll> di(n);
  vector<vector<ll>> g(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    di.add_edge(a, b, 1);
    di.add_edge(b, a, 1);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  di.get_distance(0);
  // rep(i, n) printf("%lld\\n", di.dist[i]);
  cout << "Yes" << endl;
  reps(i, 1, n) {
    ll mv = LONG_LONG_MAX, mi = -1;
    for (auto x : g[i]) {
      if (mv > di.dist[x]) {
        mv = di.dist[x];
        mi = x;
      }
    }
    cout << (mi + 1) << endl;
  }
  // ans.resize(n, make_pair(-1, -1));
  // ans[0] = make_pair(0, 0);
  // dfs(0);
  // cout << "Yes" << endl;
  // reps(i, 1, n) cout << (ans[i].first + 1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

ll n, m;
vector<vector<ll>> g;
vector<pair<ll, ll>> ans;

void dfs(ll v, ll p = -1, ll d = 0) {
  for (auto x : g[v]) {
    if (x == p)
      continue;
    if (ans[x].first == -1) {
      ans[x].first = v;
      ans[x].second = d + 1;
      dfs(x, v, d + 1);
    } else if (ans[x].second > (d + 1)) {
      ans[x].first = v;
      ans[x].second = d + 1;
      dfs(x, v, d + 1);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  cin >> n >> m;
  g.resize(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  ans.resize(n, make_pair(-1, -1));
  ans[0] = make_pair(0, 0);
  dfs(0);
  cout << "Yes" << endl;
  reps(i, 1, n) cout << (ans[i].first + 1) << endl;
  return 0;
}