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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define fi first
#define se second
const int N = 1e6 + 10;
const int mod7 = 1e9 + 7;
const int mod = 1e9 + 7;
const int maxn = 0x3fffffff;
const double pi = 3.14159265357;
void read(int &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
void read(ll &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
struct note {
  int next;
  int to;
  int dis;
} edge[N];
int num = 0, dis[N], head[N], ans[N];
bool vis[N];
void add(int from, int to, int dis) {
  edge[++num].next = head[from];
  edge[num].to = to;
  edge[num].dis = dis;
  head[from] = num;
  edge[++num].next = head[to];
  edge[num].to = from;
  edge[num].dis = dis;
  head[to] = num;
}
struct node {
  int dis;
  int pos;
  bool operator<(const node &x) const { return x.dis < dis; }
};
priority_queue<node> q;
inline void dijkstra(int st, int n) {
  for (register int i = 1; i <= n; i++)
    dis[i] = maxn;
  dis[st] = 0;
  q.push((node){0, st});
  while (!q.empty()) {
    node x = q.top();
    q.pop();
    int d = x.dis;
    int p = x.pos;
    if (vis[p])
      continue;
    vis[p] = true;
    for (register int i = head[p]; i; i = edge[i].next) {
      int t = edge[i].to;
      if (dis[t] > dis[p] + edge[i].dis) {
        dis[t] = dis[p] + edge[i].dis;
        if (!vis[t]) {
          ans[t] = p;
          q.push((node){dis[t], t});
        }
      }
    }
  }
}
int main() {
  int n, m;
  read(n);
  read(m);
  for (re int i = 1; i <= m; i++) {
    int a, b;
    read(a);
    read(b);
    add(a, b, 1);
  }
  dijkstra(1, n);
  bool f = 1;
  for (re int i = 2; i <= n; i++)
    if (dis[i] == 0x3fffffff)
      f = 0;
  if (f) {
    puts("Yes");
    for (re int i = 2; i <= n; i++) {
      int k = i;
      printf("%d\\n", ans[k]);
    }
  } else
    puts("No");
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define fi first
#define se second
const int N = 1e6 + 10;
const int mod7 = 1e9 + 7;
const int mod = 1e9 + 7;
const int maxn = 0x3fffffff;
const double pi = 3.14159265357;
void read(int &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
void read(ll &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-')
      d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
struct note {
  int next;
  int to;
  int dis;
} edge[200005];
int num = 0, dis[100005], head[100005];
bool vis[100005];
void add(int from, int to, int dis) {
  edge[++num].next = head[from];
  edge[num].to = to;
  edge[num].dis = dis;
  head[from] = num;
  edge[++num].next = head[to];
  edge[num].to = from;
  edge[num].dis = dis;
  head[to] = num;
}
struct node {
  int dis;
  int pos;
  bool operator<(const node &x) const { return x.dis < dis; }
};
priority_queue<node> q;
inline void dijkstra(int st, int n) {
  for (register int i = 1; i <= n; i++)
    dis[i] = maxn;
  dis[st] = 0;
  q.push((node){0, st});
  while (!q.empty()) {
    node x = q.top();
    q.pop();
    int d = x.dis;
    int p = x.pos;
    if (vis[p])
      continue;
    vis[p] = true;
    for (register int i = head[p]; i; i = edge[i].next) {
      int t = edge[i].to;
      if (dis[t] > dis[p] + edge[i].dis) {
        dis[t] = dis[p] + edge[i].dis;
        // if(!vis[t])
        q.push((node){dis[t], t});
      }
    }
  }
}
int main() {
  int n, m;
  read(n);
  read(m);
  for (re int i = 1; i <= m; i++) {
    int a, b;
    read(a);
    read(b);
    add(a, b, 1);
  }
  dijkstra(1, n);
  bool f = 1;
  for (re int i = 2; i <= n; i++)
    if (dis[i] == 0x3fffffff)
      f = 0;
  if (f) {
    puts("Yes");
    for (re int i = 2; i <= n; i++)
      printf("%d\\n", dis[i]);
  } else
    puts("No");
  return 0;
}