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

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)
#define REV(X, Y, Z) for (ll(X) = (Y); (X) > (Z); (X)--)

using namespace std;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMod = 1e9 + 7;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  ll N, M;
  cin >> N >> M;
  unordered_map<ll, usll> R;
  REP(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    if (R.find(a) == R.end()) {
      R.emplace(a, usll{b});
    } else {
      R[a].emplace(b);
    }
    if (R.find(b) == R.end()) {
      R.emplace(b, usll{a});
    } else {
      R[b].emplace(a);
    }
  }

  vll D(N + 1, -1);
  D[1] = 0;
  deque<pll> Q;
  for (const auto &r : R[1]) {
    D[r] = 1;
    Q.emplace_back(r, 1);
  }

  while (!Q.empty()) {
    ll x = Q[0].first;
    ll d = Q[0].second;
    Q.pop_front();

    for (const auto &r : R[x]) {
      if (D[r] != -1) {
        continue;
      }

      D[r] = x;
      Q.emplace_back(pll(r, d + 1));
    }
  }

  cout << "Yes" << endl;
  REP(i, 2, N + 1) { cout << D[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)
#define REV(X, Y, Z) for (ll(X) = (Y); (X) > (Z); (X)--)

using namespace std;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMod = 1e9 + 7;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

void search(ll n, vll &done, ll point, map<ll, vll> &R,
            unordered_map<ll, pll> &lens) {
  if (R.find(n) == R.end()) {
    return;
  }

  for (const auto &r : R[n]) {
    if (find(done.begin(), done.end(), r) != done.end()) {
      if (lens.find(r) == lens.end()) {
        lens[r] = pll(n, point);
      } else {
        if (lens[r].second == 0 || lens[r].second > point) {
          lens[r] = pll(n, point);
        }
      }
      continue;
    }

    if (lens.find(r) == lens.end()) {
      lens[r] = pll(n, point + 1);
    } else {
      if (lens[r].second == 0 || lens[r].second > point + 1) {
        lens[r] = pll(n, point + 1);
      }
    }
    vll done2 = done;
    done2.emplace_back(r);
    search(r, done2, point + 1, R, lens);
  }
}

unordered_map<ll, pll> doit(ll N, map<ll, vll> &R) {
  unordered_map<ll, pll> lens(N + 1);
  lens[0] = pll(0, 0);
  lens[1] = pll(0, 0);

  if (R.find(1) != R.end()) {
    for (const auto &r : R[1]) {
      lens[r] = pll(1, 1);
      vll done(1, 1);
      search(r, done, 1, R, lens);
    }
  }

  return lens;
}

int main() {
  init();

  ll N, M;
  cin >> N >> M;
  vll A(M), B(M);
  map<ll, vll> R;
  REP(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    A[i] = a;
    B[i] = b;
    if (R.find(a) == R.end()) {
      vll v(1, b);
      R.emplace(a, v);
    } else {
      R[a].emplace_back(b);
    }
    if (R.find(b) == R.end()) {
      vll v(1, a);
      R.emplace(b, v);
    } else {
      R[b].emplace_back(a);
    }
  }

  unordered_map<ll, pll> lens = doit(N, R);
  for (const auto &l : lens) {
    if (l.second.second == kMod) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  REP(i, 2, N + 1) { cout << lens[i].first << endl; }

  return 0;
}