alexjercan/bugnet huggingface dataset:
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <deque>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e5 + 5;
const int inf = 100000000;
deque<int> q;
vector<int> e[MAX];
int a[MAX];
int v[MAX];
int book[MAX];
int ans[MAX];
void bfs() {
  int p;
  while (!q.empty()) {
    p = q.front();
    q.pop_front();
    for (int i = 0; i < a[p]; i++) {
      if (!book[e[p][i]]) {
        if (v[p] + 1 < v[e[p][i]]) {
          v[e[p][i]] = v[p] + 1;
          ans[e[p][i]] = p;
        }
        book[e[p][i]] = 1;
        q.push_back(e[p][i]);
      }
    }
  }
}
int main() {
  int n;
  int m;
  cin >> n >> m;
  int hhn, hn;
  for (int i = 2; i <= n; i++) {
    v[i] = inf, a[i] = 0, book[i] = 0, ans[i] = 0;
  }
  v[1] = 0, book[1] = 1;
  for (int i = 1; i <= m; i++) {
    cin >> hn >> hhn;
    e[hn].push_back(hhn);
    e[hhn].push_back(hn);
    a[hn]++;
    a[hhn]++;
  }
  q.push_back(1);
  bfs();
  int flag = 1;
  for (int i = 2; i <= n; i++) {
    if (v[i] == inf)
      flag = 0;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << ans[i] << endl;
  } else
    cout << "No" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define MAX 110000
#define max(a, b) ((a) < (b) ? (b) : (a))
using namespace std;
int to[MAX];
int head[MAX];
int nxt[MAX];
int v[MAX];
int sign[MAX];
int tot;
void Add(int fir, int sec) {
  nxt[++tot] = head[fir];
  to[tot] = sec;
  head[fir] = tot;
}

void bfs(int node) {
  // v[node] = 1;
  queue<int> q;
  q.push(node);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    v[x] = 1;
    for (int i = head[x]; i != -1; i = nxt[i]) {
      int y = to[i];
      if (v[y] == 1) {
        // cout << y << "#" << endl;
        continue;
      }
      if (sign[y] == 0)
        sign[y] = x;
      q.push(y);
      // printf("%d sign is %d\\n", y, x);
    }
  }
}

