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

// ABC168-D
#include <bits/stdc++.h>
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
namespace you {
std::string to_string(char val) {
  return std::string(1, val);
  // return std::string{val};
  // you::to_string(char)
}
} // namespace you

using Graph = vector<vector<int>>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int n, m;
  cin >> n >> m;

  Graph node(
      n +
      1); // using graphを宣言しておくことで、nodeの長さを入力後に決められる。
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    node[a].push_back(b);
    node[b].push_back(a);
  }

  int inf = pow(10, 9);
  vector<int> oya(n + 1, inf);
  oya[1] = 1;
  queue<int> que;
  que.push(1);

  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int next : node[now]) {
      if (oya[next] != inf)
        continue;
      oya[next] = now;
      que.push(next);
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << oya[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

//
#include <bits/stdc++.h>
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
namespace you {
std::string to_string(char val) {
  return std::string(1, val);
  // return std::string{val};
  // you::to_string(char)
}
} // namespace you

int num = pow(10, 5) + 10;
vector<vector<int>> node(num);
vector<int> seen(num, 0);
vector<int> miti(num);
vector<int> setu(num, 0);
vector<int> ans(num, 0);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    node[a].push_back(b);
    node[b].push_back(a);
    setu[a] += 1;
    setu[b] += 1;
  }
  vector<int> next(num, 1);
  vector<int> hen(num, 0);
  int sou = 1;
  int total = 0; // 今まで見た数
  while (total <= n) {
    int ne = 0;
    for (int i = 0; i < sou; i++) {
      int now = next[total + (i + 1)];
      // cout<<now<<endl;
      seen[now] = 1;
      for (int j = 0; j < setu[now]; j++) {
        if (seen[node[now][j]] == 1)
          continue;
        ne += 1;
        next[total + sou + ne] = node[now][j];
        if (hen[node[now][j]] == 0) {
          ans[node[now][j]] = now;
          hen[node[now][j]] = 1;
        }
      }
    }
    total += sou;
    sou = ne;
  }
  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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;

vector<int> G[100100];

int main() {
  int n, m;
  cin >> n >> m;
  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, -1);
  vector<int> pre(n, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

vector<int> G[10010];

int main() {
  int n, m;
  cin >> n >> m;
  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, -1);
  vector<int> pre(n, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  vector<int> next(n, -1);
  queue<int> que;

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

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

    for (int num = 0; num < g[v].size(); ++num) {
      int nv = g[v][num];
      if (next[nv] != -1)
        continue;
      next[nv] = v;
      que.push(nv);
    }
  }

  cout << "Yes" << endl;
  for (int v = 1; v < n; ++v)
    cout << next[v] + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

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

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

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

    for (int num = 0; num < g[v].size(); ++num) {
      int nv = g[v][num];
      if (next[nv] != -1)
        continue;
      next[nv] = v;
      dist[nv] = dist[v] + 1;
      int index = -1;
      que.push(nv);
    }
  }

  bool ok = true;
  int pos;
  int cur_dist;
  for (int v = 1; v < n; ++v) {
    cur_dist = 1;
    pos = next[v];
    while (pos != 0) {
      pos = next[pos];
      cur_dist++;
    }
    if (cur_dist != dist[v]) {
      ok = false;
      cout << "No" << endl;
      return 0;
    }
  }

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

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

int main() {
  int N, M;
  cin >> N >> M;
  int A[M], B[M];
  rep(i, M) cin >> A[i] >> B[i];
  //

  vector<vector<int>> G(N + 1);
  rep(i, M) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }

  int visit[N + 1];
  rep(i, N + 1) visit[i] = false;

  int ans[N + 1];
  rep(i, N + 1) ans[i] = -1;

  queue<int> q;
  int now, next;
  q.push(1);
  visit[1] = true;
  while (q.empty() != 1) {
    now = q.front();
    q.pop();
    int size = G[now].size();
    rep(i, size) {
      next = G[now][i];
      if (visit[next] == false) {
        visit[next] = true;
        ans[next] = now;
        q.push(next);
      }
    }
  }

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

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

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  int A[M], B[M];
  rep(i, M) cin >> A[i] >> B[i];
  //

  vector<vector<int>> G(M + 1);
  rep(i, M) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }

  int visit[N + 1];
  rep(i, N) visit[i + 1] = false;

  int ans[N + 1];
  rep(i, N + 1) ans[i] = -1;

  queue<int> q;
  int now, next;
  q.push(1);
  visit[1] = true;
  while (q.empty() != 1) {
    now = q.front();
    q.pop();
    rep(i, G[now].size()) {
      next = G[now][i];
      if (visit[next] == false) {
        visit[next] = true;
        ans[next] = now;
        q.push(next);
      }
    }
  }

  for (int i = 2; i <= N; i++)
    if (ans[i] == -1) {
      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: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<set<int>> path(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    path.at(a - 1).insert(b - 1);
    path.at(b - 1).insert(a - 1);
  }
  queue<int> q;
  vector<int> depth(n, -1);
  vector<int> done(n, 0);
  vector<int> parent(n, 0);
  q.push(0);
  done.at(0) = 1;
  depth.at(0) = 0;
  while (!q.empty()) {
    int front = q.front();
    done.at(front) = 1;
    for (auto value : path.at(front)) {
      if (done.at(value) == 0) {
        q.push(value);
        done.at(value) = 1;
        depth.at(value) = depth.at(front) + 1;
        parent.at(value) = front;
      }
    }
    q.pop();
  }

  for (int i = 0; i < n; i++) {
    if (depth.at(i) == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << parent.at(i) + 1 << endl;
    // cout << depth.at(i)  << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<set<int>> path(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    path.at(a - 1).insert(b - 1);
    path.at(b - 1).insert(a - 1);
  }
  queue<int> q;
  vector<int> depth(n, -1);
  vector<int> done(n, 0);
  vector<int> parent(n, 0);
  q.push(0);
  done.at(0) = 1;
  depth.at(0) = 0;
  while (!q.empty()) {
    int front = q.front();
    done.at(front) = 1;
    for (int i = 0; i < n; i++) {
      if (path.at(front).count(i) && done.at(i) == 0) {
        q.push(i);
        done.at(i) = 1;
        depth.at(i) = depth.at(front) + 1;
        parent.at(i) = front;
      }
    }
    q.pop();
  }

  for (int i = 0; i < n; i++) {
    if (depth.at(i) == -1) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << parent.at(i) + 1 << endl;
    // cout << depth.at(i)  << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pi 3.14159265358979323846

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> 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);
  }

  queue<int> que;
  que.push(0);
  vector<bool> visit(n, false);
  vector<int> key(n, -1); // 道しるべ

  while (que.size() != 0) {
    int now = que.front();
    que.pop();
    if (visit[now] == true)
      continue;

    visit[now] = true;

    for (auto next : G[now]) {
      if (visit[next] == true)
        continue;

      if (key[next] == -1) {
        key[next] = now;
      }
      que.push(next);
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pi 3.14159265358979323846

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> 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);
  }

  queue<int> que;
  que.push(0);
  vector<bool> visit(n, false);
  vector<int> key(n, -1); // 道しるべ

  while (que.size() != 0) {
    int now = que.front();
    que.pop();

    visit[now] = true;

    for (auto next : G[now]) {
      if (visit[next] == true)
        continue;

      if (key[next] == -1) {
        key[next] = now;
      }
      que.push(next);
    }
  }

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

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  //    cout << fixed << setprecision(10);
  int N, M;
  map<int, bool> visited;
  visited[1] = true;
  cin >> N >> M;
  vector<int> previousMark(N);
  vector<vector<int>> rooms(N + 1);
  for (int i = 1; i <= N; i++) {
    rooms[i] = vector<int>();
  }
  int A, B;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    rooms[A].push_back(B);
    rooms[B].push_back(A);
  }

  vector<int> queue;
  queue.push_back(1);
  while (queue.size() > 0) {
    vector<int> nextQueue;
    for (int i = 0; i < queue.size(); i++) {
      int currentRoom = queue[i];
      vector<int> nextRooms = rooms[currentRoom];
      for (int j = 0; j < nextRooms.size(); j++) {
        if (visited[nextRooms[j]] == false) {
          visited[nextRooms[j]] = true;
          nextQueue.push_back(nextRooms[j]);
          previousMark[nextRooms[j] - 2] = currentRoom;
        }
      }
    }
    queue = nextQueue;
  }

  cout << "Yes" << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << previousMark[i] << endl;
  }

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  //    cout << fixed << setprecision(10);
  int N, M;
  map<int, bool> visited;
  cin >> N >> M;
  vector<int> previousMark(N);
  vector<vector<int>> rooms(N + 1);
  for (int i = 1; i <= N; i++) {
    rooms[i] = vector<int>();
  }
  int A, B;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    rooms[A].push_back(B);
    rooms[B].push_back(A);
  }

  vector<int> queue;
  queue.push_back(1);
  while (queue.size() > 0) {
    vector<int> nextQueue;
    for (int i = 0; i < queue.size(); i++) {
      int currentRoom = queue[i];
      vector<int> nextRooms = rooms[currentRoom];
      for (int j = 0; j < nextRooms.size(); j++) {
        if (visited[nextRooms[j]] == false) {
          visited[nextRooms[j]] = true;
          nextQueue.push_back(nextRooms[j]);
          previousMark[nextRooms[j] - 2] = currentRoom;
        }
      }
    }
    queue = nextQueue;
  }

  cout << "Yes" << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << previousMark[i] << endl;
  }

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



