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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<vector<int>> Graph;
Graph G(100010);
vector<int> par(100010);
int room[10010] = {};
void init(int N) { // 最初は全てが根であるとして初期化
  for (int i = 0; i < N; i++)
    par[i] = i;
}

int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  if (par[x] == x)
    return x;
  return par[x] = root(par[x]);
}

void unite(int x, int y) { // xとyの木を併合
  int rx = root(x);        // xの根をrx
  int ry = root(y);        // yの根をry
  if (rx == ry)
    return; // xとyの根が同じ(=同じ木にある)時はそのまま
  if (ry < rx) {
    par[rx] = ry;
  } else {
    par[ry] = rx;
  }
}

bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
  int rx = root(x);
  int ry = root(y);
  return rx == ry;
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  init(n + 1);
  rep(i, m) {
    cin >> a >> b;
    unite(a, b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 2; i <= n; i++) {
    if (!same(1, i)) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    rep(i, G[c].size()) {
      if (room[G[c][i]] == 0) {
        room[G[c][i]] = c;
        q.push(G[c][i]);
      }
    }
  }
  for (int i = 2; i <= n; i++)
    cout << room[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<vector<int>> Graph;
Graph G(100010);
vector<int> par(100010);
int room[100010] = {};
void init(int N) { // 最初は全てが根であるとして初期化
  for (int i = 0; i < N; i++)
    par[i] = i;
}

int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  if (par[x] == x)
    return x;
  return par[x] = root(par[x]);
}

void unite(int x, int y) { // xとyの木を併合
  int rx = root(x);        // xの根をrx
  int ry = root(y);        // yの根をry
  if (rx == ry)
    return; // xとyの根が同じ(=同じ木にある)時はそのまま
  if (ry < rx) {
    par[rx] = ry;
  } else {
    par[ry] = rx;
  }
}

bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
  int rx = root(x);
  int ry = root(y);
  return rx == ry;
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  init(n + 1);
  rep(i, m) {
    cin >> a >> b;
    unite(a, b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 2; i <= n; i++) {
    if (!same(1, i)) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    rep(i, G[c].size()) {
      if (room[G[c][i]] == 0) {
        room[G[c][i]] = c;
        q.push(G[c][i]);
      }
    }
  }
  for (int i = 2; i <= n; i++)
    cout << room[i] << endl;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+9
#define MAX_V 10

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int y[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < (int)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        y[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  for (int i = 0; i < E; ++i) {
    int a, b;
    cin >> a >> b;
    edge e = {b - 1, 1};
    G[a - 1].push_back(e);
    e = {a - 1, 1};

    G[b - 1].push_back(e);
  }
  dijkstra(0);
  cout << "Yes" << endl;

  for (int i = 1; i < V; ++i) {
    if (d[i] != INF)
      cout << y[i] + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+9
#define MAX_V 100000

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int y[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < (int)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        y[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  for (int i = 0; i < E; ++i) {
    int a, b;
    cin >> a >> b;
    edge e = {b - 1, 1};
    G[a - 1].push_back(e);
    e = {a - 1, 1};

    G[b - 1].push_back(e);
  }
  dijkstra(0);
  cout << "Yes" << endl;

  for (int i = 1; i < V; ++i) {
    if (d[i] != INF)
      cout << y[i] + 1 << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define TRAV(i, a) for (auto &i : (a))
#define SZ(x) ((int)(x).size())
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  std::cin >> n;
  int m;
  std::cin >> m;
  std::vector<VI> g(n);
  FOR(i, n) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  std::queue<int> que;
  que.push(0);
  VI prev(n, -1);
  prev[0] = 0;
  while (SZ(que)) {
    int cur = que.front();
    que.pop();
    TRAV(ch, g[cur]) {
      if (prev[ch] == -1) {
        prev[ch] = cur;
        que.push(ch);
      }
    }
  }

  std::cout << "Yes\n";
  REP(i, 1, n) std::cout << prev[i] + 1 << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define TRAV(i, a) for (auto &i : (a))
#define SZ(x) ((int)(x).size())
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  std::cin >> n;
  int m;
  std::cin >> m;
  std::vector<VI> g(n);
  FOR(i, m) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  std::queue<int> que;
  que.push(0);
  VI prev(n, -1);
  prev[0] = 0;
  while (SZ(que)) {
    int cur = que.front();
    que.pop();
    TRAV(ch, g[cur]) {
      if (prev[ch] == -1) {
        prev[ch] = cur;
        que.push(ch);
      }
    }
  }

  std::cout << "Yes\n";
  REP(i, 1, n) std::cout << prev[i] + 1 << "\n";

  return 0;
}
Problem p02678 (C++)

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

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(M);
  int a, b;
  REP(i, M) {
    cin >> a >> b;
    --a;
    --b;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
  }

  vector<bool> visit(N, false);
  queue<int> q;
  unordered_map<int, int> route;

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

    for (int nv : G.at(v)) {
      if (!visit.at(nv)) {
        q.push(nv);
        visit.at(nv) = true;
        route.insert(make_pair(nv, v));
      }
    }
  }

  if (!all_of(visit.begin(), visit.end(), [](bool f) { return f; })) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  REPI(i, 1, N) {
    // cout << i << ":" << route.at(i).size() << endl;
    cout << route.at(i) + 1 << endl;
  }

  return 0;
}


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

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  int a, b;
  REP(i, M) {
    cin >> a >> b;
    --a;
    --b;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
  }

  vector<bool> visit(N, false);
  queue<int> q;
  unordered_map<int, int> route;

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

    for (int nv : G.at(v)) {
      if (!visit.at(nv)) {
        q.push(nv);
        visit.at(nv) = true;
        route.insert(make_pair(nv, v));
      }
    }
  }

  if (!all_of(visit.begin(), visit.end(), [](bool f) { return f; })) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  REPI(i, 1, N) {
    // cout << i << ":" << route.at(i).size() << endl;
    cout << route.at(i) + 1 << endl;
  }

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

void search(vector<vector<int>> g, vector<int> &nxt, queue<int> &q) {
  int tar = q.front();
  q.pop();
  for (int s : g[tar]) {
    if (nxt[s] == -1) {
      q.push(s);
      nxt[s] = tar;
    }
  }
  if (q.size() < 1)
    return;
  else
    search(g, nxt, q);
}

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(0));
  int n1, n2;
  for (int i = 0; i < m; i++) {
    cin >> n1 >> n2;
    g[n1 - 1].push_back(n2 - 1);
    g[n2 - 1].push_back(n1 - 1);
  }

  vector<int> nxt(n, -1);
  nxt[0] = 0;
  queue<int> q;
  q.push(0);
  search(g, nxt, q);
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << nxt[i] + 1 << endl;
  }
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

void search(vector<vector<int>> &g, vector<int> &nxt, queue<int> &q) {
  int tar = q.front();
  q.pop();
  for (int s : g[tar]) {
    if (nxt[s] == -1) {
      q.push(s);
      nxt[s] = tar;
    }
  }
  if (q.size() < 1)
    return;
  else
    search(g, nxt, q);
}

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(0));
  int n1, n2;
  for (int i = 0; i < m; i++) {
    cin >> n1 >> n2;
    g[n1 - 1].push_back(n2 - 1);
    g[n2 - 1].push_back(n1 - 1);
  }

  vector<int> nxt(n, -1);
  nxt[0] = 0;
  queue<int> q;
  q.push(0);
  search(g, nxt, q);
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << nxt[i] + 1 << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
using ll = long long;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> G(M, vector<ll>(0));
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  queue<ll> q;
  q.push(0);
  vector<ll> ans(N, -1);
  vector<bool> gone(N, false);
  gone[0] = true;
  while (!q.empty()) {
    ll x = q.front();
    q.pop();
    if (!G[x].empty()) {
      for (auto g : G[x]) {
        if (gone[g])
          continue;
        gone[g] = true;
        ans[g] = x + 1;
        q.push(g);
      }
    }
  }
  bool can = true;
  for (ll i = 1; i < N; i++) {
    if (ans[i] == -1) {
      can = false;
      break;
    }
  }
  if (can) {
    cout << "Yes" << endl;
    for (ll i = 1; i < N; i++) {
      cout << ans[i] << endl;
    }
  } else
    cout << "No" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
using ll = long long;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> G(N, vector<ll>(0));
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  queue<ll> q;
  q.push(0);
  vector<ll> ans(N, -1);
  vector<bool> gone(N, false);
  gone[0] = true;
  while (!q.empty()) {
    ll x = q.front();
    q.pop();
    if (!G[x].empty()) {
      for (auto g : G[x]) {
        if (gone[g])
          continue;
        gone[g] = true;
        ans[g] = x + 1;
        q.push(g);
      }
    }
  }
  bool can = true;
  for (ll i = 1; i < N; i++) {
    if (ans[i] == -1) {
      can = false;
      break;
    }
  }
  if (can) {
    cout << "Yes" << endl;
    for (ll i = 1; i < N; i++) {
      cout << ans[i] << endl;
    }
  } else
    cout << "No" << endl;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define reps(i, s, e) for (long long i = s; i < e; i++)
#define repr(i, n) for (long long i = n - 1; i >= 0; i--)
#define reprs(i, s, e) for (long long i = e - 1; i >= s; i--)

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m;
  cin >> n >> m;
  ll a[m], b[m];
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  vector<ll> g[n];
  rep(i, n) {
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  ll signal[n];
  memset(signal, -1, sizeof(signal));

  queue<ll> que;
  que.push(0);

  while (!que.empty()) {
    ll p_now = que.front();
    que.pop();

    for (ll p_nxt : g[p_now]) {
      if (signal[p_nxt] != -1)
        continue;

      signal[p_nxt] = p_now;
      que.push(p_nxt);
    }
  }

  cout << "Yes" << endl;
  reps(i, 1, n) { cout << signal[i] + 1 << endl; }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define reps(i, s, e) for (long long i = s; i < e; i++)
#define repr(i, n) for (long long i = n - 1; i >= 0; i--)
#define reprs(i, s, e) for (long long i = e - 1; i >= s; i--)

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m;
  cin >> n >> m;
  ll a[m], b[m];
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  vector<ll> g[n];
  rep(i, m) {
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  ll signal[n];
  memset(signal, -1, sizeof(signal));

  queue<ll> que;
  que.push(0);

  while (!que.empty()) {
    ll p_now = que.front();
    que.pop();

    for (ll p_nxt : g[p_now]) {
      if (signal[p_nxt] != -1)
        continue;

      signal[p_nxt] = p_now;
      que.push(p_nxt);
    }
  }

  cout << "Yes" << endl;
  reps(i, 1, n) { cout << signal[i] + 1 << endl; }

  return 0;
}
Problem p02678 (C++)

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

int n;
const int full = 200006;
vector<int> adj[full];
bool vis[full];
int par[full];
int depth[full];
int ans[full];

void dfs(int i) {
  queue<int> q;
  q.push(1);
  while (q.size() > 0) {
    int x = q.front();
    vis[x] = 1;
    q.pop();
    for (int j = 0; j < adj[x].size(); j++) {
      if (vis[adj[x][j]] == 0) {
        if (ans[adj[x][j]] == 0)
          ans[adj[x][j]] = x;
        q.push(adj[x][j]);
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int i, j, k;
  int n, m;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].push_back(a);
  }
  dfs(1);
  for (i = 2; i <= n; i++) {
    if (ans[i] == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\n";
  for (i = 2; i <= n; i++) {
    cout << ans[i] << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
long long int mod = (long long int)1000000007;
using namespace std;

int n;
const int full = 200006;
vector<int> adj[full];
bool vis[full];
int par[full];
int depth[full];
int ans[full];

void dfs(int i) {
  queue<int> q;
  q.push(1);
  while (q.size() > 0) {
    int x = q.front();
    vis[x] = 1;
    q.pop();
    for (int j = 0; j < adj[x].size(); j++) {
      if (vis[adj[x][j]] == 0 && ans[adj[x][j]] == 0) {

        ans[adj[x][j]] = x;
        q.push(adj[x][j]);
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int i, j, k;
  int n, m;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].push_back(a);
  }
  dfs(1);
  for (i = 2; i <= n; i++) {
    if (ans[i] == 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\n";
  for (i = 2; i <= n; i++) {
    cout << ans[i] << "\n";
  }
}
Problem p02678 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> room(m + 1);
  vector<int> dist(n + 1, -1);
  queue<int> que;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    room[a].push_back(b);
    room[b].push_back(a);
  }
  // 部屋1から始める
  dist[1] = 0;
  que.push(1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : room[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  // for(int i=1; i<dist.size(); i++) cout << "room :" << i << ", dist :" <<
  // dist[i] << endl;
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    int minDist = 10000000;
    int minRoom;
    for (int j = 0; j < (int)room[i].size(); j++) {
      // cout << "j :" << j << ", room :" << room[i][j] <<  ", dist :" <<
      // dist[room[i][j]] << endl;
      if (dist[room[i][j]] < minDist) {
        minDist = dist[room[i][j]];
        minRoom = room[i][j];
      }
    }
    cout << minRoom << endl;
  }
}


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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> room(m + 2);
  vector<int> dist(n + 1, -1);
  queue<int> que;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    room[a].push_back(b);
    room[b].push_back(a);
  }
  // 部屋1から始める
  dist[1] = 0;
  que.push(1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : room[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  // for(int i=1; i<dist.size(); i++) cout << "room :" << i << ", dist :" <<
  // dist[i] << endl;
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    int minDist = 10000000;
    int minRoom;
    for (int j = 0; j < (int)room[i].size(); j++) {
      // cout << "j :" << j << ", room :" << room[i][j] <<  ", dist :" <<
      // dist[room[i][j]] << endl;
      if (dist[room[i][j]] < minDist) {
        minDist = dist[room[i][j]];
        minRoom = room[i][j];
      }
    }
    cout << minRoom << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/*
void showqueue(queue<int> q) {
        cout << "queue: " ;
        while (!q.empty()) {
                cout << q.front() << " " ;
                q.pop();
        }
        cout << endl;
        return;
}
*/

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

  vector<queue<int>> road(M);
  vector<int> output(N, -1);

  for (int i = 0; i < M; i++) {
    int temp_A, temp_B;
    cin >> temp_A >> temp_B;
    temp_A--;
    temp_B--;
    road[temp_A].push(temp_B);
    road[temp_B].push(temp_A);
  }

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

  while (!room_queue.empty()) {
    int temp_room = room_queue.front();
    queue<int> temp_room_queue = road[temp_room];
    while (!temp_room_queue.empty()) {
      int temp_room_new = temp_room_queue.front();
      if (output[temp_room_new] == -1) {
        output[temp_room_new] = temp_room;
        room_queue.push(temp_room_new);
        // cout << temp_room_new << " " << num << endl;
      }
      temp_room_queue.pop();
    }
    room_queue.pop();
  }

  // output
  // cout << endl;

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/*
void showqueue(queue<int> q) {
        cout << "queue: " ;
        while (!q.empty()) {
                cout << q.front() << " " ;
                q.pop();
        }
        cout << endl;
        return;
}
*/

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

  vector<queue<int>> road(N);
  vector<int> output(N, -1);

  for (int i = 0; i < M; i++) {
    int temp_A, temp_B;
    cin >> temp_A >> temp_B;
    temp_A--;
    temp_B--;
    road[temp_A].push(temp_B);
    road[temp_B].push(temp_A);
  }

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

  while (!room_queue.empty()) {
    int temp_room = room_queue.front();
    queue<int> temp_room_queue = road[temp_room];
    while (!temp_room_queue.empty()) {
      int temp_room_new = temp_room_queue.front();
      if (output[temp_room_new] == -1) {
        output[temp_room_new] = temp_room;
        room_queue.push(temp_room_new);
        // cout << temp_room_new << " " << num << endl;
      }
      temp_room_queue.pop();
    }
    room_queue.pop();
  }

  // output
  // cout << endl;

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using std::cin;
using std::cout;
using std::vector;

using ll = long long;
using ull = unsigned long long;

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

  ull n, m, a, b;
  std::cin >> n >> m;

  vector<ull> from(n);
  vector<vector<ull>> edge(m);
  for (ull i = 0; i < m; i++) {
    cin >> a >> b;
    edge[a - 1].push_back(b - 1);
    edge[b - 1].push_back(a - 1);
  }

  // bfs
  std::queue<ull> q;
  vector<bool> done(n, false);
  q.push(0);
  done[0] = true;
  while (!q.empty()) {
    ull node = q.front();
    q.pop();
    for (ull next_node : edge[node]) {
      if (!done[next_node]) {
        q.push(next_node);
        from[next_node] = node;
        done[next_node] = true;
      }
    }
  }

  cout << "Yes\n";
  for (ull i = 1; i < n; i++) {
    cout << from[i] + 1 << "\n";
  }

  return 0;
}

Fixed submission:
#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using std::cin;
using std::cout;
using std::vector;

using ll = long long;
using ull = unsigned long long;

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

  ull n, m, a, b;
  std::cin >> n >> m;

  vector<ull> from(n);
  vector<vector<ull>> edge(n);
  for (ull i = 0; i < m; i++) {
    cin >> a >> b;
    edge[a - 1].push_back(b - 1);
    edge[b - 1].push_back(a - 1);
  }

  // bfs
  std::queue<ull> q;
  vector<bool> done(n, false);
  q.push(0);
  done[0] = true;
  while (!q.empty()) {
    ull node = q.front();
    q.pop();
    for (ull next_node : edge[node]) {
      if (!done[next_node]) {
        q.push(next_node);
        from[next_node] = node;
        done[next_node] = true;
      }
    }
  }

  cout << "Yes\n";
  for (ull i = 1; i < n; i++) {
    cout << from[i] + 1 << "\n";
  }

  return 0;
}
Problem p02678 (C++)

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

const double pi = 3.141592653589793;
#define YESNO(x) x ? printf("YES") : printf("NO")
#define YesNo(x) x ? printf("Yes") : printf("No")
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define scani(x) scanf("%d", &x)
#define scanll(x) scanf("%lld", &x)
#define printi(x) printf("%d", x)
#define printll(x) printf("%lld", x)
typedef long long ll;
using namespace std;

long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }

long long LCM(long long x, long long y) { return x * y / GCD(x, y); }

// give up(TLE)
int ABC168D() {
  int n, m;
  scani(n);
  scani(m);

  vector<int> graph[10001];
  int ans[10001];
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  vector<bool> visit(n, false);
  visit[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();

    for (auto &itr : graph[cur]) {
      if (!visit[itr]) {
        ans[itr] = cur;
        visit[itr] = true;
        q.push(itr);
      }
    }
  }

  bool isOk = true;
  for (auto itr : visit) {
    if (!itr) {
      YesNo(0);
      return 0;
    }
  }

  YesNo(1);
  printf("\n");
  rep(i, 1, n) { printf("%d\n", ans[i] + 1); }

  return 0;
}

int main() {
  ABC168D();
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

const double pi = 3.141592653589793;
#define YESNO(x) x ? printf("YES") : printf("NO")
#define YesNo(x) x ? printf("Yes") : printf("No")
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define scani(x) scanf("%d", &x)
#define scanll(x) scanf("%lld", &x)
#define printi(x) printf("%d", x)
#define printll(x) printf("%lld", x)
typedef long long ll;
using namespace std;

long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }

long long LCM(long long x, long long y) { return x * y / GCD(x, y); }

// give up(TLE)
int ABC168D() {
  int n, m;
  scani(n);
  scani(m);

  vector<int> graph[101010];
  int ans[101010];
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  vector<bool> visit(n, false);
  visit[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();

    for (auto &itr : graph[cur]) {
      if (!visit[itr]) {
        ans[itr] = cur;
        visit[itr] = true;
        q.push(itr);
      }
    }
  }

  bool isOk = true;
  for (auto itr : visit) {
    if (!itr) {
      YesNo(0);
      return 0;
    }
  }

  YesNo(1);
  printf("\n");
  rep(i, 1, n) { printf("%d\n", ans[i] + 1); }

  return 0;
}

int main() {
  ABC168D();
  return 0;
}
Problem p02678 (C++)

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

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

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

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

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;

  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 (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> e[11000];
  int ans[11000];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    for (int d : e[c]) {
      if (d == c)
        continue;
      if (ans[d] != 0)
        continue;
      ans[d] = c;
      q.push(d);
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> e[110000];
  int ans[110000];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    for (int d : e[c]) {
      if (d == c)
        continue;
      if (ans[d] != 0)
        continue;
      ans[d] = c;
      q.push(d);
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb push_back;
#define sp " ";
void vout(vector<int> v) {
  for (unsigned int i = 1; i < v.size(); i++)
    cout << v.at(i) + 1 << endl;
}
int main() {
  int n, m;
  cin >> n >> m;
  vvi edge(n, vi(n));
  vi ans(n, -1);
  rep(i, m) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    edge.at(s).push_back(t);
    edge.at(t).push_back(s);
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (int j : edge.at(k)) {
      if (ans.at(j) == -1) {
        ans.at(j) = k;
        q.push(j);
      }
    }
  }
  cout << "Yes" << endl;
  vout(ans);
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb push_back;
#define sp " ";
void vout(vector<int> v) {
  for (unsigned int i = 1; i < v.size(); i++)
    cout << v.at(i) + 1 << endl;
}
int main() {
  int n, m;
  cin >> n >> m;
  vvi edge(n);
  vi ans(n, -1);
  rep(i, m) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    edge.at(s).push_back(t);
    edge.at(t).push_back(s);
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (int j : edge.at(k)) {
      if (ans.at(j) == -1) {
        ans.at(j) = k;
        q.push(j);
      }
    }
  }
  cout << "Yes" << endl;
  vout(ans);
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define REP(i, n) for (decltype(n) i = 0; i < (n); ++i)
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> adj(N);
  REP(i, N) { adj[i].reserve(M); }
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  deque<tuple<int, int, int>> q;
  q.emplace_back(0, 0, -1);
  int INF = N + 10;
  vector<int> dist(N, INF);
  vector<int> prev(N, -1);
  int cnt = 0;
  while (!q.empty()) {
    auto [l, node, pnode] = q.front();
    q.pop_front();
    if (dist[node] < l)
      continue;
    dist[node] = l;
    prev[node] = pnode;
    ++l;
    for (auto &&nxt : adj[node]) {
      if (dist[nxt] > l) {
        q.emplace_back(l, nxt, node);
      }
    }
  }
  cout << "Yes\n";
  for (int i = 1; i < N; ++i) {
    cout << (prev[i] + 1) << "\n";
  }
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define REP(i, n) for (decltype(n) i = 0; i < (n); ++i)
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> adj(N);
  REP(i, N) { adj[i].reserve(M); }
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  deque<tuple<int, int, int>> q;
  q.emplace_back(0, 0, -1);
  int INF = N + 10;
  vector<int> dist(N, INF);
  vector<int> prev(N, -1);
  int cnt = 0;
  while (!q.empty()) {
    auto [l, node, pnode] = q.front();
    q.pop_front();
    if (dist[node] <= l)
      continue;
    dist[node] = l;
    prev[node] = pnode;
    ++l;
    for (auto &&nxt : adj[node]) {
      if (dist[nxt] > l) {
        q.emplace_back(l, nxt, node);
      }
    }
  }
  cout << "Yes\n";
  for (int i = 1; i < N; ++i) {
    cout << (prev[i] + 1) << "\n";
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
double const pi = acos(double(-1));
const int maxn = 2e5 + 100;
using namespace std;
typedef long long ll;
struct node {
  ll to;
  ll next;
} edge[maxn + 5];
ll n, m, ans;
ll head[maxn + 5];
void addedge(ll u, ll v) {
  edge[ans].to = v;
  edge[ans].next = head[u];
  head[u] = ans++;
}
void init() {
  memset(head, -1, sizeof(head));
  ans = 0;
}
ll cnt = 0;
ll num[maxn + 5];
int vis[maxn + 5];
void bfs(int u) {
  queue<int> q;
  vis[u] = 1;
  q.push(u);
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    // printf("%d ",k);
    for (int i = head[k]; ~i; i = edge[i].next) {
      int to = edge[i].to;
      if (vis[to] == -1) {
        q.push(to);
        vis[to] = k;
        cnt++;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init();
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    addedge(u, v);
    addedge(v, u);
  }
  memset(vis, -1, sizeof(vis));
  bfs(1);
  // printf("\n");
  if (cnt != n - 1) {
    cout << "No";
    return 0;
  } else
    cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << vis[i] << endl;
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
double const pi = acos(double(-1));
const int maxn = 4e5 + 100;
using namespace std;
typedef long long ll;
struct node {
  ll to;
  ll next;
} edge[maxn + 5];
ll n, m, ans;
ll head[maxn + 5];
void addedge(ll u, ll v) {
  edge[ans].to = v;
  edge[ans].next = head[u];
  head[u] = ans++;
}
void init() {
  memset(head, -1, sizeof(head));
  ans = 0;
}
ll cnt = 0;
ll num[maxn + 5];
int vis[maxn + 5];
void bfs(int u) {
  queue<int> q;
  vis[u] = 1;
  q.push(u);
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    // printf("%d ",k);
    for (int i = head[k]; ~i; i = edge[i].next) {
      int to = edge[i].to;
      if (vis[to] == -1) {
        q.push(to);
        vis[to] = k;
        cnt++;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init();
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    addedge(u, v);
    addedge(v, u);
  }
  memset(vis, -1, sizeof(vis));
  bfs(1);
  // printf("\n");
  if (cnt != n - 1) {
    cout << "No";
    return 0;
  } else
    cout << "Yes" << endl;
  for (int i = 2; i <= n; i++)
    cout << vis[i] << endl;
  cout << endl;
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pi 3.1415
#define ll long long
#define pii pair<ll, ll>
#define debug(a) cout << a << '\n'
#define maxn 50009
#define MOD 1000000007
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
using namespace std;
const ll INF = 1e15 + 9;
ll n, m;
vector<pii> adj[maxn];
ll dis[maxn];
ll pai[maxn];
void addedge(ll a, ll b, ll c) {
  adj[a].push_back({b, c});
  adj[b].push_back({a, c});
}
void INFINITAR() {
  for (ll i = 0; i <= n; i++) {
    dis[i] = INF;
  }
}
void dijkstra(int x) {
  INFINITAR();
  dis[x] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push({0, x});
  while (!pq.empty()) {
    auto f = pq.top();
    pq.pop();
    if (dis[f.second] < f.first)
      continue;
    for (auto e : adj[f.second]) {
      if (f.first + e.second < dis[e.first]) {
        pai[e.first] = f.second;
        dis[e.first] = f.first + e.second;
        pq.push({dis[e.first], e.first});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  memset(pai, -1, sizeof pai);
  while (m--) {
    ll a, b;
    cin >> a >> b;
    addedge(a, b, 1);
  }
  dijkstra(1);
  bool can = true;
  rep(i, 2, n + 1) {
    if (pai[i] == -1)
      can = false;
  }
  if (!can) {
    cout << "No\n";
  } else {
    cout << "Yes\n";
    rep(i, 2, n + 1) cout << pai[i] << '\n';
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pi 3.1415
#define ll long long
#define pii pair<ll, ll>
#define debug(a) cout << a << '\n'
#define maxn 200009
#define MOD 1000000007
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
using namespace std;
const ll INF = 1e15 + 9;
ll n, m;
vector<pii> adj[maxn];
ll dis[maxn];
ll pai[maxn];
void addedge(ll a, ll b, ll c) {
  adj[a].push_back({b, c});
  adj[b].push_back({a, c});
}
void INFINITAR() {
  for (ll i = 0; i <= n; i++) {
    dis[i] = INF;
  }
}
void dijkstra(int x) {
  INFINITAR();
  dis[x] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push({0, x});
  while (!pq.empty()) {
    auto f = pq.top();
    pq.pop();
    if (dis[f.second] < f.first)
      continue;
    for (auto e : adj[f.second]) {
      if (f.first + e.second < dis[e.first]) {
        pai[e.first] = f.second;
        dis[e.first] = f.first + e.second;
        pq.push({dis[e.first], e.first});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  memset(pai, -1, sizeof pai);
  while (m--) {
    ll a, b;
    cin >> a >> b;
    addedge(a, b, 1);
  }
  dijkstra(1);
  bool can = true;
  rep(i, 2, n + 1) {
    if (pai[i] == -1)
      can = false;
  }
  if (!can) {
    cout << "No\n";
  } else {
    cout << "Yes\n";
    rep(i, 2, n + 1) cout << pai[i] << '\n';
  }
  return 0;
}
Problem p02678 (C++)

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

using namespace std;

int mark[10005];
vector<int> m[10005];

int main() {
  int n, md;
  queue<int> q;

  cin >> n >> md;

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

  q.push(1);
  while (!q.empty()) {
    int val = q.front();
    q.pop();
    for (int i = 0; i < m[val].size(); i++) {
      int next = m[val][i];
      if (mark[next] == 0) {
        mark[next] = val;
        q.push(next);
      }
    }
  }

  bool flg = true;
  for (int i = 2; i <= n; i++) {
    if (mark[i] == 0)
      flg = false;
  }
  if (flg) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << mark[i] << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int mark[100005];
vector<int> m[100005];

int main() {
  int n, md;
  queue<int> q;

  cin >> n >> md;

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

  q.push(1);
  while (!q.empty()) {
    int val = q.front();
    q.pop();
    for (int i = 0; i < m[val].size(); i++) {
      int next = m[val][i];
      if (mark[next] == 0) {
        mark[next] = val;
        q.push(next);
      }
    }
  }

  bool flg = true;
  for (int i = 2; i <= n; i++) {
    if (mark[i] == 0)
      flg = false;
  }
  if (flg) {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++)
      cout << mark[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 main() {
  long N, M;
  cin >> N >> M;
  vector<long> data(N, INT_MAX);
  vector<vector<long>> path(N, vector<long>(N));
  long a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    path.at(a - 1).push_back(b - 1);
    path.at(b - 1).push_back(a - 1);
  }
  queue<long> que;
  que.push(0);
  long size, sub;
  while (que.size() != 0) {
    size = que.size();
    for (int i = 0; i < size; i++) {
      sub = que.front();
      que.pop();
      for (unsigned int j = 0; j < path.at(sub).size(); j++) {
        if (data.at(path.at(sub).at(j)) == INT_MAX) {
          data.at(path.at(sub).at(j)) = sub;
          que.push(path.at(sub).at(j));
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << data.at(i) + 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, M;
  cin >> N >> M;
  vector<long> data(N, INT_MAX);
  vector<vector<long>> path(N, vector<long>());
  long a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    path.at(a - 1).push_back(b - 1);
    path.at(b - 1).push_back(a - 1);
  }
  queue<long> que;
  que.push(0);
  long size, sub;
  while (que.size() != 0) {
    size = que.size();
    for (int i = 0; i < size; i++) {
      sub = que.front();
      que.pop();
      for (unsigned int j = 0; j < path.at(sub).size(); j++) {
        if (data.at(path.at(sub).at(j)) == INT_MAX) {
          data.at(path.at(sub).at(j)) = sub;
          que.push(path.at(sub).at(j));
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << data.at(i) + 1 << endl;
  return 0;
}