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

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

struct Edge {
  int u, v, next;
} edge[500050];

struct Point {
  int p, dis;
};

int n, m, cnt = 0;
int head[200050], vis[200050];

void addedge(int u, int v) {
  edge[cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

void bfs() {
  queue<Point> q;
  q.push({1, 0});
  vis[1] = 0;
  while (!q.empty()) {
    Point p = q.front();
    q.pop();
    for (int i = head[p.p]; i != -1; i = edge[i].next) {
      if (vis[edge[i].v] == -1) {
        vis[edge[i].v] = p.p;
        q.push({edge[i].v, p.dis + 1});
      }
    }
  }
  return;
}

int main() {
  scanf("%d %d", &n, &m);
  memset(head, -1, sizeof(head));
  memset(vis, -1, sizeof(vis));
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  bfs();
  bool flag = 1;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    puts("Yes");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", vis[i]);
    }
  } else {
    puts("No");
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

struct Edge {
  int u, v, next;
} edge[200050];

struct Point {
  int p, dis;
};

int n, m, cnt = 0;
int head[100050], vis[100050];

void addedge(int u, int v) {
  edge[cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

void bfs() {
  queue<Point> q;
  q.push({1, 0});
  vis[1] = 0;
  while (!q.empty()) {
    Point p = q.front();
    q.pop();
    for (int i = head[p.p]; i != -1; i = edge[i].next) {
      if (vis[edge[i].v] == -1) {
        vis[edge[i].v] = p.p;
        q.push({edge[i].v, p.dis + 1});
      }
    }
  }
  return;
}

int main() {
  scanf("%d %d", &n, &m);
  memset(head, -1, sizeof(head));
  memset(vis, -1, sizeof(vis));
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  bfs();
  bool flag = 1;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    puts("Yes");
    for (int i = 2; i <= n; i++) {
      printf("%d\\n", vis[i]);
    }
  } else {
    puts("No");
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
typedef pair<int, int> P;

int main() {
  int N, M;
  vector<stack<int>> room;

  cin >> N >> M;
  room.resize(N + 1);
  int a, b;
  /*rep(i,N+1){
      rep(j,N+1){
          room[i][j]=false;
      }
  }*/
  rep(i, M) {
    cin >> a >> b;
    room[a].push(b);
    room[b].push(a);
  }
  vector<int> visit;
  visit.resize(N + 1);
  visit[0] = 0;
  rep(i, N + 1) { visit[i] = 0; }
  int now_room;
  queue<int> q;
  q.push(1);
  int count = 0;
  while (!q.empty()) {
    now_room = q.front();
    q.pop();
    while (!room[now_room].empty()) {
      int next_room = room[now_room].top();
      room[now_room].pop();
      if (visit[next_room] == 0) {
        q.push(next_room);
        visit[next_room] = now_room;
      }
    }
  }
  bool result = true;
  for (int i = 2; i <= N; ++i) {
    if (visit[i] == 0) {
      result = false;
      break;
    }
  }
  if (result) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; ++i) {
      cout << visit[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
typedef pair<int, int> P;

int main() {
  int N, M;
  vector<vector<bool>> room;

  cin >> N >> M;
  room.resize(N + 1, vector<bool>(N + 1));
  int a, b;
  /*rep(i,N+1){
      rep(j,N+1){
          room[i][j]=false;
      }
  }*/
  rep(i, M) {
    cin >> a >> b;
    room[a][b] = true;
    room[b][a] = true;
  }
  vector<int> visit;
  visit.resize(N + 1);
  visit[0] = 0;
  rep(i, N + 1) { visit[i] = 0; }
  int now_room;
  queue<int> q;
  q.push(1);
  int count = 0;
  while (!q.empty()) {
    now_room = q.front();
    q.pop();
    rep(i, N + 1) {
      if (room[now_room][i] == true && visit[i] == 0) {
        q.push(i);
        visit[i] = now_room;
      }
    }
  }
  bool result = true;
  for (int i = 2; i <= N; ++i) {
    if (visit[i] == 0) {
      result = false;
      break;
    }
  }
  if (result) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; ++i) {
      cout << visit[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<ll> ans(n, -1);
  queue<ll> q;
  q.push(0);
  vector<bool> bb(n, false);
  while (!q.empty()) {
    ll v = q.front();
    bb[v] = true;
    q.pop();
    for (auto nv : G[v]) {
      if (bb[nv] == true || ans[nv] != -1)
        continue;
      q.push(nv);
      ans[nv] = v + 1;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<ll> ans(n, -1);
  queue<ll> q;
  q.push(0);
  vector<bool> bb(n, false);

  while (!q.empty()) {
    ll v = q.front();
    bb[v] = true;
    q.pop();
    for (auto nv : G[v]) {
      if (bb[nv] == true)
        continue;
      q.push(nv);
      if (ans[nv] == -1) {
        ans[nv] = v + 1;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

// usage:
// vector<T> v; v.push_back(T(1, 2, 3)); v.emplace_back(1, 2, 3);
// queue<T> q; q.push(T(1, 2, 3));
// int x, y, z; tie(x, y, z) = q.front(); q.pop();
typedef tuple<int, int, int> T;

void solve() {
  int n, m;
  cin >> n >> m;
  vvi to(n);
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].push_back(v);
    to[v].push_back(u);
  }
  deque<T> q;
  vi dist(n, INF);
  vi used(n, 0);
  vi from(n, 0);
  q.push_back(T(0, -1, 0));
  while (!q.empty()) {
    int u, p, c;
    tie(u, p, c) = q.front();
    q.pop_front();
    if (used[u] && dist[u] <= c)
      continue;
    used[u] = 1;
    dist[u] = c;
    from[u] = p;
    for (auto &v : to[u]) {
      q.push_back(T(v, u, c + 1));
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << from[i] + 1 << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

// usage:
// vector<T> v; v.push_back(T(1, 2, 3)); v.emplace_back(1, 2, 3);
// queue<T> q; q.push(T(1, 2, 3));
// int x, y, z; tie(x, y, z) = q.front(); q.pop();
typedef tuple<int, int, int> T;

void solve() {
  int n, m;
  cin >> n >> m;
  vvi to(n);
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].push_back(v);
    to[v].push_back(u);
  }
  deque<T> q;
  vi dist(n, -1);
  vi used(n, 0);
  vi from(n, 0);
  q.push_back(T(0, -1, 0));
  while (!q.empty()) {
    int u, p, c;
    tie(u, p, c) = q.front();
    q.pop_front();
    if (used[u] && dist[u] < c)
      continue;
    used[u] = 1;
    dist[u] = c;
    from[u] = p;
    for (auto &v : to[u]) {
      q.push_back(T(v, u, c + 1));
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << from[i] + 1 << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1e9;

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

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

  queue<int> q;
  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int nv : list[v]) {
      if (dist[nv] != INF) {
        continue;
      }
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      q.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;
typedef long long ll;

const int INF = 1e9;

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

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

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

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  ll m;
  cin >> m;
  vector<vector<ll>> l(n);
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    l[a].push_back(b);
    l[b].push_back(a);
  }
  deque<pair<ll, ll>> q;
  vector<ll> ans(n, -2);
  q.push_back(pair<ll, ll>(0, 0));
  while (q.size() != 0) {
    ll now = q.front().first;
    ll old = q.front().second;
    q.pop_front();

    ans[now] = old;

    // cout<<now<<":";
    // cout << "q: "; REP(i,(signed)q.size()) cout<<q[i].first<<" "; cout <<
    // endl; //----------cout----------//

    for (auto &i : l[now]) {
      if (i == old)
        continue;
      if (ans[i] != -2)
        continue;
      q.push_back(pair<ll, ll>(i, now));
      ans[i] = -1;
    }
  }
  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << ans[i] + 1 << endl; }
  return 0;
}
/*
10 10
1 2
2 4
4 7
2 5
5 8
5 9
1 3
3 6
6 10
3 5
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  ll m;
  cin >> m;
  vector<vector<bool>> l(n, vector<bool>(n));
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    l[a][b] = true;
    l[b][a] = true;
  }
  deque<pair<ll, ll>> q;
  vector<ll> ans(n, -2);
  q.push_back(pair<ll, ll>(0, 0));
  while (q.size() != 0) {
    ll now = q.front().first;
    ll old = q.front().second;
    q.pop_front();

    ans[now] = old;

    // cout<<now<<":";
    // cout << "q: "; REP(i,(signed)q.size()) cout<<q[i].first<<" "; cout <<
    // endl; //----------cout----------//

    REP(i, n) {
      if (i == old)
        continue;
      if (ans[i] != -2)
        continue;
      if (l[now][i] == true) {
        q.push_back(pair<ll, ll>(i, now));
        ans[i] = -1;
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 1, n) { cout << ans[i] + 1 << endl; }
  return 0;
}
/*
10 10
1 2
2 4
4 7
2 5
5 8
5 9
1 3
3 6
6 10
3 5
*/
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using graph = vector<vector<int>>;

const int INF = 1001001001;

int main() {
  int n, m;
  cin >> n >> m;
  graph G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> dist(n, INF);
  queue<int> q;
  vector<int> kari(n, -1);

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

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

    for (int nv : G[v]) {
      if (dist[nv] != INF)
        continue;

      dist[nv] = dist[v] + 1;
      kari[nv] = v;
      q.push(nv);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    int an = kari[i];
    an++;
    cout << an << endl;
  }
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

const int INF = 1001001001;

struct edge {
  ll to, cost;
};
struct graph {
  ll V;
  vector<vector<edge>> G;
  vector<ll> d;

  graph(ll n) { init(n); }

  void init(ll n) {
    V = n;
    G.resize(V);
    d.resize(V);
    rep(i, V) { d[i] = INF; }
  }

  void add_edge(ll s, ll t, ll cost) {
    edge e;
    e.to = t, e.cost = cost;
    G[s].push_back(e);
  }

  void dikstra(ll s) {
    rep(i, V) { d[i] = INF; }
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      ll v = p.second;
      if (d[v] < p.first)
        continue;
      for (auto e : G[v]) {
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.push(P(d[e.to], e.to));
        }
      }
    }
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  graph g(n);
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    g.add_edge(a, b, 1);
  }
  g.dikstra(1);

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

#include <iostream>
#include <queue>

using namespace std;

const int INF = 1e9;

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

  int ans[N], depth[N];
  for (int i = 0; i < N; i++) {
    ans[i] = INF;
    depth[i] = INF;
  }
  depth[0] = 0;

  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();

    for (int i = 0; i < v[temp].size(); i++) {
      if (depth[v[temp][i]] > depth[temp] + 1) {
        depth[v[temp][i]] = depth[temp] + 1;
        ans[v[temp][i]] = temp;
        q.push(v[temp][i]);
      }
    }
  }

  bool isok = true;
  for (int i = 1; i < N; i++) {
    if (ans[i] == INF) {
      isok = false;
    }
  }
  if (isok) {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++) {
      cout << ans[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

const int INF = 1e9;

int main() {
  int N, M;
  cin >> N >> M;
  int A[M], B[M];
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  int ans[N], depth[N];
  for (int i = 0; i < N; i++) {
    ans[i] = INF;
    depth[i] = INF;
  }
  depth[0] = 0;

  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();

    for (int i = 0; i < N; i++) {
      if (A[i] == temp) {
        if (depth[B[i]] > depth[temp] + 1) {
          depth[B[i]] = depth[temp] + 1;
          ans[B[i]] = temp;
          q.push(B[i]);
        }
      }
      if (B[i] == temp) {
        if (depth[A[i]] > depth[temp] + 1) {
          depth[A[i]] = depth[temp] + 1;
          ans[A[i]] = temp;
          q.push(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 <iostream>
#include <map>
#include <queue>
#include <vector>

#include <unordered_set>

using namespace std;
map<int, unordered_set<int>> paths;

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

  for (int i = 0; i < m; ++i) {
    int t1;
    int t2;
    cin >> t1 >> t2;
    paths[t2].insert(t1);
    paths[t1].insert(t2);
  }

  map<int, int> ans;
  queue<int> roots;
  vector<int> visits(n + 1);
  visits[1] = 1;
  roots.push(1);
  //  while (roots.empty) {
  while (!roots.empty()) {
    int r = roots.front();
    roots.pop();
    for (auto p : paths[r]) {
      if (visits[p] == 0)
        ans[p] = r;
      if (visits[p] == 0)
        roots.push(p);
      visits[p] = 1;
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

#include <unordered_set>

using namespace std;
map<int, unordered_set<int>> paths;

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

  for (int i = 0; i < m; ++i) {
    int t1;
    int t2;
    cin >> t1 >> t2;
    paths[t2].insert(t1);
    paths[t1].insert(t2);
  }

  map<int, int> ans;
  queue<int> roots;
  roots.push(1);
  //  while (roots.empty) {
  while (!roots.empty()) {
    int r = roots.front();
    roots.pop();
    for (auto p : paths[r]) {
      if (ans[p] == 0)
        ans[p] = r;
      // delete the key for parent routes...otherwise loop never ends
      paths[p].erase(r);
      roots.push(p);
    }
  }

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

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

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

const int INF = 1001001001;

int main() {
  int n, m;
  int a, b;

  cin >> n >> m;
  vector<int> G[100005];

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

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

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

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

    for (vector<int>::iterator it = G[v].begin(); it != G[v].end(); it++) {
      if (dist[*it] != INF)
        continue;

      dist[*it] = dist[v] + 1;
      pre[*it] = v;
      que.push(*it);
    }
  }

  cout << "Yes" << endl;

  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

const int INF = 100100100;

int main() {
  int n, m;
  int a, b;

  cin >> n >> m;
  vector<int> G[10005];

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

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

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

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

    for (vector<int>::iterator it = G[v].begin(); it != G[v].end(); it++) {
      if (dist[*it] != INF)
        continue;

      dist[*it] = dist[v] + 1;
      pre[*it] = v;
      que.push(*it);
    }
  }

  cout << "Yes" << endl;

  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>

const int MAX_N = 110000;
const int MAX_M = 220000;
std::vector<int> edge[MAX_N];
std::vector<int> costs(MAX_N, MAX_M);
std::vector<int> ans(MAX_N, -1);

void dijkstra() {
  auto compare = [&](int a, int b) { return costs[a] > costs[b]; };
  std::priority_queue<int, std::vector<int>, decltype(compare)> que{compare};
  que.push(0);
  costs[0] = 0;
  ans[0] = 0;
  while (!que.empty()) {
    int from = que.top();
    que.pop();
    for (auto to : edge[from]) {
      if (costs[from] + 1 < costs[to]) {
        costs[to] = costs[from] + 1;
        ans[to] = from;
        que.push(to);
      }
    }
  }
  return;
}
int main() {
  int n, m;
  std::cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    std::cin >> a >> b;
    --a;
    --b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dijkstra();
  std::cout << "Yes" << std::endl;
  for (int i = 1; i < n; ++i) {
    std::cout << ans[i] + 1 << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

const int MAX_N = 110000;
const int MAX_M = 220000;
std::vector<int> edge[MAX_N];
std::vector<int> costs(MAX_N, MAX_M);
std::vector<int> ans(MAX_N, -1);

void dfs(int x, int past, int cost = 0) {
  ans[x] = past;
  costs[x] = cost;
  cost++;
  for (auto to : edge[x]) {
    if (ans[to] != -1 && costs[to] <= cost) {
      continue;
    }
    dfs(to, x, cost);
  }
  return;
}
int main() {
  int n, m;
  std::cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    std::cin >> a >> b;
    --a;
    --b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dfs(0, 0, 0);
  std::cout << "Yes" << std::endl;
  for (int i = 1; i < n; ++i) {
    std::cout << ans[i] + 1 << std::endl;
  }
  return 0;
}