Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define rush()                                                                 \\
  int T;                                                                       \\
  scanf("%d", &T);                                                             \\
  while (T--)
#define mm(a, b) memset(a, b, sizeof(a))
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define pf(a) printf("%d\\n", a)
#define pf2(a, b) printf("%d %d\\n", a, b)
#define p_f(a) printf("%d ", a)
#define pyn(a)                                                                 \\
  if (a)                                                                       \\
    puts("Yes");                                                               \\
  else                                                                         \\
    puts("No");
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const db eps = 1e-9;
const int N = 1e5 + 5, M = N << 2;
struct edge {
  int u, v, w, next;
} e[M];
int head[N], cnt;
void add_edges(int u, int v, int w) {
  e[++cnt].u = u;
  e[cnt].v = v;
  e[cnt].w = w;
  e[cnt].next = head[u];
  head[u] = cnt;
  e[++cnt].u = v;
  e[cnt].v = u;
  e[cnt].w = w;
  e[cnt].next = head[v];
  head[v] = cnt;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    heap;
int n, m, u, v, dist[N], pre[N];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    add_edges(u, v, 1);
  }
  mm(dist, inf);
  dist[1] = 0;
  heap.push({0, 1});
  while (!heap.empty()) {
    pair<int, int> cur = heap.top();
    heap.pop();
    int u = cur.second;
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].v, w = e[i].w;
      if (dist[v] > dist[u] + w) {
        dist[v] = dist[u] + w;
        pre[v] = u;
        heap.push({dist[v], v});
      }
    }
  }
  int i;
  for (i = 1; i <= n; i++)
    if (dist[i] == inf)
      break;
  if (i != n + 1)
    puts("No");
  else {
    puts("Yes");
    for (i = 2; i <= n; i++)
      cout << pre[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define rush()                                                                 \\
  int T;                                                                       \\
  scanf("%d", &T);                                                             \\
  while (T--)
#define mm(a, b) memset(a, b, sizeof(a))
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define pf(a) printf("%d\\n", a)
#define pf2(a, b) printf("%d %d\\n", a, b)
#define p_f(a) printf("%d ", a)
#define pyn(a)                                                                 \\
  if (a)                                                                       \\
    puts("Yes");                                                               \\
  else                                                                         \\
    puts("No");
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const db eps = 1e-9;
const int N = 1e5 + 5, M = N << 1;
struct edge {
  int u, v, w, next;
} e[M];
int head[N], cnt;
void add_edges(int u, int v, int w) {
  e[++cnt].u = u;
  e[cnt].v = v;
  e[cnt].w = w;
  e[cnt].next = head[u];
  head[u] = cnt;
  e[++cnt].u = v;
  e[cnt].v = u;
  e[cnt].w = w;
  e[cnt].next = head[v];
  head[v] = cnt;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    heap;
int n, m, u, v, dist[N], pre[N];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    add_edges(u, v, 1);
  }
  mm(dist, inf);
  dist[1] = 0;
  heap.push({0, 1});
  while (!heap.empty()) {
    pair<int, int> cur = heap.top();
    heap.pop();
    int u = cur.second;
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].v, w = e[i].w;
      if (dist[v] > dist[u] + w) {
        dist[v] = dist[u] + w;
        pre[v] = u;
        heap.push({dist[v], v});
      }
    }
  }
  int i;
  for (i = 1; i <= n; i++)
    if (dist[i] == inf)
      break;
  if (i != n + 1)
    puts("No");
  else {
    puts("Yes");
    for (i = 2; i <= n; i++)
      cout << pre[i] << endl;
  }
  return 0;
}