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

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
#define maxn 400005
using namespace std;
// using namespace __gnu_pbds;
const double PI = acos(-1.0);
const double eps = 1e-6;
struct Edge {
  int to, nxt;
} edge[maxn];
bool vis[maxn];
int cnt, head[maxn], ans[maxn];
void addedge(int u, int v) {
  edge[cnt].to = v;
  edge[cnt].nxt = head[u];
  head[u] = cnt++;
  return;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void bfs() {
  queue<int> q;
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int k = head[u]; k != -1; k = edge[k].nxt) {
      int v = edge[k].to;
      if (!vis[v]) {
        q.push(v);
        ans[v] = u;
        vis[v] = 1;
      }
    }
  }
  return;
}
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  int n = read(), m = read();
  memset(head, -1, sizeof(head));
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    addedge(u, v);
    addedge(v, u);
  }
  bfs();
  for (int i = 2; i <= n; i++) {
    if (ans[i] == 0) {
      printf("No\\n");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
#define maxn 200005
using namespace std;
// using namespace __gnu_pbds;
const double PI = acos(-1.0);
const double eps = 1e-6;
struct Edge {
  int to, nxt;
} edge[maxn];
bool vis[maxn];
int cnt, head[maxn], ans[maxn];
void addedge(int u, int v) {
  edge[cnt].to = v;
  edge[cnt].nxt = head[u];
  head[u] = cnt++;
  return;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void bfs() {
  queue<int> q;
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int k = head[u]; k != -1; k = edge[k].nxt) {
      int v = edge[k].to;
      if (!vis[v]) {
        q.push(v);
        ans[v] = u;
        vis[v] = 1;
      }
    }
  }
  return;
}
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  int n = read(), m = read();
  memset(head, -1, sizeof(head));
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    addedge(u, v);
    addedge(v, u);
  }
  bfs();
  for (int i = 2; i <= n; i++) {
    if (ans[i] == 0) {
      printf("No\\n");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}