Problem p02678 (C++)

Buggy submission (status: Runtime Error):
// Author: pankaj_m05
// Problem: D
// Created: 17.05.2020 18:20:52

#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define f_in(file) freopen(file, "r", stdin)
#define f_out(file) freopen(file, "w", stdout)
#define sz(a) int((a).size())
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) c.begin(), c.end()
#define tr(c, i) for (typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c, x) (c.find(x) != c.end())      // Set and Maps
#define cpresent(c, x) (find(allc, x) != c.end()) // Vectors
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const int mxN = 1e5;

int n;
vector<int> adj[mxN + 1];
bool vis[mxN];
int sign[mxN] = {0};

void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    // cout << v << " ";
    if (!vis[v]) {
      vis[v] = 1;
      for (int u : adj[v]) {
        q.push(u);
        if (sign[u] == 0)
          sign[u] = v;
      }
    }
  }
}

int main(void) {
  fast int m;
  bool flag = 1;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
    if (u == 1 || v == 1)
      flag = 0;
  }
  if (flag) {
    cout << "No" << '\n';
    return 0;
  }
  cout << "Yes" << '\n';
  bfs();
  for (int i = 2; i <= n; ++i) {
    cout << sign[i] << '\n';
  }
  return 0;
}

Fixed submission:
// Author: pankaj_m05
// Problem: D
// Created: 17.05.2020 18:20:52

#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define f_in(file) freopen(file, "r", stdin)
#define f_out(file) freopen(file, "w", stdout)
#define sz(a) int((a).size())
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) c.begin(), c.end()
#define tr(c, i) for (typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c, x) (c.find(x) != c.end())      // Set and Maps
#define cpresent(c, x) (find(allc, x) != c.end()) // Vectors
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const int mxN = 1e5;

int n;
vector<int> adj[mxN + 1];
bool vis[mxN + 1];
int sign[mxN + 1] = {0};

void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    // cout << v << " ";
    if (!vis[v]) {
      vis[v] = 1;
      for (int u : adj[v]) {
        q.push(u);
        if (sign[u] == 0)
          sign[u] = v;
      }
    }
  }
}

int main(void) {
  fast int m;
  bool flag = 1;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
    if (u == 1 || v == 1)
      flag = 0;
  }
  if (flag) {
    cout << "No" << '\n';
    return 0;
  }
  cout << "Yes" << '\n';
  bfs();
  for (int i = 2; i <= n; ++i) {
    cout << sign[i] << '\n';
  }
  return 0;
}