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

/*************************************************************************
        > File Name: 1.cpp
        > Author: Knowledge_llz
        > Mail: 925538513@qq.com
        > Blog: https://blog.csdn.net/Pig_cfbsl
        > Created Time: 2020/5/17 20:32:34
 ************************************************************************/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define For(i, a, b) for (register int i = (a); i <= (b); ++i)
#define LL long long
using namespace std;
int read() {
  char x = getchar();
  int u = 0, fg = 0;
  while (!isdigit(x)) {
    if (x == '-')
      fg = 1;
    x = getchar();
  }
  while (isdigit(x)) {
    u = (u << 3) + (u << 1) + (x ^ 48);
    x = getchar();
  }
  return fg ? -u : u;
}
const int maxx = 2e5 + 10;
int n, m, e = 0, to[maxx << 1], be[maxx], ne[maxx << 1], dis[maxx],
          q[maxx << 4];
bool vis[maxx];
void add(int x, int y) {
  to[++e] = y;
  ne[e] = be[x];
  be[x] = e;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif
  int u, v, f = 0, l = 0;
  n = read();
  m = read();
  For(i, 1, m) {
    u = read();
    v = read();
    add(u, v);
    add(v, u);
  }
  q[++l] = 1;
  vis[1] = 1;
  while (f < l) {
    int now = q[++f];
    for (int i = be[now]; i; i = ne[i]) {
      int go = to[i];
      if (vis[go])
        continue;
      vis[go] = 1;
      dis[go] = now;
      q[++l] = go;
    }
  }
  For(i, 2, n) {
    if (!dis[i]) {
      printf("No");
      return 0;
    }
  }
  printf("Yes\\n");
  For(i, 2, n) printf("%d\\n", dis[i]);

  return 0;
}


Error: TLE

Fixed submission:

/*************************************************************************
        > File Name: 1.cpp
        > Author: Knowledge_llz
        > Mail: 925538513@qq.com
        > Blog: https://blog.csdn.net/Pig_cfbsl
        > Created Time: 2020/5/17 20:32:34
 ************************************************************************/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define For(i, a, b) for (register int i = (a); i <= (b); ++i)
#define LL long long
using namespace std;
int read() {
  char x = getchar();
  int u = 0, fg = 0;
  while (!isdigit(x)) {
    if (x == '-')
      fg = 1;
    x = getchar();
  }
  while (isdigit(x)) {
    u = (u << 3) + (u << 1) + (x ^ 48);
    x = getchar();
  }
  return fg ? -u : u;
}
const int maxx = 1e5 + 10;
int n, m, e = 0, to[maxx << 1], be[maxx << 1], ne[maxx << 1], dis[maxx],
          q[maxx << 4];
bool vis[maxx];
void add(int x, int y) {
  to[++e] = y;
  ne[e] = be[x];
  be[x] = e;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif
  int u, v, f = 0, l = 0;
  n = read();
  m = read();
  For(i, 1, m) {
    u = read();
    v = read();
    add(u, v);
    add(v, u);
  }
  q[++l] = 1;
  vis[1] = 1;
  while (f < l) {
    int now = q[++f];
    for (int i = be[now]; i; i = ne[i]) {
      int go = to[i];
      if (vis[go])
        continue;
      vis[go] = 1;
      dis[go] = dis[now] + 1;
      q[++l] = go;
    }
  }
  For(i, 2, n) {
    if (!dis[i]) {
      printf("No");
      return 0;
    }
  }
  printf("Yes\\n");
  For(i, 2, n) printf("%d\\n", dis[i]);

  return 0;
}