Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <vector>

#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
#define mod 1000000007
#define inf 1e18 + 42
#define endl "\n"

#define out1(a) cout << #a << " " << a << endl
#define out2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define out3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl

#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define fori(it, A) for (auto it = A.begin(); it != A.end(); it++)

#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define zero(x) memset(x, 0, sizeof(x));

using namespace std;

int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

void bfs(vector<vector<int>> &v, int x, vector<int> &vis, vector<int> &dist,
         vector<int> &pred) {
  queue<int> q;
  q.push(x);
  vis[x] = 1;

  while (!q.empty()) {
    int p = q.front();
    q.pop();

    rep(i, 0, v[p].size()) {
      if (vis[v[p][i]] == 0) {
        dist[v[p][i]] = dist[p] + 1;
        pred[v[p][i]] = p;
        vis[v[p][i]] = 1;
        q.push(v[p][i]);
      }
    }
  }
}

// START OF CODE ->->->->->->->

void solve() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> v(n + 1);

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;

    v[a].pb(b);
    v[b].pb(a);
  }

  vector<int> vis(n + 1, 0);
  vector<int> dist(n + 1, mod);

  dist[1] = 0;
  vector<int> pred(n + 1, -1);
  bfs(v, 1, vis, dist, pred);
  int found = 0;
  rep(i, 2, dist.size()) {
    if (dist[i] == mod) {
      found = 1;
      break;
    }
  }
  if (found == 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    rep(i, 2, dist.size()) {
      int l = i;
      // cout<<dist[i]<<endl;
      vector<int> path;
      while (pred[l] != -1) {
        path.pb(pred[l]);
        l = pred[l];
      }

      cout << path[0] << endl;
    }
  }
}

// END OF CODE ->->->->->->->->

signed main() {
  fast;
  int t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <vector>

#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
#define mod 1000000007
#define inf 1e18 + 42
#define endl "\n"

#define out1(a) cout << #a << " " << a << endl
#define out2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define out3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl

#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define fori(it, A) for (auto it = A.begin(); it != A.end(); it++)

#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define zero(x) memset(x, 0, sizeof(x));

using namespace std;

int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

void bfs(vector<vector<int>> &v, int x, vector<int> &vis, vector<int> &dist,
         vector<int> &pred) {
  queue<int> q;
  q.push(x);
  vis[x] = 1;

  while (!q.empty()) {
    int p = q.front();
    q.pop();

    rep(i, 0, v[p].size()) {
      if (vis[v[p][i]] == 0) {
        dist[v[p][i]] = dist[p] + 1;
        pred[v[p][i]] = p;
        vis[v[p][i]] = 1;
        q.push(v[p][i]);
      }
    }
  }
}

// START OF CODE ->->->->->->->

void solve() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> v(n + 1);

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;

    v[a].pb(b);
    v[b].pb(a);
  }

  vector<int> vis(n + 1, 0);
  vector<int> dist(n + 1, mod);

  dist[1] = 0;
  vector<int> pred(n + 1, -1);
  bfs(v, 1, vis, dist, pred);
  int found = 0;
  rep(i, 2, dist.size()) {
    if (dist[i] == mod) {
      found = 1;
      break;
    }
  }
  if (found == 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    rep(i, 2, dist.size()) {
      int l = i;
      // cout<<dist[i]<<endl;
      vector<int> path;
      while (pred[l] != -1) {
        path.pb(pred[l]);
        l = pred[l];
        break;
      }

      cout << path[0] << endl;
    }
  }
}

// END OF CODE ->->->->->->->->

signed main() {
  fast;
  int t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
  return 0;
}