Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

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

typedef unsigned int uint;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int, ii> edge;

#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mem(arr, val) memset(arr, val, sizeof arr)
#define sz(v) (int)v.size()
#define rsz(v) v.resize
#define clr(v) v.clear()
#define rev(v) reverse(all(v))
#define lop(i, end) for (int i = 0; i < end; i++)
#define rlop(i, start) for (int i = start - 1; i >= 0; i--)
#define loop(i, start, end) for (int i = start; i < end; i++)
#define rloop(i, start, end) for (int i = start - 1; i >= end; i--)
#define PB push_back
#define pb pop_back
#define mP make_pair
#define f first
#define s second

const ll OO = (ll)(4e18) + 9;
const int MOD = (int)(1e9) + 7;
const int oo = 2147483647;
const double EPS = 1e-8;
const double PI = acos(-1.0);

// enum dir { d, r, u, l , dr, ur, dl, ul };
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

string abc = "abcdefghijklmnopqrstuvwxyz";
string vowels = "aeiouy"; // and sometimes "aeiouy"

template <typename X, typename Y> bool ckmin(X &a, Y b) {
  return a > b ? a = b, true : false;
}
template <typename X, typename Y> bool ckmax(X &a, Y b) {
  return a < b ? a = b, true : false;
}

int dcmp(ld d1, ld d2) {
  return fabs(d1 - d2) <= EPS ? 0 : d1 > d2 ? 1 : -1;
} // Compare Double Numbers
ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
// convert index from 2D array to 1D array. (Zero-indexed)
int conv_2_1(int r, int c, int nCol) { return (nCol * r) + c + 1; }
// convert index from 1D array to 2D array. (Zero-indexed)
ii conv_1_2(int n, int nCol) {
  int r = n / nCol, c = n % nCol;
  return ii(r, c);
}

void inter(string s) {
  cout << s << endl;
  fflush(stdout);
}

ll power(ll base, ll exp) {
  ll ans = 1;
  base %= MOD;
  while (exp > 0) {
    if (exp & 1)
      ans = (ans * base) % MOD;
    exp >>= 1;
    base = (base * base) % MOD;
  }
  return ans;
}

ll modInverse(ll a) { return power(a, MOD - 2); }

ll fact[2000010];

void init() {
  fact[0] = 1;
  for (int i = 1; i < 2000010; ++i)
    fact[i] = (fact[i - 1] * 1LL * i) % MOD;
}

ll nCr(ll n, ll r) {
  return (fact[n] * modInverse(fact[r] * 1LL * fact[n - r])) % MOD;
}

const int N = 1e5 + 10;
vector<int> adj[N];
int dis[N]; // dis[v] : holds the shortest distance between the source and node
            // "v".

void bfs(int src) {
  queue<int> q;
  q.push(src);

  memset(dis, -1, sizeof(dis));

  dis[src] = 0;

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

    for (int v : adj[u]) {
      if (dis[v] == -1) {
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE

#else
  // freopen("pyramid.in", "r", stdin);
#endif
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, u, v;
  cin >> n >> m;
  lop(i, m) sc(u), sc(v), adj[u].PB(v), adj[v].PB(u);
  bfs(1);
  for (int i = 2; i <= n; i++)
    if (dis[i] == -1)
      return puts("No");
  cout << "Yes\n";
  loop(i, 2, n + 1) {
    for (int u : adj[i]) {
      if (dis[i] == dis[u] + 1) {
        cout << u << endl;
        break;
      }
    }
  }

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

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

typedef unsigned int uint;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int, ii> edge;

#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mem(arr, val) memset(arr, val, sizeof arr)
#define sz(v) (int)v.size()
#define rsz(v) v.resize
#define clr(v) v.clear()
#define rev(v) reverse(all(v))
#define lop(i, end) for (int i = 0; i < end; i++)
#define rlop(i, start) for (int i = start - 1; i >= 0; i--)
#define loop(i, start, end) for (int i = start; i < end; i++)
#define rloop(i, start, end) for (int i = start - 1; i >= end; i--)
#define PB push_back
#define pb pop_back
#define mP make_pair
#define f first
#define s second

const ll OO = (ll)(4e18) + 9;
const int MOD = (int)(1e9) + 7;
const int oo = 2147483647;
const double EPS = 1e-8;
const double PI = acos(-1.0);

// enum dir { d, r, u, l , dr, ur, dl, ul };
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

string abc = "abcdefghijklmnopqrstuvwxyz";
string vowels = "aeiouy"; // and sometimes "aeiouy"

template <typename X, typename Y> bool ckmin(X &a, Y b) {
  return a > b ? a = b, true : false;
}
template <typename X, typename Y> bool ckmax(X &a, Y b) {
  return a < b ? a = b, true : false;
}

int dcmp(ld d1, ld d2) {
  return fabs(d1 - d2) <= EPS ? 0 : d1 > d2 ? 1 : -1;
} // Compare Double Numbers
ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
// convert index from 2D array to 1D array. (Zero-indexed)
int conv_2_1(int r, int c, int nCol) { return (nCol * r) + c + 1; }
// convert index from 1D array to 2D array. (Zero-indexed)
ii conv_1_2(int n, int nCol) {
  int r = n / nCol, c = n % nCol;
  return ii(r, c);
}

void inter(string s) {
  cout << s << endl;
  fflush(stdout);
}

ll power(ll base, ll exp) {
  ll ans = 1;
  base %= MOD;
  while (exp > 0) {
    if (exp & 1)
      ans = (ans * base) % MOD;
    exp >>= 1;
    base = (base * base) % MOD;
  }
  return ans;
}

ll modInverse(ll a) { return power(a, MOD - 2); }

ll fact[2000010];

void init() {
  fact[0] = 1;
  for (int i = 1; i < 2000010; ++i)
    fact[i] = (fact[i - 1] * 1LL * i) % MOD;
}

ll nCr(ll n, ll r) {
  return (fact[n] * modInverse(fact[r] * 1LL * fact[n - r])) % MOD;
}

const int N = 1e5 + 10;
vector<int> adj[N];
int dis[N]; // dis[v] : holds the shortest distance between the source and node
            // "v".

void bfs(int src) {
  queue<int> q;
  q.push(src);

  memset(dis, -1, sizeof(dis));

  dis[src] = 0;

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

    for (int v : adj[u]) {
      if (dis[v] == -1) {
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE

#else
  // freopen("pyramid.in", "r", stdin);
#endif
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, u, v;
  cin >> n >> m;
  lop(i, m) cin >> u >> v, adj[u].PB(v), adj[v].PB(u);
  bfs(1);
  for (int i = 2; i <= n; i++)
    if (dis[i] == -1)
      return puts("No");
  cout << "Yes\n";
  loop(i, 2, n + 1) {
    for (int u : adj[i]) {
      if (dis[i] == dis[u] + 1) {
        cout << u << endl;
        break;
      }
    }
  }

  return 0;
}