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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define ll long long
#define R register
#define IL inline
#define Rf(a, b, c) for (R int(a) = (b); (a) <= (c); ++(a))
#define Tf(a, b, c) for (R int(a) = (b); (a) >= (c); --(a))
#define MP make_pair
#define PA pair<int, int>
#define MES(a, b) memset((a), (b), sizeof((a)))
#define MEC(a, b) memcpy((a), (b), sizeof((b)))
#define D double

using namespace std;

const int N = 1e5 + 5;

int n, m, head[N], tot, vis[N], dis[N], fa[N];
queue<int> q;
struct Edge {
  int next, to;
} e[4 * N];

IL int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x *= 10;
    x += (ch - '0');
    ch = getchar();
  }
  return x * f;
}
IL void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

void add(int u, int v) {
  e[++tot].next = head[u];
  head[u] = tot;
  e[tot].to = v;
}

void spfa() {
  q.push(1);
  vis[1] = 1;
  dis[1] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    vis[now] = 0;
    for (int i = head[now]; i; i = e[i].next) {
      int p = e[i].to;
      if (dis[p] > dis[now] + 1) {
        dis[p] = dis[now] + 1;
        fa[p] = now;
        q.push(p);
        vis[p] = 1;
      }
    }
  }
}

signed main() {
  n = read();
  m = read();
  Rf(i, 2, n) dis[i] = N;
  Rf(i, 1, m) {
    R int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  spfa();
  R int fl = 0;
  Rf(i, 1, n) if (dis[i] == N) fl = 1;
  if (fl) {
    puts("No");
    return 0;
  }
  puts("Yes");
  Rf(i, 2, n) printf("%d\\n", fa[i]);

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define ll long long
#define R register
#define IL inline
#define Rf(a, b, c) for (R int(a) = (b); (a) <= (c); ++(a))
#define Tf(a, b, c) for (R int(a) = (b); (a) >= (c); --(a))
#define MP make_pair
#define PA pair<int, int>
#define MES(a, b) memset((a), (b), sizeof((a)))
#define MEC(a, b) memcpy((a), (b), sizeof((b)))
#define D double

using namespace std;

const int N = 2e5 + 5;

int n, m, head[N], tot, vis[N], dis[N], fa[N];
queue<int> q;
struct Edge {
  int next, to;
} e[N];

IL int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x *= 10;
    x += (ch - '0');
    ch = getchar();
  }
  return x * f;
}
IL void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

void add(int u, int v) {
  e[++tot].next = head[u];
  head[u] = tot;
  e[tot].to = v;
}

void spfa() {
  q.push(1);
  vis[1] = 1;
  dis[1] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    vis[now] = 0;
    for (int i = head[now]; i; i = e[i].next) {
      int p = e[i].to;
      if (dis[p] > dis[now] + 1) {
        dis[p] = dis[now] + 1;
        fa[p] = now;
        q.push(p);
        vis[p] = 1;
      }
    }
  }
}

signed main() {
  n = read();
  m = read();
  Rf(i, 2, n) dis[i] = N;
  Rf(i, 1, m) {
    R int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  spfa();
  R int fl = 0;
  Rf(i, 1, n) if (dis[i] == N) fl = 1;
  if (fl) {
    puts("No");
    return 0;
  }
  puts("Yes");
  Rf(i, 2, n) printf("%d\\n", fa[i]);

  return 0;
}