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

#include <bits/stdc++.h>
using namespace std;
int inf = 10000000;
typedef pair<int, int> P;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);

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

  vector<int> parent(n, inf);
  queue<int> que;

  for (int i = 0; i < to[0].size(); i++) {
    que.push(to[0][i]);
    parent[to[0][i]] = 1;
  }

  while (!que.empty()) {
    int k = que.front();
    que.pop();
    for (int i = 0; i < to[k].size(); i++) {
      if (parent[to[k][i]] == inf) {
        que.push(to[k][i]);
        parent[to[k][i]] = k + 1;
      }
    }
  }

  cout << "Yes" << endl;

  for (int i = 1; i < n; i++)
    cout << parent[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int inf = 10000000;
typedef pair<int, int> P;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);

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

  vector<int> parent(n, inf);
  queue<int> que;

  for (int i = 0; i < to[0].size(); i++) {
    que.push(to[0][i]);
    parent[to[0][i]] = 1;
  }

  while (!que.empty()) {
    int k = que.front();
    que.pop();
    for (int i = 0; i < to[k].size(); i++) {
      if (parent[to[k][i]] == inf) {
        que.push(to[k][i]);
        parent[to[k][i]] = k + 1;
      }
    }
  }

  cout << "Yes" << endl;

  for (int i = 1; i < n; i++)
    cout << parent[i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

vector<int> graph[100005];

int dist[100005], pre[100005];
bool vis[100005];

void bfs(int src) {

  queue<int> Q;
  memset(vis, 0, sizeof vis);
  Q.push(src);
  vis[src] = 1;
  dist[src] = 0;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < graph[u].size(); i++) {
      int v = graph[u][i];
      if (vis[v] == 1)
        continue;
      vis[v] = 1;
      dist[v] = dist[u] + 1;
      pre[v] = u;
      Q.push(v);
    }
  }
}

int main() {
  int n, st, u, v;
  cin >> n >> st;
  for (int i = 0; i < st; i++) {
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  bfs(1);
  cout << "Yes\\n";
  for (int i = 2; i <= n; i++)
    cout << pre[i] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> graph[100000];

int dist[100000], pre[100005];
bool vis[100000];

void bfs(int src) {

  queue<int> Q;
  memset(vis, 0, sizeof vis);
  Q.push(src);
  vis[src] = 1;
  dist[src] = 0;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < graph[u].size(); i++) {
      int v = graph[u][i];
      if (vis[v] == 1)
        continue;
      vis[v] = 1;
      dist[v] = dist[u] + 1;
      pre[v] = u;
      Q.push(v);
    }
  }
}

