Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

#define debug(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (int i = 0; i < (n); i++)
#define fori(a, b, i) for (int i = (a); i <= (b); i++)
#define ford(a, b, i) for (int i = (a); i >= (b); i--)
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL);

#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007

#define fir first
#define sec second
#define pb push_back
#define mp make_pair

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MII;
typedef set<int> SI;
typedef long long int llint;

llint n, e;
llint height[10000];

bool bfs(llint root, vector<llint> v[]) {
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  vis[root] = 1;
  queue<llint> q;
  q.push(root);
  while (!q.empty()) {
    llint x = q.front();
    q.pop();
    for (auto i : v[x]) {
      if (!vis[i]) {
        vis[i] = 1;
        q.push(i);
        height[i] = 1 + height[x];
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      return false;
  return true;
}

int main() {
  cin >> n >> e;
  vector<llint> v[n + 1];
  loop(i, e) {
    llint a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  if (!bfs(1, v)) {
    cout << "No";
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; ++i) {
    llint mdis = INF, ans = -1;
    for (auto j : v[i]) {
      if (height[j] < mdis) {
        mdis = height[j];
        ans = j;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

#define debug(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (int i = 0; i < (n); i++)
#define fori(a, b, i) for (int i = (a); i <= (b); i++)
#define ford(a, b, i) for (int i = (a); i >= (b); i--)
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL);

#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007

#define fir first
#define sec second
#define pb push_back
#define mp make_pair

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MII;
typedef set<int> SI;
typedef long long int llint;

llint n, e;
llint height[500000];

bool bfs(llint root, vector<llint> v[]) {
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  vis[root] = 1;
  queue<llint> q;
  q.push(root);
  while (!q.empty()) {
    llint x = q.front();
    q.pop();
    for (auto i : v[x]) {
      if (!vis[i]) {
        vis[i] = 1;
        q.push(i);
        height[i] = 1 + height[x];
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      return false;
  return true;
}

int main() {
  cin >> n >> e;
  vector<llint> v[n + 1];
  loop(i, e) {
    llint a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  if (!bfs(1, v)) {
    cout << "No";
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; ++i) {
    llint mdis = INF, ans = -1;
    for (auto j : v[i]) {
      if (height[j] < mdis) {
        mdis = height[j];
        ans = j;
      }
    }
    cout << ans << endl;
  }
  return 0;
}