prithivMLmods/Coder-Stat huggingface dataset:
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
string explanation1 = "これは幅優先探索の問題！";
string explanation2 =
    "参考URL:https://qiita.com/drken/items/996d80bcae64649a6580";

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;

  // グラフ入力受け取り
  Graph G(m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;               // distで０と１のズレを直しておくため
    G[a].push_back(b); // 有向グラフ
    G[b].push_back(a); // 無向グラフなのでこれも追加
  }

  // BFSのためのデータ構造
  vector<int> dist(n, INF); // 全頂点を「未訪問」に初期化
  queue<int> que;           // キュー
  vector<int> pre(n, -1);

  // 初期条件（頂点1をノードとする）
  dist[0] = 0;
  que.push(0); // 1を橙色頂点にする

  // BFS開始（キューが空になるまで探索を行う）
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();           // 先頭要素を削除

    // v から辿れる頂点をすべて調べる
    for (int u : G[v]) { // 拡張for文 変数uにG[v]の値を入れてる
      if (dist[u] != INF)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点uについて距離情報を更新してキューに追加する
      dist[u] = dist[v] + 1;
      pre[u] = v;
      que.push(u);
    }
  }

  for (int i = 1; i < n; i++) {
    if (pre[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++; // 18,19行目で１減らしてたのを元に戻す
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
string explanation1 = "これは幅優先探索の問題！";
string explanation2 =
    "参考URL:https://qiita.com/drken/items/996d80bcae64649a6580";

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;

  // グラフ入力受け取り
  Graph G(100005);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;               // distで０と１のズレを直しておくため
    G[a].push_back(b); // 有向グラフ
    G[b].push_back(a); // 無向グラフなのでこれも追加
  }

  // BFSのためのデータ構造
  vector<int> dist(n, INF); // 全頂点を「未訪問」に初期化
  queue<int> que;           // キュー
  vector<int> pre(n, -1);

  // 初期条件（頂点1をノードとする）
  dist[0] = 0;
  que.push(0); // 1を橙色頂点にする

  // BFS開始（キューが空になるまで探索を行う）
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();           // 先頭要素を削除

    // v から辿れる頂点をすべて調べる
    for (int u : G[v]) { // 拡張for文 変数uにG[v]の値を入れてる
      if (dist[u] != INF)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点uについて距離情報を更新してキューに追加する
      dist[u] = dist[v] + 1;
      pre[u] = v;
      que.push(u);
    }
  }

  for (int i = 1; i < n; i++) {
    if (pre[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ans++; // 18,19行目で１減らしてたのを元に戻す
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
const ll INF = 1e9;

using Graph = vector<vector<int>>;

int N, M;
Graph G;

int main() {
  cin >> N >> M;
  G.assign(N, vector<int>());
  rep(i, 0, N) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(N, INF);
  ans[0] = -1;
  queue<int> que;
  que.push(0);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (auto &nv : G[v]) {
      if (ans[nv] == INF) {
        que.push(nv);
        ans[nv] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (auto &v : ans) {
    if (v == -1)
      continue;
    cout << v + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
const ll INF = 1e9;

using Graph = vector<vector<int>>;

int N, M;
Graph G;

int main() {
  cin >> N >> M;
  G.assign(N, vector<int>());
  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(N, INF);
  ans[0] = -1;
  queue<int> que;
  que.push(0);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (auto &nv : G[v]) {
      if (ans[nv] == INF) {
        que.push(nv);
        ans[nv] = v;
      }
    }
  }
  cout << "Yes" << endl;
  for (auto &v : ans) {
    if (v == -1)
      continue;
    cout << v + 1 << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

int N, M;
vector<vector<int>> G;
vector<int> ans;

void bfs() {
  queue<ll> Q;
  vector<ll> color(N, 0);
  Q.push(0);
  color[0] = 1;
  while (!Q.empty()) {
    ll u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      if (color[G[u][i]] == 0) {
        Q.push(G[u][i]);
        color[G[u][i]] = 1;
        ans[G[u][i]] = u;
      }
    }
    // cout << u << endl;
  }
  for (int i = 0; i < N; i++) {
    if (color[i] == 0) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] + 1 << endl;
}

signed main() {
  cin >> N >> M;
  G.resize(N);
  ans.resize(N);
  for (int i = 0; i < N; i++)
    G[i].resize(N);
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  // cout << "OK" << endl;
  bfs();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

int N, M;
vector<vector<int>> G;
vector<int> ans;

void bfs() {
  queue<ll> Q;
  vector<ll> color(N, 0);
  Q.push(0);
  color[0] = 1;
  while (!Q.empty()) {
    ll u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      if (color[G[u][i]] == 0) {
        Q.push(G[u][i]);
        color[G[u][i]] = 1;
        ans[G[u][i]] = u;
      }
    }
    // cout << u << endl;
  }
  for (int i = 0; i < N; i++) {
    if (color[i] == 0) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] + 1 << endl;
}

signed main() {
  cin >> N >> M;
  G.resize(N);
  ans.resize(N);
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  // cout << "OK" << endl;
  bfs();
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int s[205000];
vector<int> v[100];
int main() {
  int n, m;
  memset(s, -1, sizeof(s));
  cin >> n >> m;
  while (m--) {
    int u, v1;
    cin >> u >> v1;
    v[u].push_back(v1);
    v[v1].push_back(u);
  }
  queue<int> que;
  s[1] = 0;
  for (int i = 0; i < v[1].size(); i++) {
    que.push(v[1][i]);
    s[v[1][i]] = 1;
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (int i = 0; i < v[p].size(); i++) {
      int g = v[p][i];
      if (s[g] == -1) {
        s[g] = p;
        que.push(g);
      }
    }
  }
  int flag = 1;
  for (int i = 2; i <= n; i++) {
    if (s[i] == -1) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << s[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int s[205000];
vector<int> v[205000];
int main() {
  int n, m;
  memset(s, -1, sizeof(s));
  cin >> n >> m;
  while (m--) {
    int u, v1;
    cin >> u >> v1;
    v[u].push_back(v1);
    v[v1].push_back(u);
  }
  queue<int> que;
  s[1] = 0;
  for (int i = 0; i < v[1].size(); i++) {
    que.push(v[1][i]);
    s[v[1][i]] = 1;
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (int i = 0; i < v[p].size(); i++) {
      int g = v[p][i];
      if (s[g] == -1) {
        s[g] = p;
        que.push(g);
      }
    }
  }
  int flag = 1;
  for (int i = 2; i <= n; i++) {
    if (s[i] == -1) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << s[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool v[100001], unvisited = false;
int ans[100001];
int dis[100001];
vector<int> adj[100001], newcurr;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  newcurr.push_back(1);
  int distance = 0;
  while (0 == 0) {
    vector<int> curr = newcurr;
    newcurr.clear();
    if (curr.size() == 0) {
      break;
    }
    for (int i = 0; i < curr.size(); i++) {
      v[curr[i]] = true;
      dis[curr[i]] = distance;
    }
    distance++;
    for (int i = 0; i < curr.size(); i++) {
      for (int j = 0; j < adj[curr[i]].size(); j++) {
        if (!v[adj[curr[i]][j]]) {
          ans[adj[curr[i]][j]] = curr[i];
          newcurr.push_back(adj[curr[i]][j]);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i])
      unvisited = true;
  }
  if (unvisited) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool v[100001], unvisited = false;
int ans[100001];
int dis[100001];
vector<int> adj[100001], newcurr;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  newcurr.push_back(1);
  int distance = 0;
  while (0 == 0) {
    vector<int> curr = newcurr;
    newcurr.clear();
    if (curr.size() == 0) {
      break;
    }
    for (int i = 0; i < curr.size(); i++) {
      v[curr[i]] = true;
      dis[curr[i]] = distance;
    }
    distance++;
    for (int i = 0; i < curr.size(); i++) {
      for (int j = 0; j < adj[curr[i]].size(); j++) {
        if (!v[adj[curr[i]][j]]) {
          ans[adj[curr[i]][j]] = curr[i];
          v[adj[curr[i]][j]] = true;
          newcurr.push_back(adj[curr[i]][j]);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i])
      unvisited = true;
  }
  if (unvisited) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << endl;
    }
  }
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int N = 100010;
vector<int> G[N];
vector<int> D(N);
vector<int> B(N);

void dfs(int now) {
  for (auto x : G[now]) {
    int to = x;
    if (D[to] > D[now] + 1) {
      D[to] = D[now] + 1;
      B[to] = now + 1;
      dfs(to);
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 0; i < N; i++) {
    D[i] = 1e9;
  }
  D[0] = 0;
  B[0] = -1;

  dfs(0);

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

  return 0;
}

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

const int N = 100010;
vector<int> G[N];
vector<int> D(N);
vector<int> B(N);

void dfs(int now) {
  for (auto x : G[now]) {
    int to = x;
    if (D[to] > D[now] + 1) {
      D[to] = D[now] + 1;
      B[to] = now + 1;
      dfs(to);
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 0; i < N; i++) {
    D[i] = 1e9;
  }
  D[0] = 0;
  B[0] = -1;

  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto x : G[now]) {
      int to = x;
      if (D[to] > D[now] + 1) {
        D[to] = D[now] + 1;
        B[to] = now + 1;
        q.push(to);
      }
    }
  }

  dfs(0);

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: 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, 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;
}

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);
  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 (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
using pl = pair<ll, ll>;
signed main() {
  ll n, m;
  cin >> n >> m;
  vvl g(n, vl(n));
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans(n);
  vector<bool> seen(n);
  seen[0] = 1;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
using pl = pair<ll, ll>;
signed main() {
  ll n, m;
  cin >> n >> m;
  vvl g(n, vl(0));
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans(n);
  vector<bool> seen(n);
  seen[0] = 1;
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: 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 < 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;
}

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 < 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;
}
Problem p02678 (C++)

Buggy submission (status: 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();
    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;
}


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();
    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;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02678 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02678 (C++)

Buggy submission (status: 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 (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;
  }
}


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 (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;
  }
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[200010];
int dis[200010];
int main() {
  int n, m;
  while (cin >> n >> m) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      dis[i] = n + 10;
    }
    int x, y;
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      a[y].push_back(x);
    }
    queue<pair<int, int>> q;
    q.push(mk(0, 1));
    pair<int, int> tmp;
    while (!q.empty()) {
      tmp = q.front();
      q.pop();
      if (dis[tmp.sc] < tmp.fs) {
        continue;
      }
      dis[tmp.sc] = tmp.fs;
      for (auto it : a[tmp.sc]) {
        q.push(mk(tmp.fs + 1, it));
      }
    }
    // for(int i=1; i<=n; ++i){
    // 	b[dis[i]].push_back(i);
    // }
    printf("Yes\n");
    for (int i = 1; i <= n; ++i) {
      for (auto it : a[i]) {
        if (dis[it] < dis[i]) {
          printf("%d\n", it);
          break;
        }
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[200010];
int dis[200010];
int main() {
  int n, m;
  while (cin >> n >> m) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      dis[i] = n + 10;
    }
    int x, y;
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      a[y].push_back(x);
    }
    queue<pair<int, int>> q;
    q.push(mk(0, 1));
    pair<int, int> tmp;
    while (!q.empty()) {
      tmp = q.front();
      q.pop();
      if (dis[tmp.sc] <= tmp.fs) {
        continue;
      }
      dis[tmp.sc] = tmp.fs;
      for (auto it : a[tmp.sc]) {
        q.push(mk(tmp.fs + 1, it));
      }
    }
    // for(int i=1; i<=n; ++i){
    // 	b[dis[i]].push_back(i);
    // }
    printf("Yes\n");
    for (int i = 1; i <= n; ++i) {
      for (auto it : a[i]) {
        if (dis[it] < dis[i]) {
          printf("%d\n", it);
          break;
        }
      }
    }
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
#define mod 200003
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
using vvb = vector<vector<bool>>;
using vb = vector<bool>;
ll f(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll t = f(a, b / 2, p);
    return t * t % p;
  }
  return a * (f(a, b - 1, p)) % p;
}

//__builtin_popcount()
signed main() {
  ll n, m;
  cin >> n >> m;
  vl g[n];
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans{n + 10};
  queue<ll> q;
  q.push(0);
  vector<bool> seen(n);
  seen[0] = 1;
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
#define mod 200003
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
using vvb = vector<vector<bool>>;
using vb = vector<bool>;
ll f(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll t = f(a, b / 2, p);
    return t * t % p;
  }
  return a * (f(a, b - 1, p)) % p;
}

//__builtin_popcount()
signed main() {
  ll n, m;
  cin >> n >> m;
  vl g[n];
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vl ans(n + 10);
  queue<ll> q;
  q.push(0);
  vector<bool> seen(n);
  seen[0] = 1;
  while (!q.empty()) {
    ll k = q.front();
    q.pop();
    for (auto c : g[k]) {
      if (seen[c])
        continue;
      seen[c] = 1;
      ans[c] = k;
      q.push(c);
    }
  }
  cout << "Yes" << endl;
  rep(i, 1, n) cout << ans[i] + 1 << endl;

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: 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 P = pair<ll, ll>;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
void YesNo(bool j) {
  cout << (j ? "Yes" : "No") << endl;
  return;
}
void yesno(bool j) {
  cout << (j ? "yes" : "no") << endl;
  return;
}
template <class Head> void pt(Head &&head) {
  cout << head << endl;
  return;
}

vector<int> to[20005];

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int u, v;
    cin >> u >> v;

    to[v].push_back(u);
    to[u].push_back(v);
  }
  vector<int> c(n + 1, inf);
  queue<int> q;
  vector<int> ans(n + 1, -1);

  c[1] = 1;
  q.push(1);
  while (!q.empty()) {
    int a = q.front();
    q.pop();

    for (int i : to[a]) {
      if (c[i] != inf)
        continue;
      c[i] = c[a] + 1;
      ans[i] = a;
      q.push(i);
    }
  }
  /* bool ok=true;
  for(int i=1 ;i<n+1;i++) {
      if(c[i]==inf) ok=false;
  }
  if(!ok) {
      pt("No");
  }
  else {
      pt("Yes");
      for(int i=2; i<=n; i++) {
          pt(ans[i]);
      }
  }*/
  pt("Yes");
  for (int i = 2; i <= n; i++) {
    pt(ans[i]);
  }
  return 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 P = pair<ll, ll>;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
void YesNo(bool j) {
  cout << (j ? "Yes" : "No") << endl;
  return;
}
void yesno(bool j) {
  cout << (j ? "yes" : "no") << endl;
  return;
}
template <class Head> void pt(Head &&head) {
  cout << head << endl;
  return;
}

vector<int> to[200005];

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int u, v;
    cin >> u >> v;

    to[v].push_back(u);
    to[u].push_back(v);
  }
  vector<int> c(n + 1, inf);
  queue<int> q;
  vector<int> ans(n + 1, -1);

  c[1] = 1;
  q.push(1);
  while (!q.empty()) {
    int a = q.front();
    q.pop();

    for (int i : to[a]) {
      if (c[i] != inf)
        continue;
      c[i] = c[a] + 1;
      ans[i] = a;
      q.push(i);
    }
  }
  /* bool ok=true;
  for(int i=1 ;i<n+1;i++) {
      if(c[i]==inf) ok=false;
  }
  if(!ok) {
      pt("No");
  }
  else {
      pt("Yes");
      for(int i=2; i<=n; i++) {
          pt(ans[i]);
      }
  }*/
  pt("Yes");
  for (int i = 2; i <= n; i++) {
    pt(ans[i]);
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

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

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

  vector<vector<int>> dp(n);
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    dp[a].push_back(b);
    dp[b].push_back(a);
  }

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

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

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

  cout << "Yes" << '\n';
  REP(i, n) {
    if (i == 0)
      continue;
    cout << pre[i] + 1 << '\n';
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

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

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

  vector<vector<int>> dp(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    dp[a].push_back(b);
    dp[b].push_back(a);
  }

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

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

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

  cout << "Yes" << '\n';
  REP(i, n) {
    if (i == 0)
      continue;
    cout << pre[i] + 1 << '\n';
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

typedef long long ll;

#define debug(x) cerr << #x << ": " << x << "\n";
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

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

  vector<vector<int>> G(M);
  rep(i, M) {
    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()) {
    int s = q.front();
    q.pop();
    rep(i, G[s].size()) {
      if (ans[G[s][i]] == -1) {
        ans[G[s][i]] = s;
        q.push(G[s][i]);
      }
    }
  }

  bool ok = true;
  rep(i, N - 1) if (ans[i + 1] == -1) ok = false;

  if (ok) {
    cout << "Yes" << endl;
    rep(i, N - 1) cout << ans[i + 1] + 1 << endl;
  } else
    cout << "No" << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

typedef long long ll;

#define debug(x) cerr << #x << ": " << x << "\n";
#define rep(i, n) for (int i = 0; i < n; i++)

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].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(N, -1);

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int s = q.front();
    q.pop();
    rep(i, G[s].size()) {
      if (ans[G[s][i]] == -1) {
        ans[G[s][i]] = s;
        q.push(G[s][i]);
      }
    }
  }

  bool ok = true;
  rep(i, N - 1) if (ans[i + 1] == -1) ok = false;

  if (ok) {
    cout << "Yes" << endl;
    rep(i, N - 1) cout << ans[i + 1] + 1 << endl;
  } else
    cout << "No" << endl;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

  int N, M;
  scanf("%d %d", &N, &M);

  vector<vector<pair<int, int>>> w(N + 1,
                                   vector<pair<int, int>>()); //(部屋,道番号)
  vector<pair<int, int>> result(N + 1, make_pair(N + 100, -1)); //(距離, 道)

  int a, b;
  for (int i = 1; i < M + 1; i++) {
    scanf("%d %d", &a, &b);
    w[a].insert(w[a].end(), make_pair(b, i));
    w[b].insert(w[b].end(), make_pair(a, i));
  }

  queue<int> que;

  que.push(1);
  result[1].first = 0;
  result[1].second = 0;
  while (!que.empty()) {
    int n = que.front();
    // std::cout << n << "\n";
    que.pop(); // 先頭要素を削除
    for (int i = 0; i < w[n].size(); i++) {
      pair<int, int> now = w[n][i];
      // cout << "now is w[" << n << "][" << i << "]=" << now.first << " " <<
      // now.second << "\n";
      if (result[now.first].first <= result[n].first) {
        continue;
      }
      result[now.first].first = result[n].first + 1;
      result[now.first].second = n;
      que.push(now.first);
      // cout << "hoge";
    }
  }

  cout << "Yes\n";

  for (int i = 2; i <= N; i++) {
    cout << result[i].second << "\n";
  }
  return 0;
}

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

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

  int N, M;
  scanf("%d %d", &N, &M);

  vector<vector<pair<int, int>>> w(N + 1,
                                   vector<pair<int, int>>()); //(部屋,道番号)
  vector<pair<int, int>> result(N + 1, make_pair(N + 100, -1)); //(距離, 道)

  int a, b;
  for (int i = 1; i < M + 1; i++) {
    scanf("%d %d", &a, &b);
    w[a].insert(w[a].end(), make_pair(b, i));
    w[b].insert(w[b].end(), make_pair(a, i));
  }

  queue<int> que;

  que.push(1);
  result[1].first = 0;
  result[1].second = 0;
  while (!que.empty()) {
    int n = que.front();
    // std::cout << n << "\n";
    que.pop(); // 先頭要素を削除
    for (int i = 0; i < w[n].size(); i++) {
      pair<int, int> now = w[n][i];
      // cout << "now is w[" << n << "][" << i << "]=" << now.first << " " <<
      // now.second << "\n";
      if (result[now.first].first <= result[n].first + 1) {
        continue;
      }
      result[now.first].first = result[n].first + 1;
      result[now.first].second = n;
      que.push(now.first);
      // cout << "hoge";
    }
  }

  cout << "Yes\n";

  for (int i = 2; i <= N; i++) {
    cout << result[i].second << "\n";
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> path(M + 1);
  for (int i = 1; i <= M; i++) {
    int a, b;
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }

  vector<int> dist(N + 1, INT_MAX);
  vector<int> prev(N + 1, INT_MAX);
  dist[1] = 0;

  queue<int> que;
  que.push(1);

  while (!que.empty()) {
    int n = que.front();
    que.pop();
    for (size_t i = 0; i < path[n].size(); i++) {
      if (dist[path[n][i]] != INT_MAX) {
        continue;
      }
      dist[path[n][i]] = dist[n] + 1;
      prev[path[n][i]] = n;
      que.push(path[n][i]);
    }
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> path(N + 1);
  for (int i = 1; i <= M; i++) {
    int a, b;
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }

  vector<int> dist(N + 1, INT_MAX);
  vector<int> prev(N + 1, INT_MAX);
  dist[1] = 0;

  queue<int> que;
  que.push(1);

  while (!que.empty()) {
    int n = que.front();
    que.pop();
    for (size_t i = 0; i < path[n].size(); i++) {
      if (dist[path[n][i]] != INT_MAX) {
        continue;
      }
      dist[path[n][i]] = dist[n] + 1;
      prev[path[n][i]] = n;
      que.push(path[n][i]);
    }
  }

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
int mod = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  queue<int> q;
  vector<int> used(n, -1);
  q.push(0);
  used[0] = 0;
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    for (int i = 0; i < g[a].size(); i++) {
      int to = g[a][i];
      if (used[to] == -1) {
        used[to] = a;
        q.push(to);
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (used[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << used[i] + 1 << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
int mod = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  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> q;
  vector<int> used(n, -1);
  q.push(0);
  used[0] = 0;
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    for (int i = 0; i < g[a].size(); i++) {
      int to = g[a][i];
      if (used[to] == -1) {
        used[to] = a;
        q.push(to);
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (used[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << used[i] + 1 << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> r(m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    r[a - 1].push_back(b - 1);
    r[b - 1].push_back(a - 1);
  }
  vector<int> d(n, -1);
  queue<int> que;
  d[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : r[v]) {
      if (d[nv] != -1)
        continue;
      d[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << d[i] + 1 << endl;
  }
  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> r(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    r[a - 1].push_back(b - 1);
    r[b - 1].push_back(a - 1);
  }
  vector<int> d(n, -1);
  queue<int> que;
  d[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : r[v]) {
      if (d[nv] != -1)
        continue;
      d[nv] = v;
      que.push(nv);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << d[i] + 1 << endl;
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;
#define N 10001
#define INFTY 100000000

int n, m;
vector<int> num[N];
bool color[N];
int d[N];

void bfs(int s) {
  queue<int> q; // 標準ライブラリのqueue 入るものは最後尾　出すものは先頭
  q.push(s);
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty()) { // qが空になるまで続ける
    u = q.front();     // uはqの先頭
    q.pop();           // qの先頭を取り出
    if (color[u] == false)
      continue;
    color[u] = false;
    for (int i = 0; i < num[u].size(); i++) {
      d[num[u][i]] = min(d[u] + 1, d[num[u][i]]);
      q.push(num[u][i]); // qの最後尾にvを追加
      // color[num[u][i]]=false;
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    color[i] = true;
    d[i] = 100000000;
  }

  for (int i = 0; i < m; i++) {
    int p, q;
    cin >> p >> q;
    num[p].push_back(q);
    num[q].push_back(p);
  }

  d[1] = 0;

  bfs(1);

  // cout<<d[num[6][1]]<<d[6]<<endl;

  // for (int i=1; i<=n; i++)cout<<d[i]<<endl;

  cout << "Yes" << endl;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < num[i].size(); j++) {
      if (d[num[i][j]] == d[i] - 1) {
        cout << num[i][j] << endl;
        break;
      }
    }
  }
}

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;
#define N 200001
#define INFTY 100000000

int n, m;
vector<int> num[N];
bool color[N];
int d[N];

void bfs(int s) {
  queue<int> q; // 標準ライブラリのqueue 入るものは最後尾　出すものは先頭
  q.push(s);
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty()) { // qが空になるまで続ける
    u = q.front();     // uはqの先頭
    q.pop();           // qの先頭を取り出
    if (color[u] == false)
      continue;
    color[u] = false;
    for (int i = 0; i < num[u].size(); i++) {
      d[num[u][i]] = min(d[u] + 1, d[num[u][i]]);
      q.push(num[u][i]); // qの最後尾にvを追加
      // color[num[u][i]]=false;
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    color[i] = true;
    d[i] = 100000000;
  }

  for (int i = 0; i < m; i++) {
    int p, q;
    cin >> p >> q;
    num[p].push_back(q);
    num[q].push_back(p);
  }

  d[1] = 0;

  bfs(1);

  // cout<<d[num[6][1]]<<d[6]<<endl;

  // for (int i=1; i<=n; i++)cout<<d[i]<<endl;

  cout << "Yes" << endl;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < num[i].size(); j++) {
      if (d[num[i][j]] == d[i] - 1) {
        cout << num[i][j] << endl;
        break;
      }
    }
  }
}