Problem p02678 (C++)

Buggy submission (status: Runtime Error):
/*

                              Jai Shree Krishna !!
                                 Radhe Radhe !!

             उद्यमेन हि सिद्ध्यन्ति कार्याणि न मनोरथैः। नहि सुप्तस्य सिंहस्य मुखे प्रविशन्ति
   मृगाः।।

*/

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

#define int long long
typedef long long ll;
#define loop(i, n) for (int i = 0; i < n; i++)
#define read(a)                                                                \
  int a;                                                                       \
  cin >> a;
#define readarr(a, n)                                                          \
  int a[n];                                                                    \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define readmat(a, n, m)                                                       \
  int a[n][m];                                                                 \
  for (int i = 0; i < n; i++)                                                  \
    for (int j = 0; j < m; j++)                                                \
      cin >> a[i][j];
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define vvi vector<vector<int>>
#define sti set<int>
#define vpi vector<pair<int, int>>
#define print(a, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << endl;
#define printmat(a, n, m)                                                      \
  for (int i = 0; i < n; i++) {                                                \
    for (int j = 0; j < m; j++)                                                \
      cout << a[i][j] << " ";                                                  \
    cout << endl;                                                              \
  }
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define endl "\n"
#define mp make_pair
#define ins insert
#define rep(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = e; i >= s; i--)
#define vb vector<bool>
#define read2(n, m)                                                            \
  int n, m;                                                                    \
  cin >> n >> m;
#define range(v) v.begin(), v.end()
#define sortd(v) sort(v.begin(), v.end(), greater<int>())
#define sorti(v) sort(v.begin(), v.end())
#define PI 3.14159265358979323846264338
const ll inf = 1e18;
const ll mod = 1e9 + 7;

vb visited(100001);
vi adj[100001];
void dfs(int v) {
  visited[v] = true;
  for (int u : adj[v]) {
    if (!visited[u])
      dfs(u);
  }
}

signed main() {

  fast;
  read2(n, m);
  rep(i, 0, m - 1) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(1);
  rep(i, 1, n) {
    if (!visited[i]) {
      cout << "No" << endl;
      return 0;
    }
  }

  queue<int> q;
  vector<bool> used(n);
  vector<int> d(n), p(n);
  int s = 1;
  q.push(s);
  used[s] = true;
  p[s] = -1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : adj[v]) {
      if (!used[u]) {
        used[u] = true;
        q.push(u);
        // d[u] = v;
        p[u] = v;
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, 2, n) cout << p[i] << endl;
  return 0;
}

/* Pooja Yadav */

Fixed submission:
/*

                              Jai Shree Krishna !!
                                 Radhe Radhe !!

             उद्यमेन हि सिद्ध्यन्ति कार्याणि न मनोरथैः। नहि सुप्तस्य सिंहस्य मुखे प्रविशन्ति
   मृगाः।।

*/

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

#define int long long
typedef long long ll;
#define loop(i, n) for (int i = 0; i < n; i++)
#define read(a)                                                                \
  int a;                                                                       \
  cin >> a;
#define readarr(a, n)                                                          \
  int a[n];                                                                    \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define readmat(a, n, m)                                                       \
  int a[n][m];                                                                 \
  for (int i = 0; i < n; i++)                                                  \
    for (int j = 0; j < m; j++)                                                \
      cin >> a[i][j];
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define vvi vector<vector<int>>
#define sti set<int>
#define vpi vector<pair<int, int>>
#define print(a, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << endl;
#define printmat(a, n, m)                                                      \
  for (int i = 0; i < n; i++) {                                                \
    for (int j = 0; j < m; j++)                                                \
      cout << a[i][j] << " ";                                                  \
    cout << endl;                                                              \
  }
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define endl "\n"
#define mp make_pair
#define ins insert
#define rep(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = e; i >= s; i--)
#define vb vector<bool>
#define read2(n, m)                                                            \
  int n, m;                                                                    \
  cin >> n >> m;
#define range(v) v.begin(), v.end()
#define sortd(v) sort(v.begin(), v.end(), greater<int>())
#define sorti(v) sort(v.begin(), v.end())
#define PI 3.14159265358979323846264338
const ll inf = 1e18;
const ll mod = 1e9 + 7;

vb visited(100001);
vi adj[100001];
void dfs(int v) {
  visited[v] = true;
  for (int u : adj[v]) {
    if (!visited[u])
      dfs(u);
  }
}

signed main() {

  fast;
  read2(n, m);
  rep(i, 0, m - 1) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(1);
  rep(i, 1, n) {
    if (!visited[i]) {
      cout << "No" << endl;
      return 0;
    }
  }

  queue<int> q;
  vector<bool> used(n + 1);
  vector<int> p(n + 1);
  int s = 1;
  q.push(s);
  used[s] = true;
  p[s] = -1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : adj[v]) {
      if (!used[u]) {
        used[u] = true;
        q.push(u);
        // d[u] = v;
        p[u] = v;
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, 2, n) cout << p[i] << endl;
  return 0;
}

/* Pooja Yadav */