Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define quickie                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, a, b) for (int i = a; i <= b; i++)
#define repp(i, a, b) for (int i = b - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define db double
#define mi map<int, int>
#define vi vector<int>
#define MI(x) power(x, mod - 2)
#define test                                                                   \
  int t;                                                                       \
  cin >> t;
#define mod 1000000007LL
#define pi 3.141592653589
// #define mod 998244353
using namespace std;

int power(int x, int y);
int gcd(int a, int b);

/*___________________________TEMPLATE END______________________________*/

vi v[10001];
bool vis[10001];
int dis[10001];

void bfs(int src) {
  queue<int> q;
  q.push(src);
  vis[src] = true;
  dis[src] = 0;
  while (!q.empty()) {
    int curr = q.front();
    q.pop();
    for (int i = 0; i < v[curr].size(); i++) {
      if (!vis[v[curr][i]]) {
        q.push(v[curr][i]);
        dis[v[curr][i]] = dis[curr] + 1;
        vis[v[curr][i]] = true;
      }
    }
  }
}

signed main() {
  quickie int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  bfs(1);
  rep(i, 2, n + 1) {
    if (dis[i] == 0) {
      cout << "No\n";
      return 0;
    }
  }
  cout << "Yes\n";
  rep(i, 2, n + 1) {
    for (auto it : v[i]) {
      if (dis[it] == dis[i] - 1) {
        cout << it << "\n";
        break;
      }
    }
  }
}

int power(int x, int y) {
  int res = 1;
  x %= mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define quickie                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, a, b) for (int i = a; i <= b; i++)
#define repp(i, a, b) for (int i = b - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define db double
#define mi map<int, int>
#define vi vector<int>
#define MI(x) power(x, mod - 2)
#define test                                                                   \
  int t;                                                                       \
  cin >> t;
#define mod 1000000007LL
#define pi 3.141592653589
// #define mod 998244353
using namespace std;

int power(int x, int y);
int gcd(int a, int b);

/*___________________________TEMPLATE END______________________________*/

vi v[1000001];
bool vis[1000001];
int dis[1000001];

void bfs(int src) {
  queue<int> q;
  q.push(src);
  vis[src] = true;
  dis[src] = 0;
  while (!q.empty()) {
    int curr = q.front();
    q.pop();
    for (int i = 0; i < v[curr].size(); i++) {
      if (!vis[v[curr][i]]) {
        q.push(v[curr][i]);
        dis[v[curr][i]] = dis[curr] + 1;
        vis[v[curr][i]] = true;
      }
    }
  }
}

signed main() {
  quickie int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  bfs(1);
  rep(i, 2, n + 1) {
    if (dis[i] == 0) {
      cout << "No\n";
      return 0;
    }
  }
  cout << "Yes\n";
  rep(i, 2, n + 1) {
    for (auto it : v[i]) {
      if (dis[it] == dis[i] - 1) {
        cout << it << "\n";
        break;
      }
    }
  }
}

int power(int x, int y) {
  int res = 1;
  x %= mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}