#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

vector<int> G[100005];
int dist[100005];
int ans[100005];
bool check[100005];

void bfs(int st) {

  queue<int> que;
  que.push(st);
  dist[st] = 0;
  ans[st] = 1;

  while (!que.empty()) {

    int now = que.front();
    que.pop();

    for (auto nxt : G[now]) {
      if (dist[nxt] > dist[now] + 1) {
        ans[nxt] = now;
        dist[nxt] = dist[now] + 1;
        que.push(nxt);
      }
    }
  }
}

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

  for (int i = 0; i < 100005; i++) {
    dist[i] = ans[i] = 1e9;
  }

  dist[1] = 0;
  bfs(1);

  bool flag = true;
  for (int i = 1; i <= N; i++) {
    if (dist[i] == 1e9) {
      flag = false;
      break;
    }
  }

  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:



#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> G[100005];
int dist[100005];
int ans[100005];

void dfs(int now, int pa) {

  for (auto nxt : G[now]) {

    if (nxt == pa)
      continue;
    if (dist[nxt] > dist[now] + 1) {
      dist[nxt] = dist[now] + 1;
      ans[nxt] = now;
      dfs(nxt, now);
    }
  }
}

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

  for (int i = 0; i < 100005; i++) {
    dist[i] = ans[i] = 1e9;
  }

  dist[1] = 0;
  dfs(1, -1);

  bool flag = true;
  for (int i = 1; i <= N; i++) {
    if (dist[i] == 1e9) {
      flag = false;
      break;
    }
  }

  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= N; i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

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

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

