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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using pii = pair<ll, ll>;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

int main() {
  ios::sync_with_stdio(false);
  ll n, m, a, b;
  cin >> n >> m;
  vvi g = vvi(n);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<pii> sign = vector<pii>(n, make_pair(-1, 1e6));
  sign[0] = make_pair(0, 0);

  queue<ll> q;

  q.push(0);
  bool able = true;
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    ll previous = p;
    ll score = sign[p].second;

    if (score > n) {
      break;
    }
    // cerr << "previous:" << previous << endl;
    // cerr << "score:" << score << endl;
    // cerr << p << endl;
    for (ll i : g[previous]) {
      if (i == 0)
        continue;
      if (sign[i].first == -1) {
        // cerr << '\\t' << i;
        // cerr << '\\t' << sign[i] << endl;
        sign[i].first = previous;
        sign[i].second = score + 1;
        // cerr << '\\t' << "updated:" << sign[i] << endl;
        q.push(i);
      }
    }
  }

  // cerr << "test" << endl;
  for (int i = 1; able && i < n; i++) {
    // cerr << sign[i] << endl;
    if (sign[i].first == -1) {
      able = false;
    }
  }

  if (able) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << sign[i].first + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using pii = pair<ll, ll>;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

int main() {
  ll n, m, a, b;
  cin >> n >> m;
  vvi g = vvi(n);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<pii> sign = vector<pii>(n, make_pair(-1, 1e8));
  sign[0] = make_pair(0, 0);

  queue<ll> q;

  q.push(0);
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    ll previous = p;
    ll score = sign[p].second;

    // cerr << "previous:" << previous << endl;
    // cerr << "score:" << score << endl;
    // cerr << p << endl;
    for (ll i : g[previous]) {
      if (sign[i].first == -1 || score < sign[i].second) {
        // cerr << '\\t' << i ;
        // cerr << '\\t' << sign[i] << endl;
        sign[i].first = previous;
        sign[i].second = score + 1;
        // cerr << '\\t' << "updated:" << sign[i] << endl;
        q.push(i);
      }
    }
  }

  bool able = true;
  // cerr << "test" << endl;
  for (int i = 1; i < n; i++) {
    // cerr << sign[i] << endl;
    if (sign[i].first == -1) {
      able = false;
    }
  }

  if (able) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << sign[i].first + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}