int main() {
  int n, st, u, v;
  cin >> n >> st;
  for (int i = 0; i < st; i++) {
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  bfs(1);
  cout << "Yes\\n";
  for (int i = 2; i <= n; i++)
    cout << pre[i] << '\\n';
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <vector>

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
// #define RREP(i, n ,N) for(ll i = (N-1); i >= (n); i--)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define vint vector<int>
#define INF 1e9

using namespace std;
typedef long long ll;

const long long MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> michi(n + 1, vector<int>());
  int tmp1, tmp2;
  REP(i, 0, m) {
    cin >> tmp1 >> tmp2;
    michi[tmp1].push_back(tmp2);
    michi[tmp2].push_back(tmp1);
  }

  vint ans(n + 1, 0);

  queue<int> bfs;
  REP(i, 0, michi[1].size()) {
    bfs.push(michi[1][i]);
    ans[michi[1][i]] = 1;
  }

  int target;
  while (not bfs.empty()) {
    target = bfs.front();
    REP(i, 0, michi[target].size()) {
      if (ans[michi[target][i]] != 0) {
        continue;
      }
      bfs.push(michi[target][i]);
      ans[michi[target][i]] = target;
    }
    bfs.pop();
  }

  REP(i, 2, n + 1) {
    if (ans[i] == 0) {
      p("No");
      return 0;
    }
  }

  p("Yes");
  REP(i, 2, n + 1) { p(ans[i]); }

  return 0;
}


Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <vector>

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
// #define RREP(i, n ,N) for(ll i = (N-1); i >= (n); i--)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define vint vector<int>
#define INF 1e9

using namespace std;
typedef long long ll;

const long long MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> michi(n + 1, vector<int>());
  int tmp1, tmp2;
  REP(i, 0, m) {
    cin >> tmp1 >> tmp2;
    michi[tmp1].push_back(tmp2);
    michi[tmp2].push_back(tmp1);
  }

  vint ans(n - 1, 0);

  queue<int> bfs;
  REP(i, 0, michi[1].size()) {
    bfs.push(michi[1][i]);
    ans[michi[1][i] - 2] = 1;
  }

  int target;
  while (not bfs.empty()) {
    target = bfs.front();
    REP(i, 0, michi[target].size()) {
      if (ans[michi[target][i] - 2] != 0) {
        continue;
      }
      bfs.push(michi[target][i]);
      ans[michi[target][i] - 2] = target;
    }
    bfs.pop();
  }

  REP(i, 0, ans.size()) {
    if (ans[i] == 0) {
      p("No");
      return 0;
    }
  }

  p("Yes");
  REP(i, 0, ans.size()) { p(ans[i]); }

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
vector<int> g[MAXN];

int main() {
  faster int n, m;
  cin >> n >> 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> p(n, -1);
  vector<bool> was(n, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (was[v])
      continue;
    was[v] = true;
    for (int to : g[v]) {
      if (!was[to]) {
        q.push(to);
        if (p[to] < 0)
          p[to] = v;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (p[i] < 0) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (int i = 1; i < n; i++) {
    cout << p[i] + 1 << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
vector<int> g[MAXN];

int main() {
  faster int n, m;
  cin >> n >> 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> p(n, -1);
  vector<bool> was(n, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    was[v] = true;
    for (int to : g[v]) {
      if (!was[to]) {
        q.push(to);
        if (p[to] < 0)
          p[to] = v;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (p[i] < 0) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (int i = 1; i < n; i++) {
    cout << p[i] + 1 << "\\n";
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<int> to[100005];

void bfs(int n) {
  queue<int> q;
  vector<int> dist(n, -1);
  vector<int> pre(n, -1);

  q.push(0);
  dist[0] = 0;

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != -1)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << pre[i] + 1 << endl;
  }
}

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

  bfs(n);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> to[10005];

void bfs(int n) {
  queue<int> q;
  vector<int> dist(n, -1);
  vector<int> pre(n, -1);

  q.push(0);
  dist[0] = 0;

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : to[v]) {
      if (dist[u] != -1)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << pre[i] + 1 << endl;
  }
}

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

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

#include <stdio.h>
#include <stdlib.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
const int sz = 1e5;
int qLen, qHead, Q[sz], *cave[sz];
void PUSH(int n) {
  Q[(qHead + qLen) % sz] = n;
  qLen++;
}
int POP(void) {
  int q = Q[qHead];
  qLen--;
  qHead = (qHead + 1) % sz;
  return q;
}
int main(void) {
  int A, B, N, M, x, y;
  x = scanf("%d %d", &N, &M);
  int room[N], path[N];
  rep(i, N) path[i] = 0, room[i] = -1;
  while (~scanf("%d %d", &A, &B)) {
    A--, B--;
    cave[A] = (int *)realloc(cave[A], sizeof(int) * (path[A] + 1));
    cave[A][path[A]] = B;
    path[A]++;
    cave[B] = (int *)realloc(cave[B], sizeof(int) * (path[B] + 1));
    cave[B][path[B]] = A;
    path[B]++;
  }
  room[0] = 0;
  PUSH(0);
  while (qLen) {
    x = POP();
    rep(i, path[x]) {
      y = cave[x][i];
      if (!(room[y] < 0))
        continue;
      room[y] = x, PUSH(y);
    }
  }
  puts("Yes");
  rep(i, N - 1) printf("%d\\n", room[i + 1] + 1);
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define array(N, t) (t *)calloc(N, sizeof(t))
const int sz = 1e5;
int qLen, qHead, Q[sz], *cave[0];
void PUSH(int n) {
  Q[(qHead + qLen) % sz] = n;
  qLen++;
}
int POP(void) {
  int q = Q[qHead];
  qLen--;
  qHead = (qHead + 1) % sz;
  return q;
}
int main(void) {
  int A, B, N, M, x, y;
  x = scanf("%d %d", &N, &M);
  int *path = array(N, int), *room = array(N, int);
  rep(i, N) path[i] = 0, room[i] = -1;
  while (~scanf("%d %d", &A, &B)) {
    A--, B--;
    cave[A] = (int *)realloc(cave[A], sizeof(int) * (path[A] + 1));
    cave[A][path[A]] = B;
    path[A]++;
    cave[B] = (int *)realloc(cave[B], sizeof(int) * (path[B] + 1));
    cave[B][path[B]] = A;
    path[B]++;
  }
  room[0] = 0;
  PUSH(0);
  while (qLen) {
    x = POP();
    rep(i, path[x]) {
      y = cave[x][i];
      if (!(room[y] < 0))
        continue;
      room[y] = x, PUSH(y);
    }
  }
  puts("Yes");
  rep(i, N - 1) printf("%d\\n", room[i + 1] + 1);
  return free(path), free(room), 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

static const ll INF = -1;
vector<ll> abc168d(ll n, ll m, vector<vector<ll>> edge) {
  vector<ll> d(n, INF);

  queue<ll> que;
  que.push(0);
  d[0] = 0;

  vector<ll> ans(n - 1);
  while (!que.empty()) {
    ll v = que.front();
    que.pop();
    for (auto nv : edge[v]) {
      if (d[nv] == INF) {
        d[nv] = d[v] + 1;
        ans[nv - 1] = v + 1;
        que.push(nv);
      }
    }
  }
  return ans;
}

int main() {
  ll n, m;
  cin >> n >> m;
  /*
  ll a[m], b[m];
  for(ll i=0;i<m;i++) {
    cin >> a[i] >> b[i];
  }
  */

  vector<vector<ll>> edge;
  edge.assign(n, vector<ll>());

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

  vector<ll> ans = abc168d(n, m, edge);
  cout << "Yes" << endl;
  for (ll i = 0; i < n - 1; i++) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef int ll;
using namespace std;
static const ll MX_V = 200001;

static const ll INF = -1;
vector<ll> abc168d(ll n, ll m, vector<vector<ll>> edge) {
  vector<ll> d(n, INF);

  queue<ll> que;
  que.push(0);
  d[0] = 0;

  vector<ll> ans(n - 1);
  while (!que.empty()) {
    ll v = que.front();
    que.pop();
    for (auto nv : edge[v]) {
      if (d[nv] == INF) {
        d[nv] = d[v] + 1;
        ans[nv - 1] = v + 1;
        que.push(nv);
      }
    }
  }
  return ans;
}

int main() {
  ll n, m;
  cin >> n >> m;
  /*
  ll a[m], b[m];
  for(ll i=0;i<m;i++) {
    cin >> a[i] >> b[i];
  }
  */

  vector<vector<ll>> edge;
  edge.assign(n, vector<ll>());

  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    vector<ll> ns = edge[a - 1];
    ns.push_back(b - 1);
    edge[a - 1] = ns;

    ns = edge[b - 1];
    ns.push_back(a - 1);
    edge[b - 1] = ns;
  }

  vector<ll> ans = abc168d(n, m, edge);
  cout << "Yes" << endl;
  for (ll i = 0; i < n - 1; i++) {
    cout << ans[i];
    if (i != n - 2) {
      cout << endl;
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;

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

  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 0));
  int dp[N];
  fill(dp, dp + N, 1000000005);
  dp[0] = 0;
  while (!q.empty()) {
    int now = q.top().second;
    int d = q.top().first;
    q.pop();
    if (dp[now] != d)
      continue;

    for (auto next : G[now]) {
      if (dp[next] > dp[now] + 1) {
        dp[next] = dp[now] + 1;
        q.push(P(dp[now] + 1, next));
      }
    }
  }

  bool fl = true;
  for (int i = 0; i < N; i++) {
    if (dp[i] == 1000000005)
      fl = false;
  }

  if (!fl) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  int ans[N];
  for (int i = 0; i < M; i++) {
    if (dp[b[i]] == dp[a[i]] - 1)
      ans[a[i]] = b[i];
    if (dp[a[i]] == dp[b[i]] - 1)
      ans[b[i]] = a[i];
  }

  for (int i = 1; i < N; i++) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;

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

  priority_queue<P> q;
  q.push(P(0, 0));
  int dp[N];
  fill(dp, dp + N, 1000000005);
  dp[0] = 0;
  while (!q.empty()) {
    int now = q.top().second;
    int d = q.top().first;
    q.pop();
    if (dp[now] != d)
      continue;

    for (auto next : G[now]) {
      if (dp[next] > dp[now] + 1) {
        dp[next] = dp[now] + 1;
        q.push(P(dp[now] + 1, next));
      }
    }
  }

  bool fl = true;
  for (int i = 0; i < N; i++) {
    if (dp[i] == 1000000005)
      fl = false;
  }

  if (!fl) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  int ans[N];
  for (int i = 0; i < M; i++) {
    if (dp[b[i]] == dp[a[i]] - 1)
      ans[a[i]] = b[i];
    if (dp[a[i]] == dp[b[i]] - 1)
      ans[b[i]] = a[i];
  }

  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>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

int N, M;
vector<int> adj[100010];
int pre[100010];
bool visited[100010];

void bfs() {
  queue<pint> que;
  que.push(pint(0, 0));
  while (!que.empty()) {
    int p = que.front().first;
    int i = que.front().second;
    que.pop();
    if (visited[i])
      continue;
    visited[i] = true;
    pre[i] = p;
    for (int j : adj[i]) {
      if (visited[j])
        continue;
      que.push(pint(i, j));
    }
  }
}

signed main() {
  cin >> N >> M;
  int a, b;
  REP(i, M) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  PRINT("Yes");
  bfs();
  FOR(i, 1, N) { PRINT(pre[i] + 1); }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

int N, M;
vector<int> adj[100010];
int pre[100010];
ll dist[100010];

void dijkstra() {
  REP(i, N) dist[i] = LONG_LONG_MAX / 2;
  priority_queue<pll> que;
  que.push(pll(0, 0));
  while (!que.empty()) {
    ll d = que.top().first;
    int i = que.top().second;
    que.pop();
    for (int j : adj[i]) {
      if (d + 1 < dist[j]) {
        dist[j] = d + 1;
        pre[j] = i;
        que.push(pll(d + 1, j));
      }
    }
  }
}

signed main() {
  cin >> N >> M;
  int a, b;
  REP(i, M) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  PRINT("Yes");
  dijkstra();
  FOR(i, 1, N) { PRINT(pre[i] + 1); }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define endl '\\n'

int sp[100001];

void bsf(vector<vector<int>> &G) {
  queue<int> Q;
  Q.push(1);
  sp[1] = 1;
  while (!Q.empty()) {
    int t = Q.front();
    Q.pop();
    for (auto &c : G[t]) {
      if (!sp[c]) {
        sp[c] = t;
        Q.push(c);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  int a, b;
  vector<vector<int>> graph(n + 1, vector<int>());
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  // for (auto& r : graph) {
  //     for (auto& e : r) cout << e << ' ';
  //     cout << endl;
  // }
  bsf(graph);
  bool completed = true;
  for (int j = 1; j <= n; j++) {
    if (!sp[j]) {
      completed = false;
      break;
    }
  }
  if (completed) {
    cout << "Yes" << endl;
    for (int j = 2; j <= n; j++)
      cout << sp[j] << endl;
  } else
    cout << "No" << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define endl '\\n'

int sp[100001];
int depth[100001];

void traverse(vector<vector<int>> &graph, int k, int d) {
  if (graph[k].empty())
    return;
  // cout << k << ' ' << d << endl;
  for (int i = 0; i < graph[k].size(); i++) {
    if (!depth[graph[k][i]] || (depth[graph[k][i]] && depth[graph[k][i]] > d)) {
      depth[graph[k][i]] = d;
      sp[graph[k][i]] = k;
      traverse(graph, graph[k][i], d + 1);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  int a, b;
  vector<vector<int>> graph(n + 1, vector<int>());
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  // for (auto& r : graph) {
  //     for (auto& e : r) cout << e << ' ';
  //     cout << endl;
  // }
  traverse(graph, 1, 1);
  bool completed = true;
  for (int j = 1; j <= n; j++) {
    if (!sp[j]) {
      completed = false;
      break;
    }
  }
  if (completed) {
    cout << "Yes" << endl;
    for (int j = 2; j <= n; j++)
      cout << sp[j] << endl;
  } else
    cout << "No" << endl;
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
#define rg register
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
const int Maxn = 1e5 + 5;
typedef int arr[Maxn];
// ll Add(ll x, ll y) { return (x += y) >= Mod ? x - Mod : (x < 0 ? x + Mod :
// x); }
ll min(ll x, ll y) { return x < y ? x : y; }
ll max(ll x, ll y) { return x > y ? x : y; }
inline ll read() {
  rg ll res = 0, sign = 1;
  rg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  do {
    res = (res << 3) + (res << 1) + (ch ^ 48);
    ch = getchar();
  } while (isdigit(ch));
  return res * sign;
}
int n, m, cnt, num;
arr fir, from;
struct {
  int to, last;
} e[Maxn << 2];
void con(int u, int v) {
  e[++cnt].to = v;
  e[cnt].last = fir[u];
  fir[u] = cnt;
}
void BFS(int x) {
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int next = fir[x], nx; next; next = e[next].last) {
      nx = e[next].to;
      if (nx != 1 && !from[nx]) {
        num--;
        from[nx] = x;
        q.push(nx);
      }
    }
  }
}
int main() {
  rg int a, b;
  cin >> n >> m;
  num = n - 1;
  for (rg int i = 1; i <= m; i++) {
    a = read();
    b = read();
    con(a, b);
    con(b, a);
  }
  BFS(1);
  if (!num) {
    puts("Yes");
    for (rg int i = 2; i <= n; i++)
      printf("%d\\n", from[i]);
  } else
    puts("No");
  return 0;
}
// D

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
#define rg register
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
const int Maxn = 1e5 + 5;
typedef int arr[Maxn];
// ll Add(ll x, ll y) { return (x += y) >= Mod ? x - Mod : (x < 0 ? x + Mod :
// x); }
ll min(ll x, ll y) { return x < y ? x : y; }
ll max(ll x, ll y) { return x > y ? x : y; }
inline ll read() {
  rg ll res = 0, sign = 1;
  rg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  do {
    res = (res << 3) + (res << 1) + (ch ^ 48);
    ch = getchar();
  } while (isdigit(ch));
  return res * sign;
}
int n, m, cnt, num;
arr fir, from;
struct {
  int to, last;
} e[Maxn << 1];
void con(int u, int v) {
  e[++cnt].to = v;
  e[cnt].last = fir[u];
  fir[u] = cnt;
}
void BFS(int x) {
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int next = fir[x], nx; next; next = e[next].last) {
      nx = e[next].to;
      if (nx != 1 && !from[nx]) {
        num--;
        from[nx] = x;
        q.push(nx);
      }
    }
  }
}
int main() {
  rg int a, b;
  cin >> n >> m;
  num = n - 1;
  for (rg int i = 1; i <= m; i++) {
    a = read();
    b = read();
    con(a, b);
    con(b, a);
  }
  BFS(1);
  if (!num) {
    puts("YES");
    for (rg int i = 2; i <= n; i++)
      printf("%d\\n", from[i]);
  } else
    puts("NO");
  return 0;
}
// D