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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 5e5 + 5;
struct Edge {
  int to, w, next;
} edge[N];

struct node {
  int id, d;
  node(int x, int y) {
    id = x;
    d = y;
  }
  bool operator<(const node &a) const { return d > a.d; }
};

int head[N];
int n, m, cnt;
void addedge(int u, int v, int w) {
  edge[cnt].to = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
int dis[N], pre[N];
bool done[N];
void init() {
  for (int i = 0; i < N; i++) {
    edge[i].next = -1;
    head[i] = -1;
  }
  cnt = 0;
}

void dijkstra() {
  int s = 1;
  for (int i = 1; i <= n; i++) {
    dis[i] = INF;
    done[i] = false;
  }
  dis[s] = 0;
  priority_queue<node> q;
  q.push(node(s, 0));
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (done[u.id])
      continue;
    done[u.id] = true;
    for (int i = head[u.id]; ~i; i = edge[i].next) {
      int v = edge[i].to, w = edge[i].w;
      if (done[v])
        continue;
      if (dis[v] > u.d + w) {
        dis[v] = u.d + w;
        q.push(node(v, dis[v]));
        pre[v] = u.id;
      }
    }
  }
  int f = 1;
  for (int i = 2; i <= n; i++) {
    if (dis[i] == INF) {
      puts("No\\n");
      f = 0;
    }
  }
  if (f == 1) {
    printf("Yes\\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", pre[i]);
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  init();
  while (m--) {
    int a, b, c;
    scanf("%d%d", &a, &b);
    addedge(a, b, 1);
    addedge(b, a, 1);
  }
  dijkstra();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 5;
struct Edge {
  int to, w, next;
} edge[N];

struct node {
  int id, d;
  node(int x, int y) {
    id = x;
    d = y;
  }
  bool operator<(const node &a) const { return d > a.d; }
};

int head[N];
int n, m, cnt;
void addedge(int u, int v, int w) {
  edge[cnt].to = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
int dis[N], pre[N];
bool done[N];
void init() {
  for (int i = 0; i < N; i++) {
    edge[i].next = -1;
    head[i] = -1;
  }
  cnt = 0;
}

void dijkstra() {
  int s = 1;
  for (int i = 1; i <= n; i++) {
    dis[i] = INF;
    done[i] = false;
  }
  dis[s] = 0;
  priority_queue<node> q;
  q.push(node(s, 0));
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (done[u.id])
      continue;
    done[u.id] = true;
    for (int i = head[u.id]; ~i; i = edge[i].next) {
      int v = edge[i].to, w = edge[i].w;
      if (done[v])
        continue;
      if (dis[v] > u.d + w) {
        dis[v] = u.d + w;
        q.push(node(v, dis[v]));
        pre[v] = u.id;
      }
    }
  }
  int f = 1;
  for (int i = 2; i <= n; i++) {
    if (dis[i] == INF) {
      puts("No\\n");
      f = 0;
    }
  }
  if (f == 1) {
    printf("Yes\\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", pre[i]);
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  init();
  while (m--) {
    int a, b, c;
    scanf("%d%d", &a, &b);
    addedge(a, b, 1);
    addedge(b, a, 1);
  }
  dijkstra();

  return 0;
}