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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
vector<vector<int>> mymap(100001, vector<int>());
int res[100001];
queue<pair<int, int>> q;

void dfs(int from, int i) {
  if (res[i] != 0)
    return;

  res[i] = from;

  for (auto &room : mymap[i]) {
    if (res[room] != 0)
      continue;
    q.push(make_pair(i, room));
  }
}

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

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

  for (int i = 0; i < n + 1; i++)
    res[i] = 0;

  q.push(make_pair(0, 1));

  while (q.size() != 0) {
    pair<int, int> arg = q.front();
    q.pop();
    dfs(arg.first, arg.second);
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
vector<vector<int>> mymap(100001, vector<int>());
int res[100001];
queue<pair<int, int>> q;

void dfs(int from, int i) {
  if (res[i] != 0)
    return;

  res[i] = from;
  if (i == 1)
    res[i] = 0;

  for (auto &room : mymap[i]) {
    if (res[room] != 0)
      continue;
    q.push(make_pair(i, room));
  }
}

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

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

  for (int i = 0; i < n + 1; i++)
    res[i] = 0;

  q.push(make_pair(0, 1));

  while (q.size() != 0) {
    pair<int, int> arg = q.front();
    q.pop();
    dfs(arg.first, arg.second);
  }

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

#include <bits/stdc++.h>

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

int main(void) {
  int N, M, a, b;
  cin >> N >> M;
  Graph G(N);
  // 　N；頂点　M；辺

  for (int i = 0; i < M; i++) {
    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; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 nv について距離情報を更新してキューに追加する
      dist[nv] = dist[v] + 1;
      que.push(nv);
      pre[nv] = v;
    }
  }
  cout << "Yes" << endl;
  for (int v = 1; v < N; ++v) {
    cout << pre[v] + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

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

int main(void) {
  int N, M, a, b;
  cin >> N >> M;
  Graph G(N);
  // 　N；頂点　M；辺

  for (int i = 0; i < N; i++) {
    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; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 nv について距離情報を更新してキューに追加する
      dist[nv] = dist[v] + 1;
      que.push(nv);
      pre[nv] = v;
    }
  }
  cout << "Yes" << endl;
  for (int v = 1; v < N; ++v) {
    cout << pre[v] + 1 << endl;
  }

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

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

int main() {
  int64_t n, m, a, b, ch, h, k, d;
  d = 0;
  k = 0;
  h = 0;
  ch = 0;
  cin >> n >> m;
  vector<vector<int64_t>> data(n);
  for (int64_t i = 0; i < m; i++) {
    cin >> a >> b;
    data.at(a - 1).push_back(b - 1);
    data.at(b - 1).push_back(a - 1);
  }
  vector<int64_t> num(n);
  vector<int64_t> pas(n);
  vector<int64_t> hon;
  vector<int64_t> kari;
  vector<int64_t> hikaku = {};
  hon.push_back(0);
  while (ch != 0 || hon != hikaku) {
    h = hon.size();
    for (int64_t i = 0; i < h; i++) {
      d = data.at(hon.at(i)).size();
      for (int64_t j = 0; j < d; j++) {
        if (num.at(data.at(hon.at(i)).at(j)) == 0 &&
            data.at(hon.at(i)).at(j) != 0) {
          num.at(data.at(hon.at(i)).at(j)) = num.at(hon.at(i)) + 1;
          kari.push_back(data.at(hon.at(i)).at(j));
          pas.at(data.at(hon.at(i)).at(j)) = hon.at(i);
        }
      }
    }
    ch = 1;
    hon = {};
    k = kari.size();
    for (int i = 0; i < k; i++) {
      hon.push_back(kari.at(i));
    }
    kari = {};
    ch = 0;
  }
  cout << "Yes" << endl;
  for (int64_t i = 1; i < n; i++) {
    cout << pas.at(i) + 1 << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)


Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> data(n);
  for (int i = 0; i < m; i++) {
    int s, t;
    s = 0;
    t = 0;
    cin >> s >> t;
    data.at(s - 1).push_back(t);
    data.at(t - 1).push_back(s);
  }
  vector<int> vec(n, 10000000);
  vector<int> list;
  vec.at(0) = 0;
  list.push_back(0);
  cout << "No" << endl;
  for (int i = 0; i < list.size(); i++) {
    for (int j = 0; j < data.at(list.at(i)).size(); j++) {
      vec.at(data.at(list.at(i)).at(j) - 1) =
          min(vec.at(list.at(i)) + 1, vec.at(data.at(list.at(i)).at(j)));
      list.push_back(data.at(list.at(i)).at(j) - 1);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < data.at(i).size(); j++) {
      if (vec.at(data.at(i).at(j) - 1) == vec.at(i) - 1) {
        cout << data.at(i).at(j) << endl;
        break;
      }
    }
  }
  for (int i = 0; i < vec.size(); i++) {
    cout << vec.size() << endl;
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> d(n, -1);
  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> q;
  q.push(0);
  while (1) {
    if (q.empty())
      break;
    int qf = q.front();
    q.pop();
    for (auto i : g[qf]) {
      if (d[i] != -1)
        continue;
      q.push(i);
      d[i] = qf;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << d[i] + 1 << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m), c(m, -1);
  vector<int> d(n, -1);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  queue<int> q;
  q.push(0);
  while (1) {
    if (q.empty())
      break;
    int dt = q.front();
    q.pop();
    for (int i = 0; i < m; i++) {
      if (c[i] == 1)
        continue;

      if (a[i] == dt && d[b[i]] == -1) {
        d[b[i]] = dt;
        q.push(b[i]);
        c[i] = 1;
      }

      if (b[i] == dt && d[a[i]] == -1) {
        d[a[i]] = dt;
        q.push(a[i]);
        c[i] = 1;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << d[i] + 1 << endl;
  }

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

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define sz(v) (v).size()
#define all(v) (v).begin(), (v).end()
using namespace std;
template <typename T> T Max(T X, T Y) { return X > Y ? X : Y; }
template <typename T> T Min(T X, T Y) { return X < Y ? X : Y; }
template <typename T> void chmax(T &X, T Y) {
  X = X > Y ? X : Y;
  return;
}
template <typename T> void chmin(T &X, T Y) {
  X = X < Y ? X : Y;
  return;
}

const int maxn = 100005, maxm = 200005;
int n, m;
int len, to[maxm << 1], nxt[maxm << 1], head[maxn], dist[maxn], from[maxn];
queue<int> q;

inline void addedge(int u, int v) {
  len++, nxt[len] = head[u], head[u] = len, to[len] = v;
  return;
}

int main() {
  memset(dist, -1, sizeof(dist));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }

  dist[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int pos = q.front();
    q.pop();
    for (int i = head[pos]; i; i = nxt[i]) {
      if (dist[to[i]] != -1)
        continue;
      dist[to[i]] = dist[pos] + 1;
      from[to[i]] = pos;
      q.push(to[i]);
    }
  }

  for (int i = 1; i <= n; i++)
    if (dist[i] == -1) {
      printf("No\\n");
      return 0;
    }

  printf("Yes\\n");
  for (int pos = 2; pos <= n; pos++)
    printf("%d\\n", from[pos]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define sz(v) (v).size()
#define all(v) (v).begin(), (v).end()
using namespace std;
template <typename T> T Max(T X, T Y) { return X > Y ? X : Y; }
template <typename T> T Min(T X, T Y) { return X < Y ? X : Y; }
template <typename T> void chmax(T &X, T Y) {
  X = X > Y ? X : Y;
  return;
}
template <typename T> void chmin(T &X, T Y) {
  X = X < Y ? X : Y;
  return;
}

const int maxn = 100005, maxm = 200005;
int n, m;
int len, to[maxm >> 1], nxt[maxm >> 1], head[maxn], dist[maxn], from[maxn];
queue<int> q;

inline void addedge(int u, int v) {
  len++, nxt[len] = head[u], head[u] = len, to[len] = v;
  return;
}

int main() {
  memset(dist, -1, sizeof(dist));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }

  dist[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int pos = q.front();
    q.pop();
    for (int i = head[pos]; i; i = nxt[i]) {
      if (dist[to[i]] != -1)
        continue;
      dist[to[i]] = dist[pos] + 1;
      from[to[i]] = pos;
      q.push(to[i]);
    }
  }

  for (int i = 1; i <= n; i++)
    if (dist[i] == -1) {
      printf("No\\n");
      return 0;
    }

  printf("Yes\\n");
  for (int pos = 2; pos <= n; pos++)
    printf("%d\\n", from[pos]);
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF 1023456789
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define trav(it, v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long long ll;

vi adj[100000 + 5];
int n, m;
int vis[100000 + 5];
int cc = 1;
void bfs(int i) {
  queue<int> q;
  q.push(i);
  vis[i] = -1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : adj[u]) {
      if (!vis[v]) {
        cc++;
        vis[v] = u;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  int a, b;
  rep(i, 0, m) {
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  memset(vis, 0, sizeof vis);
  bfs(1);
  if (cc < n) {
    printf("No");
  } else {
    printf("Yes\\n");
    rep(i, 2, n + 1) { printf("%d\\n", vis[i]); }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 1023456789
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define trav(it, v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long long ll;

vi adj[10000 + 5];
int n, m;
int vis[10000 + 5];
int cc = 1;
void bfs(int i) {
  queue<int> q;
  q.push(i);
  vis[i] = -1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : adj[u]) {
      if (!vis[v]) {
        cc++;
        vis[v] = u;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  int a, b;
  rep(i, 0, m) {
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  memset(vis, 0, sizeof vis);
  bfs(1);
  if (cc < n) {
    printf("No");
  } else {
    printf("Yes\\n");
    rep(i, 2, n + 1) { printf("%d\\n", vis[i]); }
  }
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define PIE 3.14159265358979323

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T GCD(T a, T b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  queue<int> q;
  vector<int> this_way(n, 0);
  q.push(0);
  this_way[0] = 1;
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    for (int i = 0; i < g[a].size(); i++) {
      if (this_way[g[a][i]] == 0) {
        this_way[g[a][i]] = a + 1;
        q.push(g[a][i]);
      }
    }
  }
  rep(i, n) {
    if (!this_way[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  repk(i, 1, n) cout << this_way[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define PIE 3.14159265358979323

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T GCD(T a, T b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> g(n, vector<bool>(n, false));
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    g[a - 1][b - 1] = true;
    g[b - 1][a - 1] = true;
  }
  queue<int> q;
  vector<int> this_way(n, 0);
  q.push(0);
  this_way[0] = 1;
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    rep(i, n) {
      if (g[i][a] && this_way[i] == 0) {
        this_way[i] = a + 1;
        q.push(i);
      }
    }
  }
  rep(i, n) {
    if (!this_way[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  repk(i, 1, n) cout << this_way[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<int> A(M), B(M);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  map<int, set<int>> m;
  for (int i = 0; i < M; i++) {
    m[A[i]].insert(B[i]);
    m[B[i]].insert(A[i]);
  }

  vector<int> AB(N + 1);
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int key = q.front();
    for (auto v : m[key]) {
      if (AB[v] != 0)
        continue;
      AB[v] = key;
      q.push(v);
    }
    q.pop();
  }

  bool flag = false;
  for (int i = 2; i < N + 1; i++) {
    // cerr << AB[i] << endl;
    if (AB[i] == 0)
      flag = true;
  }

  if (flag) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i < N + 1; i++) {
      cout << AB[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> A(M), B(M);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  vector<int> AB(N + 1);
  queue<int> q;
  for (int i = 0; i < M; i++) {
    if (A[i] == 1) {
      AB[B[i]] = 1;
      q.push(B[i]);
    } else if (B[i] == 1) {
      AB[A[i]] = 1;
      q.push(A[i]);
    }
  }

  while (!q.empty()) {
    int key = q.front();
    q.pop();
    for (int i = 0; i < M; i++) {
      // if (AB[key] != 0) continue;
      if (A[i] == key) {
        if (AB[B[i]] != 0)
          continue;
        AB[B[i]] = key;
        q.push(B[i]);
      } else if (B[i] == key) {
        if (AB[A[i]] != 0)
          continue;
        AB[A[i]] = key;
        q.push(A[i]);
      }
    }
  }

  bool flag = false;
  for (int i = 2; i < N + 1; i++) {
    // cerr << AB[i] << endl;
    if (AB[i] == 0)
      flag = true;
  }

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

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g[n + 1];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<bool> visit(n + 1);
  vector<int> paraent(n + 1);
  queue<int> q;
  q.push(1);
  visit[1] = 1;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (auto x : g[cur]) {
      if (!visit[x]) {
        visit[x] = 1;
        q.push(x);
        paraent[x] = cur;
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i <= n; i++)
    cout << paraent[i] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g[n];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> visit(n + 1);
  vector<int> paraent(n + 1);
  queue<int> q;
  q.push(1);
  visit[1] = 1;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (auto x : g[cur]) {
      if (!visit[x]) {
        visit[x] = 1;
        q.push(x);
        paraent[x] = cur;
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 2; i < n; i++)
    cout << paraent[i] << endl;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> edge(N);
  for (int i = 0; i < M; ++i) {
    int s, t;
    std::cin >> s >> t;
    edge[s - 1].push_back(t - 1);
    edge[t - 1].push_back(s - 1);
  }
  constexpr int inf = 1e9;
  std::vector<int> d(N, inf), prev(N, 0);
  d[0] = 0;
  std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>,
                      std::greater<>>
      que;
  que.emplace(d[0], 0);
  while (not que.empty()) {
    auto tp = que.top();
    que.pop();
    int cv, cd;
    std::tie(cd, cv) = tp;
    if (cd > d[cv])
      continue;
    for (const auto nv : edge[cv]) {
      if (d[nv] > cd + 1) {
        d[nv] = cd + 1;
        prev[nv] = cv;
        que.emplace(d[nv], nv);
      }
    }
  }
  if (std::count_if(d.begin(), d.end(), [&](auto e) { return e == inf; })) {
    std::cout << "No" << std::endl;
  } else {
    std::cout << "Yes" << std::endl;
    prev.erase(prev.begin());
    for (const auto pv : prev) {
      std::cout << pv + 1 << std::endl;
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
using namespace std;
struct Node {
  // ノード(親ノードと複数の子ノードを保存)
  int parent;
  vector<int> child;
};
void set_child(Node nodes[], int node_idx, int *Map_ptr, int N, int *list,
               queue<int> &que) {
  // 子ノードを全てセットする
  for (int i = 0; i < N; i++) {
    if (Map_ptr[i] == 1 && list[i] != 1) { // まだ書いてないノードだけ書く
      nodes[node_idx].child.push_back(i);
      nodes[i].parent = node_idx;
      list[i] = 1;
      que.push(i);
    }
  }
};
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  int **Map = (int **)malloc(sizeof(int *) * N);
  for (int i = 0; i < N; ++i)
    Map[i] = (int *)malloc(sizeof(int) * N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      Map[i][j] = 0;
    }
  }
  // 2次元配列を関数に渡すために表現行列の各行のポインタを配列化
  int **Map_ptr = (int **)malloc(sizeof(int *) * N);
  for (int i = 0; i < N; i++) {
    Map_ptr[i] = Map[i];
  }
  // 表現行列の作成
  for (int i = 0; i < M; i++) {
    int A, B;
    scanf("%d", &A);
    scanf("%d", &B);
    Map[A - 1][B - 1] = 1;
    Map[B - 1][A - 1] = 1;
  }
  Node *node = (Node *)malloc(sizeof(Node) * N);
  int *room_list = (int *)malloc(sizeof(int) * N);
  int idx = 0;
  queue<int> que;
  node[0].parent = 0;
  room_list[0] = 1;
  que.push(0);
  // node[0]から幅優先で全ノードの親と子をセットする
  while (!que.empty()) {
    idx = que.front();
    que.pop();
    set_child(node, idx, Map_ptr[idx], N, room_list, que);
    // Node nodes[], int node_idx, int* Map_ptr[], int N, int list[],
    // queue<int>& que)
  }
  int check = 0;
  for (int i = 0; i < N; i++) {
    if (room_list[i] == 0) {
      check = 1;
      break;
    }
  }
  if (check == 1) {
    printf("No\\n");
  } else {
    printf("Yes\\n");
    for (int i = 1; i < N; i++) {
      printf("%d\\n", node[i].parent + 1);
    }
  }
  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  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);
  }

  vector<int> ans(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    auto idx = q.front();
    q.pop();

    for (int i = 0; i < g[idx].size(); ++i) {
      if (ans[g[idx][i]] != -1 || g[idx][i] == 0) {
        continue;
      }

      ans[g[idx][i]] = idx;
      q.push(g[idx][i]);
    }
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  int n, m;
  cin >> n >> m;

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

  vector<int> ans(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    auto idx = q.front();
    q.pop();

    for (int i = 0; i < g[idx].size(); ++i) {
      if (ans[g[idx][i]] != -1 || g[idx][i] == 0) {
        continue;
      }

      ans[g[idx][i]] = idx;
      q.push(g[idx][i]);
    }
  }

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

  return 0;
}