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

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define INF 1000000007 // 1000000000000000003
#define MOD 1000000007
#define MAX 2000005

#define ll long long

// printf("%.10f", );
// cout << fixed << setprecision(10) << heikin << endl;
// vector<vector<int>> data(3, vector<int>(4, -1));
// left + 1 < top

using namespace std;
using P = pair<int, int>;
using Pc = pair<double, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to;
  long long dist;
};
const double PI = 3.141592653589793;
long long fact[MAX], inv[MAX];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

//////////////////////////////////

vector<vector<int>> bingo(3, vector<int>(3));
vector<vector<int>> tree(100005);
int color[100005];
map<P, int> colorMap;
int m = 0;

struct Connection {
  vector<int> connect;
  int parent = INF;
  int dep = INF;
  Connection() {}
};

bool compare_by_second(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

struct UnionFind {
  vector<int> uf;

  UnionFind(int size) : uf(size, -1){};

  int root(int target) {
    if (uf[target] < 0)
      return target;
    else
      return uf[target] = root(uf[target]);
  }

  void merge(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return;
    if (uf[b] < uf[a])
      swap(a, b);
    uf[a] += uf[b];
    uf[b] = a;
  }
};

ll CalcPow(ll num, ll power) {
  if (power == 0)
    return 1;
  if (power % 2 == 0) {
    ll half = CalcPow(num, power / 2);
    return (half * half) % MOD;
  } else
    return (num * CalcPow(num, power - 1)) % MOD;
}

ll combination(ll all, ll select) {
  if (all < select)
    return 0;
  if (all < 0 || select < 0)
    return 0;
  return fact[all] * (inv[select] * inv[all - select] % MOD) % MOD;
}

void initCombination(int max) {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= max; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  inv[max] = CalcPow(fact[max], MOD - 2);
  for (int i = max - 1; 0 <= i; --i) {
    inv[i] = (inv[i + 1] * (i + 1)) % MOD;
  }
}

ll getGcd(ll a, ll b) {
  if (a < b) {
    ll temp = b;
    b = a;
    a = temp;
  }

  ll r;
  while ((r = a % b) != 0) {
    a = b;
    b = r;
  }
  return b;
}

/*int getNum(ll target) {

        int bottom = 0;
        int top = 151;
        while (1 < top - bottom) {
                int middle = (top + bottom) / 2;
                if (target < line[middle])top = middle;
                else bottom = middle;
        }
        return bottom;
}*/

Connection connection[100005];
int co = 0;

void dfs(int depth, int next, int parent) {
  ++depth;
  for (int c : connection[next].connect) {
    if (c == parent)
      continue;
    if (depth < connection[c].dep) {
      if (connection[c].dep == INF)
        co++;
      connection[c].dep = depth;
      connection[c].parent = next;
    } else if (connection[c].parent == next) {
      continue;
    }
    dfs(depth, c, next);
  }
}

int main() {

  int N, M;
  cin >> N >> M;
  map<int, int> parent;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    connection[a].connect.emplace_back(b);
    connection[b].connect.emplace_back(a);
  }

  connection[0].dep = -1;
  connection[0].parent = -1;

  queue<int> que;
  que.push(0);
  int depth = -1;
  while (!que.empty()) {
    depth++;
    int current = que.front();
    que.pop();
    for (int c : connection[current].connect) {
      if (connection[c].dep < depth)
        continue;
      if (connection[c].dep == INF)
        co++;
      connection[c].dep = depth;
      connection[c].parent = current;
      que.push(c);
    }
  }

  if (co == N - 1) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; ++i) {
      cout << connection[i].parent + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define INF 1000000007 // 1000000000000000003
#define MOD 1000000007
#define MAX 2000005

#define ll long long

// printf("%.10f", );
// cout << fixed << setprecision(10) << heikin << endl;
// vector<vector<int>> data(3, vector<int>(4, -1));
// left + 1 < top

using namespace std;
using P = pair<int, int>;
using Pc = pair<double, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to;
  long long dist;
};
const double PI = 3.141592653589793;
long long fact[MAX], inv[MAX];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

//////////////////////////////////

vector<vector<int>> bingo(3, vector<int>(3));
vector<vector<int>> tree(100005);
int color[100005];
map<P, int> colorMap;
int m = 0;

struct Connection {
  vector<int> connect;
  int parent = INF;
  int dep = INF;
  Connection() {}
};

bool compare_by_second(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

struct UnionFind {
  vector<int> uf;

  UnionFind(int size) : uf(size, -1){};

  int root(int target) {
    if (uf[target] < 0)
      return target;
    else
      return uf[target] = root(uf[target]);
  }

  void merge(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return;
    if (uf[b] < uf[a])
      swap(a, b);
    uf[a] += uf[b];
    uf[b] = a;
  }
};

ll CalcPow(ll num, ll power) {
  if (power == 0)
    return 1;
  if (power % 2 == 0) {
    ll half = CalcPow(num, power / 2);
    return (half * half) % MOD;
  } else
    return (num * CalcPow(num, power - 1)) % MOD;
}

ll combination(ll all, ll select) {
  if (all < select)
    return 0;
  if (all < 0 || select < 0)
    return 0;
  return fact[all] * (inv[select] * inv[all - select] % MOD) % MOD;
}

void initCombination(int max) {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= max; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  inv[max] = CalcPow(fact[max], MOD - 2);
  for (int i = max - 1; 0 <= i; --i) {
    inv[i] = (inv[i + 1] * (i + 1)) % MOD;
  }
}

ll getGcd(ll a, ll b) {
  if (a < b) {
    ll temp = b;
    b = a;
    a = temp;
  }

  ll r;
  while ((r = a % b) != 0) {
    a = b;
    b = r;
  }
  return b;
}

/*int getNum(ll target) {

        int bottom = 0;
        int top = 151;
        while (1 < top - bottom) {
                int middle = (top + bottom) / 2;
                if (target < line[middle])top = middle;
                else bottom = middle;
        }
        return bottom;
}*/

Connection connection[100005];
int co = 0;

void dfs(int depth, int next, int parent) {
  ++depth;
  for (int c : connection[next].connect) {
    if (c == parent)
      continue;
    if (depth < connection[c].dep) {
      if (connection[c].dep == INF)
        co++;
      connection[c].dep = depth;
      connection[c].parent = next;
    } else if (connection[c].parent == next) {
      continue;
    }
    dfs(depth, c, next);
  }
}

int main() {

  int N, M;
  cin >> N >> M;
  map<int, int> parent;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    connection[a].connect.emplace_back(b);
    connection[b].connect.emplace_back(a);
  }

  connection[0].dep = -1;
  connection[0].parent = -1;
  dfs(-1, 0, -1);

  if (co == N - 1) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; ++i) {
      cout << connection[i].parent + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
}