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

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define all(vec) vec.begin(), vec.end()
#define ff first
#define ss second
#define inf 4e18
#define MOD 1000000007
#define pb push_back
#define for1(low, high, k) for (int i = low; i < high; i += k)
#define for2(low, high, k) for (int j = low; j < high; j += k)
#define rev(high, low, k) for (int i = high; i >= low; i -= k)

void bfs(lli k, vector<pii> &dist, vector<vector<lli>> &graph) {
  queue<lli> qu;
  qu.push(k);
  while (!qu.empty()) {
    k = qu.front();
    qu.pop();
    for (int i = 0; i < graph[k].size(); i++) {
      if (dist[graph[k][i]].ff == inf) {
        dist[graph[k][i]].ff = dist[k].ff + 1;
        dist[graph[k][i]].ss = k;
        qu.push(graph[k][i]);
      }
    }
  }
  return;
}

int main() {
  fast;
  lli n, m, x, y;
  cin >> n >> m;
  vector<vector<lli>> graph(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    graph[x].pb(y);
    graph[y].pb(x);
  }
  vector<pii> dist(n + 1);
  for (int i = 0; i < n + 1; i++)
    dist[i].ff = inf, dist[i].ss = -1;
  dist[1].ff = 0;
  bfs(1, dist, graph);
  int i;
  for (i = 1; i < n + 1; i++) {
    if (dist[i].ff == inf)
      break;
  }
  if (i == n + 1) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++)
      cout << dist[i].ss << endl;
  } else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define all(vec) vec.begin(), vec.end()
#define ff first
#define ss second
#define inf 4e18
#define MOD 1000000007
#define pb push_back
#define for1(low, high, k) for (int i = low; i < high; i += k)
#define for2(low, high, k) for (int j = low; j < high; j += k)
#define rev(high, low, k) for (int i = high; i >= low; i -= k)

void dfs(lli k, vector<pii> &dist, vector<vector<lli>> &graph) {
  for (int i = 0; i < graph[k].size(); i++) {
    if (dist[graph[k][i]].ff > dist[k].ff + 1) {
      dist[graph[k][i]].ff = dist[k].ff + 1;
      dist[graph[k][i]].ss = k;
      dfs(graph[k][i], dist, graph);
    }
  }
}

int main() {
  fast;
  lli n, m, x, y;
  cin >> n >> m;
  vector<vector<lli>> graph(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    graph[x].pb(y);
    graph[y].pb(x);
  }
  vector<pii> dist(n + 1);
  for (int i = 0; i < n + 1; i++)
    dist[i].ff = inf, dist[i].ss = -1;
  dist[1].ff = 0;
  dfs(1, dist, graph);
  int i;
  for (i = 1; i < n + 1; i++) {
    if (dist[i].ff == inf)
      break;
  }
  if (i == n + 1) {
    cout << "Yes" << endl;
    for (int i = 2; i < n + 1; i++)
      cout << dist[i].ss << endl;
  } else
    cout << "No" << endl;
}