alexjercan/bugnet huggingface dataset:
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#include <algorithm>
#include <map>
#include <string>
#include <vector>
typedef long long ll;
#include <cmath>
#include <iomanip>
#include <queue>
#include <set>
const int MOD = 1000000007;

ll POWER(ll x, ll y) {
  ll ret = 1;
  ll p = x;
  for (ll i = y; i > 0; i /= 2) {
    if (i % 2)
      ret = (ret * p) % MOD;
    p = (p * p) % MOD;
  }
  return ret;
}
ll gcm(ll a, ll b) {
  if (b > a) {
    ll c = a;
    a = b;
    b = c;
  }
  while (b > 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> c(n + 1, 0), d(n + 1);

  vector<set<int>> ab(n + 1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    ab[a].insert(b);
    ab[b].insert(a);
  }
  c[1] = 1;
  int rest = n - 1;
  queue<int> q;
  q.push(1);

  while (!q.empty()) {

    int a = q.front();
    q.pop();
    auto it = ab[a].begin();
    while (it != ab[a].end()) {
      if (c[*it] == 0) {
        c[*it] = c[a] + 1;
        d[*it] = a;
        q.push(*it);
      }
      it++;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << d[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#include <algorithm>
#include <map>
#include <string>
#include <vector>
typedef long long ll;
#include <cmath>
#include <iomanip>
#include <queue>
#include <set>
const int MOD = 1000000007;

ll POWER(ll x, ll y) {
  ll ret = 1;
  ll p = x;
  for (ll i = y; i > 0; i /= 2) {
    if (i % 2)
      ret = (ret * p) % MOD;
    p = (p * p) % MOD;
  }
  return ret;
}
ll gcm(ll a, ll b) {
  if (b > a) {
    ll c = a;
    a = b;
    b = c;
  }
  while (b > 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> c(n + 1, 0), d(n + 1);

  vector<set<int>> ab(n + 1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    ab[a].insert(b);
    ab[b].insert(a);
  }
  c[1] = 1;
  int rest = n - 1;
  queue<int> q;
  q.push(1);

  while (!q.empty()) {

    int a = q.front();

    auto it = ab[a].begin();
    while (it != ab[a].end()) {
      if (c[*it] == 0) {
        c[*it] = c[a] + 1;
        d[*it] = a;
        q.push(*it);
      }
      it++;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << d[i] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

// #pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, beg, end) for (int i = (beg); i < (end); i++)

struct cmp {
  template <typename T, typename U>
  bool operator()(T const &left, U const &right) {
    if (left.second < right.second)
      return true;
    return false;
  }
};

int edgeTo[100000 + 5];
int distTo[100000 + 5];
set<pair<int, int>> pq;
void relax(int from, int to, int weight) {
  if (distTo[to] > distTo[from] + weight) {
    pair<int, int> backup = {to, distTo[to]};
    distTo[to] = distTo[from] + weight;
    edgeTo[to] = from;
    if (pq.find(backup) != pq.end()) {
      pq.erase(backup);
      pq.insert({to, distTo[to]});
    } else {
      pq.insert({to, distTo[to]});
    }
  }
}

void dijsktra(vector<vector<pair<int, int>>> g, int s) {
  int n = g.size();
  for (int i = 0; i < n + 1; i++) {
    distTo[i] = 2147483647;
  }

  distTo[s] = 0;
  pq.insert({s, 0});
  while (!pq.empty()) {
    auto it = pq.end();
    it--;
    pair<int, int> top = *it;
    pq.erase(*it);
    for (pair<int, int> e : g[top.first]) {
      relax(top.first, e.first, e.second);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> graph(n + 1);
  rep(i, 1, m + 1) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back({b, 1});
    graph[b].push_back({a, 1});
  }
  dijsktra(graph, 1);

  cout << "Yes" << endl;
  rep(i, 2, n + 1) { cout << edgeTo[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, beg, end) for (int i = (beg); i < (end); i++)

struct cmp {
  template <typename T, typename U>
  bool operator()(T const &left, U const &right) {
    if (left.second < right.second)
      return true;
    return false;
  }
};

int edgeTo[100000 + 5];
int distTo[100000 + 5];
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
void relax(int from, int to, int weight) {
  if (distTo[to] > distTo[from] + weight) {
    distTo[to] = distTo[from] + weight;
    edgeTo[to] = from;
    pq.push({to, distTo[to]});
  }
}

void dijsktra(vector<vector<pair<int, int>>> g, int s) {
  int n = g.size();
  for (int i = 0; i < n + 1; i++) {
    distTo[i] = 2147483647;
  }

  distTo[s] = 0;
  pq = {};
  pq.push({s, 0});
  while (!pq.empty()) {
    pair<int, int> top = pq.top();
    pq.pop();
    int v = top.first;
    if (top.second > distTo[v])
      continue;

    for (pair<int, int> e : g[v]) {
      relax(v, e.first, e.second);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> graph(n + 1);
  rep(i, 1, m + 1) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back({b, 1});
    graph[b].push_back({a, 1});
  }
  dijsktra(graph, 1);

  cout << "Yes" << endl;
  rep(i, 2, n + 1) { cout << edgeTo[i] << endl; }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n, m;
vector<vector<int>> G;
int ans[101010];

int main() {
  cin >> n >> m;
  G.assign(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  for (int i = 0; i <= n; i++)
    ans[i] = -1;
  queue<int> que;
  que.emplace(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (ans[nv] != -1)
        continue;
      ans[nv] = v;
      que.emplace(nv);
    }
  }

  for (int i = 1; i < n; i++) {
    if (ans[i] == -1) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;

int n, m;
vector<vector<int>> G;
int dist[101010];
int ans[101010];
void dfs(int v, int pv) {
  for (auto nv : G[v]) {
    if (nv == pv)
      continue;
    if (dist[nv] <= dist[v] + 1)
      continue;
    dist[nv] = dist[v] + 1;
    ans[nv] = v;
    dfs(nv, v);
  }
}

int main() {
  cin >> n >> m;
  G.assign(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  for (int i = 0; i < n; i++)
    dist[i] = INF;
  dist[0] = 0;
  dfs(0, -1);
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef long int li;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<ll, ll> P;
#define PI 3.14159265358979
#define MOD 998244353
const int MAX = 510000;

ll N, M;
ll mark[100000];
ll visited[100000];
ll length[100000]; // 0からの距離
vector<vector<ll>> E(100000);
int flag = 0;

void dfs(int k, int from) {
  if (k == 0 && from != 0) {
    queue<P> qu;
    return;
  } else {
    for (auto t : E[k]) {
      if (visited[t] != -1) {
        if (::length[k] + 1 < ::length[t]) {
          ::length[t] = ::length[k] + 1;
          ::mark[t] = k;
          dfs(t, k);
        }
      } else {
        ::mark[t] = k;
        ::visited[t] = k;
        ::length[t] = length[k] + 1;
        dfs(t, k);
      }
    }
  }
}

int main() {
  cin >> N >> M;
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }

  rep(i, N) {
    ::mark[i] = -1;
    ::visited[i] = -1;
    ::length[i] = 0;
  }
  ::mark[0] = 0;
  ::visited[0] = -2;
  queue<P> qu;
  qu.push(P(0, 0));
  while (!qu.empty()) {
    auto x = qu.front();
    qu.pop();
    int k = x.first;
    for (auto t : E[k]) {
      if (visited[t] != -1) {
        if (::length[k] + 1 < ::length[t]) {
          ::length[t] = ::length[k] + 1;
          ::mark[t] = k;
        }
      } else {
        ::mark[t] = k;
        ::visited[t] = k;
        ::length[t] = length[k] + 1;
        qu.push(P(t, k));
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << mark[i] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef long int li;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<ll, ll> P;
#define PI 3.14159265358979
#define MOD 998244353
const int MAX = 510000;

ll N, M;
ll mark[100000];
ll visited[100000];
ll length[100000]; // 0からの距離
vector<vector<ll>> E(100000);
int flag = 0;

void dfs(int k, int from) {
  if (k == 0 && from != 0) {
    return;
  } else {
    for (auto t : E[k]) {
      if (visited[t] != -1) {
        if (::length[k] + 1 < ::length[t]) {
          ::length[t] = ::length[k] + 1;
          ::mark[t] = k;
          dfs(t, k);
        }
      } else {
        ::mark[t] = k;
        ::visited[t] = k;
        ::length[t] = length[k] + 1;
        dfs(t, k);
      }
    }
  }
}

int main() {
  cin >> N >> M;
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }

  rep(i, N) {
    ::mark[i] = -1;
    ::visited[i] = -1;
    ::length[i] = 0;
  }
  ::mark[0] = 0;
  ::visited[0] = -2;
  dfs(0, 0);
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << mark[i] + 1 << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;
#define rep(i, n)                                                              \\
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！

vector<int> to[100005];
const int INF = 100100100;

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;                // 頂点番号をデクリメント
    to[a].push_back(b); // b->a
    to[b].push_back(a); // a->b
  }

  queue<int> q;
  vector<int> dist(n, INF); // 距離を記録
  vector<int> pre(n, -1);   // どこから来たか記録
  dist[0] = 0;
  q.push(0); // 0を入れる
  while (!q.empty()) {
    int v = q.front();
    q.pop();              // 頂点を先頭から取り出す
    for (int u : to[v]) { // vにつながっている頂点を全探索
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v; // ここがいつものBFSと違う
      q.push(u);  // uをqueueに追加
    }
  }

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++; // 戻さないと
    cout << ans << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！
#define mod 1000000007
#define rep(i, n)                                                              \\
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！
const double pi = 3.14159265359;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++) {
      par[i] = i;
    }
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x) {
      return x;
    }
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry) {
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    }
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M;
  UnionFind tree(N);
  vector<int> par(N);

  vector<int> A(M), B(M);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    tree.unite(A[i] - 1, B[i] - 1);
    par[A[i] - 1] = B[i];
  }

  bool can = true;
  for (int i = 1; i < N; i++) {
    if (!tree.same(i, 0)) {
      cout << "No" << endl;
      can = false;
      break;
    }
  }

  if (can) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << par[i] << endl;
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int(i) = 1; (i) <= (n); (i)++)

using namespace std;

int main(int argc, char const *argv[]) {
  size_t N, M;
  cin >> N >> M;
  vector<pair<int, int>> room(N + 1);
  for (auto &elem : room) {
    elem = make_pair(-1, -1);
  }
  room[1] = make_pair(-1, 0);
  unordered_map<size_t, vector<size_t>> room_dict;
  rep(i, M) {
    size_t A, B;
    cin >> A >> B;
    room_dict[A].push_back(B);
    room_dict[B].push_back(A);
  }

  queue<size_t> q;
  unordered_set<size_t> memory;
  unordered_set<size_t> tabu;
  q.push(1);

  while (!q.empty()) {
    size_t i = q.front();
    q.pop();
    memory.insert(i);
    size_t order = room[i].second;
    if (tabu.find(i) == tabu.end()) {
      for (size_t adj_i : room_dict[i]) {
        if (room[adj_i].second == -1 || order < room[adj_i].second) {
          room[adj_i] = make_pair(i, order + 1);
          q.push(adj_i);
        }
      }
    }
    tabu.insert(i);
  }

  if (memory.size() != N) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << room[i].first << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int(i) = 1; (i) <= (n); (i)++)

using namespace std;

int main(int argc, char const *argv[]) {
  size_t N, M;
  cin >> N >> M;
  vector<pair<int, int>> room(N + 1);
  for (auto &elem : room) {
    elem = make_pair(-1, -1);
  }
  room[1] = make_pair(-1, 0);
  unordered_map<size_t, vector<size_t>> room_dict;
  rep(i, M) {
    size_t A, B;
    cin >> A >> B;
    room_dict[A].push_back(B);
    room_dict[B].push_back(A);
  }

  queue<size_t> q;
  unordered_set<size_t> memory;
  q.push(1);

  while (!q.empty()) {
    size_t i = q.front();
    q.pop();
    memory.insert(i);
    size_t order = room[i].second;
    for (size_t adj_i : room_dict[i]) {
      if (room[adj_i].second == -1 || order < room[adj_i].second) {
        room[adj_i] = make_pair(i, order + 1);
        q.push(adj_i);
      }
    }
  }

  if (memory.size() != N) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << room[i].first << endl;
    }
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> G(n + 1, vector<int>());
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(n + 1), dist(n + 1, INF);
  queue<int> que;
  que.push(1);
  dist[1] = 0;
  while (que.size() != 0) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] > dist[v] + 1) {
        que.push(nv);
        dist[nv] = dist[v] + 1;
        ans[nv] = v;
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> G(n + 1, vector<int>());
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> dist(n + 1, INF);
  vector<int> ans(n + 1);

  dist[1] = 0;
  function<void(int, int, int)> dfs = [&](int v, int p, int d) {
    for (auto nv : G[v]) {
      if (nv == p)
        continue;
      if (dist[nv] > d) {
        ans[nv] = v;
        dist[nv] = d + 1;
        dfs(nv, v, d + 1);
      }
    }
  };

  dfs(1, -1, 0);

  for (int i = 1; i <= n; i++) {
    if (dist[i] == INF) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

vector<vector<int>> G;
int *dist;
int *par;

void dijkstra(int n, int s) {
  for (int i = 0; i < n; i++) {
    dist[i] = 999999;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pque;
  dist[s] = 0;
  pque.push(pair<int, int>(0, s));

  while (!pque.empty()) {
    pair<int, int> p = pque.top();
    int v = p.second;
    int d = p.first;

    pque.pop();
    if (d > dist[v])
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      int u = G[v][i];
      if (dist[u] <= d + 1)
        continue;

      dist[u] = d + 1;
      pque.push(pair<int, int>(dist[u], u));
      par[u] = v;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  G.resize(n);
  dist = new int[n], par = new int[n];

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dijkstra(n, 0);

  for (int i = 0; i < n; i++) {
    if (dist[i] == 999999) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << par[i] + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

vector<vector<int>> G;
int *dist;
int *par;
priority_queue<pair<int, int>> pque;

void dijkstra(int n, int s) {
  for (int i = 0; i < n; i++) {
    dist[i] = 999999;
  }
  dist[s] = 0;
  pque.push(pair<int, int>(0, s));

  while (!pque.empty()) {
    pair<int, int> p = pque.top();
    int v = p.second;
    int d = p.first;
    pque.pop();
    if (d > dist[v])
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      int u = G[v][i];
      if (dist[u] <= d + 1)
        continue;

      dist[u] = d + 1;
      pque.push(pair<int, int>(dist[u], u));
      par[u] = v;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  G.resize(n);
  dist = new int[n], par = new int[n];

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dijkstra(n, 0);

  for (int i = 0; i < n; i++) {
    if (dist[i] == 999999) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << par[i] + 1 << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {

  int n, m;
  cin >> n >> m;
  Graph G(n);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> dist(n, -1);
  queue<int> que;
  vector<int> pre(n, -1);
  dist[0] = 0;
  que.push(0);

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      que.push(nv);
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << pre[i] + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {

  int n, m;
  cin >> n >> m;
  Graph G(m);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> dist(n, -1);
  queue<int> que;
  vector<int> pre(n, -1);
  dist[0] = 0;
  que.push(0);

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      que.push(nv);
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << pre[i] + 1 << endl;
  }

  return 0;
}