int N, M;
vector<int> v[100010];
int s[100010] = {};
bool gone[100010] = {};

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int A, B;
    scanf("%d%d", &A, &B);
    v[A].push_back(B);
    v[B].push_back(A);
  }

  queue<pair<int, int>> q;
  q.push(make_pair(1, -1));
  while (!q.empty()) {
    auto e = q.front();
    q.pop();
    int cur = e.first;
    int par = e.second;
    if (gone[cur])
      continue;
    gone[cur] = true;
    s[cur] = par;
    for (int child : v[cur]) {
      q.push(make_pair(child, cur));
    }
  }

  for (int i = 2; i <= N; ++i) {
    if (!gone[i]) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= N; ++i) {
    cout << s[i] << endl;
  }
}


Error: TLE

Fixed submission:

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

int N, M;
vector<int> v[100010];
int d[100010] = {};
int s[100010] = {};

void dfs(int cur, int par, int depth) {
  if (d[cur] < depth)
    return;
  s[cur] = par;
  d[cur] = depth;
  for (int child : v[cur]) {
    dfs(child, cur, depth + 1);
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int A, B;
    scanf("%d%d", &A, &B);
    v[A].push_back(B);
    v[B].push_back(A);
  }
  for (int i = 0; i <= N; ++i) {
    d[i] = 1e9;
  }

  dfs(1, -1, 0);

  for (int i = 2; i <= N; ++i) {
    if (d[i] == 1e9) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= N; ++i) {
    cout << s[i] << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<pair<int, int>> p;
  int check[n];
  int ans[n];
  for (int i = 0; i < n; i++) {
    check[i] = 100000000;
    ans[i] = 100000000;
  }
  check[0] = 0;
  ans[0] = 0;
  vector<int> gra[n];

  for (int i = 0; i < m; i++) {
    int k, t;
    cin >> k >> t;
    k--;
    t--;
    gra[k].push_back(t);
    gra[t].push_back(k);
  }

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

    for (int i = 0; i < gra[k].size(); i++) {
      if (check[gra[k][i]] > check[k] + 1) {
        check[gra[k][i]] = check[k] + 1;
        q.push(gra[k][i]);
        ans[gra[k][i]] = k + 1;
      }
    }
  }

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

Error: 0

Fixed submission:

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

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

  vector<pair<int, int>> p;
  int check[n];
  for (int i = 0; i < n; i++) {
    check[i] = -1;
  }
  check[0] = 0;
  int gra[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      gra[i][j] = -1;
    }
  }

  for (int i = 0; i < m; i++) {
    int k, t;
    cin >> k >> t;
    k--;
    t--;
    gra[k][t] = 1;
    gra[t][k] = 1;
  }

  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    queue<int> q_2 = q;
    while (!q.empty()) {
      q.pop();
    }
    while (!q_2.empty()) {
      int t = q_2.front();
      q_2.pop();
      for (int i = 0; i < n; i++) {
        if (gra[i][t] == 1 && check[i] == -1) {
          check[i] = t + 1;
          q.push(i);
        }
      }
    }
  }

  int flag = 0;
  for (int i = 1; i < n; i++) {
    if (check[i] == -1) {
      flag = 1;
      break;
    }
  }

  if (flag == 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << check[i] << endl;
    }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define INF = 1001001001
#define pi = acos(-1)
using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

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> pre(n, -1);
  queue<int> q;

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

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

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


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define INF = 1001001001
using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

int main() {
  int n, m;
  cin >> 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> pre(n, -1);
  queue<int> q;

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

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int nv : to[v]) {
      if (pre[nv] != -1)
        continue;
      pre[nv] = pre[v];
      q.push(nv);
    }
  }

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