int main() {
  int N, M;
  while (cin >> N >> M) {
    tot = 0;
    memset(nxt, -1, sizeof(nxt));
    memset(v, 0, sizeof(v));
    memset(head, 0, sizeof(head));
    memset(to, 0, sizeof(to));
    memset(sign, 0, sizeof(sign));
    int fir, sec;
    for (int i = 1; i <= M; i++) {
      scanf("%d%d", &fir, &sec);
      Add(fir, sec);
      Add(sec, fir);
    }
    bfs(1);
    int flag = 0;
    for (int i = 2; i <= N; i++) {
      if (!sign[i]) {
        flag = 1;
      }
    }
    if (flag) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
      for (int i = 2; i <= N; i++)
        cout << sign[i] << endl;
    }
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using namespace std;

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> v(n);
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> que;
  que.push(0);
  vector<bool> fin(n);
  vector<int> flag(n);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    rep(i, v[now].size()) {
      int nxt = v[now][i];
      if (!fin[nxt]) {
        flag[nxt] = now;
        fin[nxt] = true;
        que.push(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) cout << flag[i + 1] + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using namespace std;

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> v(m);
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> que;
  que.push(0);
  vector<bool> fin(n);
  vector<int> flag(n);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    rep(i, v[now].size()) {
      int nxt = v[now][i];
      if (!fin[nxt]) {
        flag[nxt] = now;
        fin[nxt] = true;
        que.push(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) cout << flag[i + 1] + 1 << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <deque>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

namespace std {
template <typename Iterator> Iterator begin(pair<Iterator, Iterator> p) {
  return p.first;
}
template <typename Iterator> Iterator end(pair<Iterator, Iterator> p) {
  return p.second;
}
} // namespace std

bool search(deque<ll> &q, vector<ll> &v,
            unordered_map<ll, unordered_set<ll>> &e) {
  while (!q.empty()) {
    auto const current = q.front();
    q.pop_front();

    // traverse edges
    for (auto const &neighbor : e[current]) {
      if (v[neighbor - 1] == 0) {   // if not yet marked
        v[neighbor - 1] = current;  // mark as
        q.push_back(neighbor);      // in next step
        e[neighbor].erase(current); // optimize
      }
    }
  }
  return true;
}

int main() {
  ll N, M;
  cin >> N >> M;
  deque<ll> q;                            // 幅優先探索キュー
  vector<ll> v(N);                        // 道しるべ
  unordered_map<ll, unordered_set<ll>> e; // 通路

  for (int i = 1; i <= M; ++i) {
    ll Ai, Bi;
    cin >> Ai >> Bi;
    e[Ai].insert(Bi);
    e[Bi].insert(Ai);
  }

  q.push_back(1); // 入口から探索開始

  auto const &res = search(q, v, e);

  if (res) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; ++i) {
      cout << v[i - 1] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

namespace std {
template <typename Iterator> Iterator begin(pair<Iterator, Iterator> p) {
  return p.first;
}
template <typename Iterator> Iterator end(pair<Iterator, Iterator> p) {
  return p.second;
}
} // namespace std

bool search(deque<ll> &q, vector<ll> &v, unordered_multimap<ll, ll> &e) {
  while (!q.empty()) {
    auto const current = q.front();
    q.pop_front();

    // traverse edges
    for (auto const &[_, neighbor] : e.equal_range(current)) {
      if (v[neighbor - 1] == 0) {  // if not yet marked
        v[neighbor - 1] = current; // mark as
        q.push_back(neighbor);     // in next step
      }
    }
  }
  return true;
}

int main() {
  ll N, M;
  cin >> N >> M;
  deque<ll> q;                  // 幅優先探索キュー
  vector<ll> v(N);              // 道しるべ
  unordered_multimap<ll, ll> e; // 通路

  for (int i = 1; i <= M; ++i) {
    ll Ai, Bi;
    cin >> Ai >> Bi;
    e.emplace(Ai, Bi);
    e.emplace(Bi, Ai);
  }

  q.push_back(1); // 入口から探索開始

  auto const &res = search(q, v, e);

  if (res) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; ++i) {
      cout << v[i - 1] << endl;
    }
  } else {
    cout << "No" << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> as(m), bs(m);
  for (int i = 0; i < m; i++) {
    cin >> as[i] >> bs[i];
    as[i]--;
    bs[i]--;
  }

  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    g[as[i]].emplace_back(bs[i]);
    g[bs[i]].emplace_back(as[i]);
  }

  int inf = (1 << 29);

  vector<int> ans(n, -1), dist(n, inf);

  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;

  dist[0] = 0;
  for (auto &&next : g[0]) {
    dist[next] = 1;
    ans[next] = 0;

    pq.emplace(dist[next], next);
  }

  while (!pq.empty()) {
    int d = pq.top().first;
    int i = pq.top().second;
    pq.pop();

    if (dist[i] < d) {
      continue;
    }

    for (auto &&next : g[i]) {
      if (dist[next] <= dist[i] + 1) {
        continue;
      }

      dist[next] = dist[i] + 1;
      ans[next] = i;

      pq.emplace(dist[next], next);
    }
  }

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

    cout << ans[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> as(m), bs(m);
  for (int i = 0; i < m; i++) {
    cin >> as[i] >> bs[i];
    as[i]--;
    bs[i]--;
  }

  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    g[as[i]].emplace_back(bs[i]);
    g[bs[i]].emplace_back(as[i]);
  }

  int inf = (1 << 29);

  vector<int> ans(n, -1), dist(n, inf);

  queue<pair<int, int>> que;

  dist[0] = 0;
  for (auto &&next : g[0]) {
    que.emplace(0, next);
  }

  while (!que.empty()) {
    int par = que.front().first;
    int now = que.front().second;
    que.pop();

    if (dist[now] < dist[par] + 1) {
      continue;
    }

    dist[now] = dist[par] + 1;
    ans[now] = par;

    for (auto &&next : g[now]) {
      if (next == par) {
        continue;
      }

      que.emplace(now, next);
    }
  }

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

    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int> ans(100005, -1);
const int INF = 10e9 + 10;
vector<int> dist(100005, INF);
void bfs(int s) {
  queue<int> q;
  q.push(s);
  dist[s] = 0;
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (auto i : adj[node]) {
      if (dist[i] > dist[node] + 1) {
        dist[i] = dist[node] + 1;
        ans[i] = node;
        q.push(i);
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  adj.assign(n + 1, vector<int>());
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  bfs(0);
  int fn(0);
  for (int i = 0; i < n; ++i) {
    if (ans[i] == -1)
      fn = 1;
  }
  if (fn) {
    cout << "Yes\\n";
    for (int i = 1; i < n; ++i)
      cout << ans[i] + 1 << "\\n";
  } else
    cout << "No";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[10000], vis[100000];
vector<vector<int>> adj;
void dfs(int s) {
  vis[s] = 1;
  for (int i = 0; i < adj[s].size(); ++i) {
    if (vis[adj[s][i]])
      continue;
    dp[adj[s][i]] = dp[s] + 1;
    dfs(adj[s][i]);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  adj.assign(n + 1, vector<int>());
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int fn(0);
  dfs(0);
  for (int i = 0; i < n; ++i) {
    if (vis[i] == 0)
      fn = 1;
  }
  if (fn == 0) {
    cout << "Yes\\n";
    for (int i = 1; i < n; ++i)
      cout << dp[i] << "\\n";
  } else
    cout << "No";
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
// #define coutyn(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" <<
// endl
#define ALL(a) (a).begin(), (a).end()
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 long long INF = 1LL << 60;
const int MOD = 1000000007;
const double PI = acos(-1); // 3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n + 1);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  queue<int> q;
  vector<int> dist(n + 1, -1);
  vector<int> pre(n + 1, -1);
  dist[0] = 0;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto it : g[v]) {
      if (dist[it] != -1)
        continue;
      dist[it] = dist[v] + 1;
      pre[it] = v;
      q.push(it);
    }
  }

  cout << "Yes" << endl;
  Rep(i, 2, n + 1) {
    int ans = pre[i];
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
// #define coutyn(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" <<
// endl
#define ALL(a) (a).begin(), (a).end()
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 long long INF = 1LL << 60;
const int MOD = 1000000007;
const double PI = acos(-1); // 3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n + 1);
  rep(i, m) {
    int a, b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  queue<int> q;
  vector<int> dist(n + 1, -1);
  vector<int> pre(n + 1, -1);
  dist[0] = 0;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto it : g[v]) {
      if (dist[it] != -1)
        continue;
      dist[it] = dist[v] + 1;
      pre[it] = v;
      q.push(it);
    }
  }

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

#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main(void) {
  long long N, M;
  std::vector<int> A, B;
  cin >> N >> M;
  A.resize(M);
  B.resize(M);
  std::vector<std::set<int>> connected(200001);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    connected[A[i]].insert(B[i]);
    connected[B[i]].insert(A[i]);
  }

  std::queue<int> q;
  std::vector<int> v(200001, -1);
  q.push(1);
  v[1] = 1;
  while (false == q.empty()) {
    int current = q.front();
    q.pop();
    for (auto child : connected[current]) {
      if (-1 == v[child]) {
        v[child] = current;
        q.push(child);
      }
    }
  }

  for (int i = 2; i <= N; i++) {
    if (-1 == v[i]) {
      std::cout << "No" << std::endl;
      break;
    } else {
      if (2 == i) {
        std::cout << "Yes" << std::endl;
      }
      std::cout << v[i] << std::endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

class Node {
public:
  Node(int iId, int iRoot) {
    id = iId;
    root = iRoot;
  }
  int id;
  int root;
};

int main(void) {
  long long N, M;
  std::vector<int> A, B;
  cin >> N >> M;
  A.resize(M);
  B.resize(M);
  std::vector<std::set<int>> connected(200001);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    connected[A[i]].insert(B[i]);
    connected[B[i]].insert(A[i]);
  }

  for (int i = 2; i <= N; i++) {
    std::queue<Node> q;
    std::vector<bool> isFound(200001, false);
    for (auto x : connected[i]) {
      Node node = Node(x, x);
      q.push(node);
    }

    bool is1Found = false;
    int ans = 0;
    while (1) {
      if (q.empty())
        break;
      Node pushed = q.front();
      if (1 == pushed.id) {
        ans = pushed.root;
        is1Found = true;
        break;
      }
      q.pop();
      if (true == isFound[pushed.id])
        continue;
      isFound[i] = true;
      for (auto x : connected[pushed.id]) {
        Node node = Node(x, pushed.root);
        q.push(node);
      }
    }

    if (is1Found) {
      if (i == 2) {
        std::cout << "Yes" << std::endl;
      }
      std::cout << ans << std::endl;
    } else {
      std::cout << "No" << std::endl;
    }
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

int main(void) {
  ll n, m;
  cin >> n >> m;
  Graph g(n + 1);
  for (int i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    // a--,b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> direct(n + 1, -1);
  queue<int> que;

  direct[0] = 0;
  direct[1] = 0;
  que.push(1);

  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int nv : g[v]) {
      if (direct[nv] != -1)
        continue;

      direct[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << direct[i + 1] << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

int main(void) {
  ll n, m;
  cin >> n >> m;
  Graph g(n);
  for (int i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> dist(n, -1);
  queue<int> que;

  dist[1] = 0;
  que.push(1);

  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int nv : g[v]) {
      if (dist[nv] != -1)
        continue;

      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    cout << dist[i + 1] << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
int main() {
  long long n, m;
  long long i, j, k;
  scanf("%lld%lld", &n, &m);
  long long a[m + 1], b[m + 1];
  long long num[n + 1], ans[n + 1];
  bool visited[n + 1];
  queue<long long> que;

  vector<long long> memo[n + 1];
  for (i = 0; i < m; i++) {
    scanf(" %lld%lld", &a[i], &b[i]);
    memo[a[i]].push_back(b[i]);
    memo[b[i]].push_back(a[i]);
  }

  que.push(1);
  for (i = 0; i < 1 + n; i++) {
    visited[i] = 0;
    ans[i] = 0;
    num[i] = 100000;
  }
  num[1] = 0;
  long long u, v;
  while (!que.empty()) {
    u = que.front();
    que.pop();
    visited[u] = 1;
    for (i = 0; i < memo[u].size(); i++) {
      v = memo[u].at(i);
      if (num[v] > num[u] + 1) {
        num[v] = num[u] + 1;
        if (!visited[v]) {
          ans[v] = u;
          que.push(v);
        }
      }
    }
  }
  puts("Yes");
  for (i = 2; i < n + 1; i++) {
    printf("%lld\\n", ans[i]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
int main() {
  long long n, m;
  long long i, j, k;
  scanf("%lld%lld", &n, &m);
  long long a[m + 1], b[m + 1];
  for (i = 0; i < m; i++) {
    scanf(" %lld%lld", &a[i], &b[i]);
  }
  long long num[n + 1], ans[n + 1];
  bool memo[n + 1];
  queue<long long> que;
  que.push(1);
  for (i = 0; i < 1 + n; i++) {
    memo[i] = 0;
    ans[i] = 0;
    num[i] = 100000;
  }
  num[1] = 0;
  long long u;
  while (!que.empty()) {
    u = que.front();
    que.pop();
    // printf("u %lld\\n",u);
    memo[u] = 1;
    for (i = 0; i < m; i++) {
      if (a[i] == u) {
        if (num[b[i]] > num[u] + 1) {
          //   printf("u %lld a %lld %lld memo %lld\\n",u,a[i],b[i],memo[b[i]]);
          num[b[i]] = num[u] + 1;
          if (!memo[b[i]]) {
            ans[b[i]] = u;
            que.push(b[i]);
          }
        }
      }
      if (b[i] == u) {
        if (num[a[i]] > num[u] + 1) {
          //   printf("u %lld b %lld %lld memo %lld\\n",u,a[i],b[i],memo[a[i]]);
          num[a[i]] = num[u] + 1;
          if (!memo[a[i]]) {
            ans[a[i]] = u;
            que.push(a[i]);
          }
        }
      }
    }
  }
  puts("Yes");
  for (i = 2; i < n + 1; i++) {
    printf("%lld\\n", ans[i]);
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int N = 100100;

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

  vector<int> to[N];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  vector<int> ans[N];
  queue<int> que;
  que.push(1);

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (!to[v].empty()) {
      for (int nv : to[v]) {
        if (ans[nv].empty()) {
          ans[nv].push_back(v);
          que.push(nv);
        }
      }
    } else {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i < n + 1; i++)
    cout << ans[i].at(0) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 100100;

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

  vector<int> to[INF];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  vector<int> ansvec(n - 1);
  vector<int> dist(n, INF);
  queue<int> que;

  dist[0] = 0;
  que.push(0);
  bool ans = true;

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (!to[v].empty()) {
      for (int nv : to[v]) {
        if (dist[nv] != INF)
          continue; // already searched
        que.push(nv);
        if (dist[nv] <= dist[v] + 1)
          continue; // longer than previous dist
        dist[nv] = dist[v] + 1;
        ansvec[nv] = v;
      }
    } else {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  rep(i, ansvec.size()) cout << ansvec[i + 1] + 1 << endl;
  return 0;
}