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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int nodes, edges;
  cin >> nodes >> edges;
  vector<int> adj[nodes + 1];
  vector<bool> visited(nodes + 1);
  vector<int> ans(nodes + 1);
  for (int i = 1; i <= edges; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  visited[1] = true;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int next : adj[cur]) {
      if (!visited[next]) {
        q.push(next);
        // visited[next] = true;
        ans[next] = cur;
      }
    }
  }
  cout << "Yes\n";
  for (int i = 2; i <= nodes; i++)
    cout << ans[i] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int nodes, edges;
  cin >> nodes >> edges;
  vector<int> adj[nodes + 1];
  vector<bool> visited(nodes + 1);
  vector<int> ans(nodes + 1);
  for (int i = 1; i <= edges; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  queue<int> q;
  q.push(1);
  ans[1] = 1;
  visited[1] = true;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int next : adj[cur]) {
      if (!visited[next]) {
        q.push(next);
        visited[next] = true;
        ans[next] = cur;
      }
    }
  }
  cout << "Yes\n";
  for (int i = 2; i <= nodes; i++)
    cout << ans[i] << "\n";
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

typedef vector<vector<int>> Graph;
queue<int> que;
vector<int> par; // 各ノードの親
void bfs(int s, Graph &g) {
  que = queue<int>();
  par = vector<int>(g.size(), -1);
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : g[v]) {
      if (par[nv] != -1) { // 訪問済み
        continue;
      } else {
        par[nv] = v; // 親ノードを保存
        que.push(nv);
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout.precision(20);

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

  bfs(0, v);

  cout << "Yes" << endl;
  REP(i, n - 1) { cout << par[i + 1] + 1 << endl; }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

typedef vector<vector<int>> Graph;
queue<int> que;
vector<int> par; // 各ノードの親
void bfs(int s, Graph &g) {
  que = queue<int>();
  par = vector<int>(g.size(), -1);
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : g[v]) {
      if (par[nv] != -1) { // 訪問済み
        continue;
      } else {
        par[nv] = v; // 親ノードを保存
        que.push(nv);
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout.precision(20);

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

  bfs(0, v);

  cout << "Yes" << endl;
  REP(i, n - 1) { cout << par[i + 1] + 1 << endl; }

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
#define int int64_t

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int N, M;
vector<vector<int>> AL;
vector<int> D;
vector<int> ans;
const int INF = 1e9;

/* ======================== */
void getInput() {
  cin >> N >> M;
  AL = vector<vector<int>>(N, vector<int>());
  D = vector<int>(N, INF);
  ans = vector<int>(N, -1);
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    AL[u].push_back(v);
    AL[v].push_back(u);
  }
}

void solve() {
  // auto nb = AL[0];
  // print(nb);
  // queue<int> que;
  queue<pair<int, int>> que;
  que.push({0, 0});
  D[0] = 0;
  // print(D);
  // exit(0);

  while (!que.empty()) {
    auto op = que.front();
    que.pop();
    auto n = op.first;
    auto d = op.second;
    // D[n] = d;
    auto nb = AL[n];

    for (auto nn : nb) {
      if (D[nn] <= d)
        continue;
      D[nn] = d + 1;
      que.push({nn, d + 1});
      ans[nn] = n;
    }
  }
  // print(D);
  // print(ans);
  for (int i = 1; i < N; ++i) {
    if (ans[i] == -1) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i)
    cout << ans[i] + 1 << endl;
}

signed main() {
  getInput();
  solve();
  return 0;
}

Fixed submission:


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
#define int int64_t

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int N, M;
vector<vector<int>> AL;
vector<int> D;
vector<int> ans;
const int INF = 1e9;

/* ======================== */
void getInput() {
  cin >> N >> M;
  AL = vector<vector<int>>(N, vector<int>());
  D = vector<int>(N, INF);
  ans = vector<int>(N, -1);
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    AL[u].push_back(v);
    AL[v].push_back(u);
  }
}

void solve() {
  // auto nb = AL[0];
  // print(nb);
  // queue<int> que;
  queue<pair<int, int>> que;
  que.push({0, 0});
  D[0] = 0;
  // print(D);
  // exit(0);

  while (!que.empty()) {
    auto op = que.front();
    que.pop();
    auto n = op.first;
    auto d = op.second;
    // D[n] = d;
    auto nb = AL[n];

    for (auto nn : nb) {
      if (D[nn] <= d + 1)
        continue;
      D[nn] = d + 1;
      que.push({nn, d + 1});
      ans[nn] = n;
    }
  }
  // print(D);
  // print(ans);
  for (int i = 1; i < N; ++i) {
    if (ans[i] == -1) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i)
    cout << ans[i] + 1 << endl;
}

signed main() {
  getInput();
  solve();
  return 0;
}
Problem p02678 (C++)

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

using namespace std;
const int INF = 1001001001;
vector<int> to[10005]; // 行き先

int main(void) {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    --A;
    --B; // 部屋番号を1ずらす
    to[A].push_back(B);
    to[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 u : to[v]) { // 部屋vにつながる部屋をすべて調べる
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }

  // 道しるべ記入
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
}

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

using namespace std;
const int INF = 1001001001;
vector<int> to[100005]; // 行き先

int main(void) {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    --A;
    --B; // 部屋番号を1ずらす
    to[A].push_back(B);
    to[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 u : to[v]) { // 部屋vにつながる部屋をすべて調べる
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }

  // 道しるべ記入
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M0D 998244353;
vector<int> G[100009];
int ans[100009];
queue<int> test;

void answww(int num, int sup) {
  test.pop();
  for (int j = 0; j < G[num].size(); j++) {
    if (ans[G[num][j]] == 0) {
      test.push(G[num][j]);
      ans[G[num][j]] = num;
    }
  }
  answww(test.front(), sup);
}

int main() {
  int N, M, sup = 1;
  cin >> N >> M;
  int A[M + 1], B[M + 1];
  for (int i = 0; i < N + 1; i++)
    ans[i] = 0;
  for (int i = 1; i <= M; i++)
    cin >> A[i] >> B[i];
  for (int i = 1; i <= M; i++) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  test.push(1);
  ans[1] = 1;
  answww(test.front(), sup);
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M0D 998244353;
vector<int> G[100009];
int ans[100009];
queue<int> test;

void answww(int num, int sup) {
  test.pop();
  for (int j = 0; j < G[num].size(); j++) {
    if (ans[G[num][j]] == 0) {
      test.push(G[num][j]);
      ans[G[num][j]] = num;
    }
  }
  if (test.size() > 0)
    answww(test.front(), sup);
}

int main() {
  int N, M, sup = 1;
  cin >> N >> M;
  int A[M + 1], B[M + 1];
  for (int i = 0; i < N + 1; i++)
    ans[i] = 0;
  for (int i = 1; i <= M; i++)
    cin >> A[i] >> B[i];
  for (int i = 1; i <= M; i++) {
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  test.push(1);
  ans[1] = 1;
  answww(test.front(), sup);
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02678 (C++)

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

bool vis[10005];
pair<int, int> dist[10005];
const int INF = 1e9;

void bfs(vector<vector<int>> graph) {
  dist[0].first = 0;
  vis[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : graph[v]) {
      if (!vis[u]) {
        vis[u] = true;
        dist[u].first = dist[v].first + 1;
        dist[u].second = v + 1;
        q.push(u);
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> cave(n, vector<int>());
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    cave[u - 1].push_back(v - 1);
    cave[v - 1].push_back(u - 1);
  }
  bfs(cave);
  // cout << "hmm";
  bool possible = true;
  /*for (int i = 0; i < n; ++i) {
      if (!vis[i]) {
          possible = false;
          break;
      }
  }*/
  if (possible) {
    cout << "Yes\n";
    for (int i = 1; i < n; ++i) {
      cout << dist[i].second << '\n';
    }
  }

  return 0;
}


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

bool vis[100005];
pair<int, int> dist[100005];
const int INF = 1e9;

void bfs(vector<vector<int>> graph) {
  dist[0].first = 0;
  vis[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : graph[v]) {
      if (!vis[u]) {
        vis[u] = true;
        dist[u].first = dist[v].first + 1;
        dist[u].second = v + 1;
        q.push(u);
      }
    }
  }
}

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> cave(n, vector<int>());
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    cave[u - 1].push_back(v - 1);
    cave[v - 1].push_back(u - 1);
  }
  bfs(cave);
  // cout << "hmm";
  bool possible = true;
  /*for (int i = 0; i < n; ++i) {
      if (!vis[i]) {
          possible = false;
          break;
      }
  }*/
  if (possible) {
    cout << "Yes\n";
    for (int i = 1; i < n; ++i) {
      cout << dist[i].second << '\n';
    }
  }

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

int main() {
  int n, m;
  vector<vector<int>> nodes;

  vector<int> length;
  vector<int> parents;
  scanf("%d%d", &n, &m);

  nodes.resize(n);
  length.resize(n);
  parents.resize(n);

  for (int i = 0; i < n; ++i) {
    length[i] = ((i == 0) ? 0 : 100000000);
  }

  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);

    a -= 1;
    b -= 1;

    nodes[a].push_back(b);
    nodes[b].push_back(a);
  }

  parents[0] = 0;

  queue<int> q;

  q.push(0);

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

    for (auto iter = nodes[p].begin(); iter != nodes[p].end(); iter++) {
      if (length[*iter] < length[p] + 1)
        continue;
      length[*iter] = length[p] + 1;
      parents[*iter] = p;
      q.push(*iter);
    }
  }

  printf("Yes\n");

  for (int i = 1; i < n; ++i) {
    printf("%d\n", parents[i] + 1);
  }

  return 0;
}

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

using namespace std;

int main() {
  int n, m;
  vector<vector<int>> nodes;

  vector<int> length;
  vector<int> parents;
  scanf("%d%d", &n, &m);

  nodes.resize(n);
  length.resize(n);
  parents.resize(n);

  for (int i = 0; i < n; ++i) {
    length[i] = ((i == 0) ? 0 : 100000000);
  }

  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);

    a -= 1;
    b -= 1;

    nodes[a].push_back(b);
    nodes[b].push_back(a);
  }

  parents[0] = 0;

  queue<int> q;

  q.push(0);

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

    for (auto iter = nodes[p].begin(); iter != nodes[p].end(); iter++) {
      if (length[*iter] <= length[p] + 1)
        continue;
      length[*iter] = length[p] + 1;
      parents[*iter] = p;
      q.push(*iter);
    }
  }

  printf("Yes\n");

  for (int i = 1; i < n; ++i) {
    printf("%d\n", parents[i] + 1);
  }

  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 < (int)(n); i++)
using namespace std;
using ll = long long;
using p = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<vector<int>> to(m, vector<int>());
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  // rep(i, n) cout<<to[i][0]<<endl;
  bool flg[200010] = {false};
  flg[0] = true;
  queue<int> root;
  root.push(0);
  int ans[200010];

  while (!root.empty()) {
    int v = root.front();
    // cout<<v<<endl;
    root.pop();
    for (int x : to[v]) {
      // cout<<x<<endl;
      if (flg[x])
        continue;
      root.push(x);
      ans[x] = v;
      flg[x] = true;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}

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

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<vector<int>> to(n, vector<int>());
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  // rep(i, n) cout<<to[i][0]<<endl;
  bool flg[200010] = {false};
  flg[0] = true;
  queue<int> root;
  root.push(0);
  int ans[200010];

  while (!root.empty()) {
    int v = root.front();
    // cout<<v<<endl;
    root.pop();
    for (int x : to[v]) {
      // cout<<x<<endl;
      if (flg[x])
        continue;
      root.push(x);
      ans[x] = v;
      flg[x] = true;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}
Problem p02678 (C++)

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

void bfs(vector<vector<int>> &G, vector<int> &dist, vector<int> &prev,
         int start) {
  dist[start] = 0;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int next_v : G[v]) {
      if (dist[next_v] != -1)
        continue;
      dist[next_v] = dist[v] + 1;
      prev[next_v] = v;
      q.push(next_v);
    }
  }
}

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

  vector<int> dist(N, -1), prev(N, -1);
  bfs(G, dist, prev, 0);

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

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

void bfs(vector<vector<int>> &G, vector<int> &dist, vector<int> &prev,
         int start) {
  dist[start] = 0;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int next_v : G[v]) {
      if (dist[next_v] != -1)
        continue;
      dist[next_v] = dist[v] + 1;
      prev[next_v] = v;
      q.push(next_v);
    }
  }
}

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

  vector<int> dist(N, -1), prev(N, -1);
  bfs(G, dist, prev, 0);

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << prev[i] + 1 << endl;
}
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<vector<int>> G(N, vector<int>(N));
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> ans(N);
  using P = pair<int, int>;
  queue<P> q;
  q.push(P(0, -1));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int nv : G[p.first]) {
      if (ans[nv] == 0 && nv != 0) {
        q.push(P(nv, p.first));
        ans[nv] = p.first + 1;
      }
    }
  }
  // DUMP(ans);

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


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

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

  vector<int> ans(N);
  using P = pair<int, int>;
  queue<P> q;
  q.push(P(0, -1));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int nv : G[p.first]) {
      if (ans[nv] == 0 && nv != 0) {
        q.push(P(nv, p.first));
        ans[nv] = p.first + 1;
      }
    }
  }
  // DUMP(ans);

  for (int i = 1; i < N; ++i) {
    if (ans[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; ++i)
    cout << ans[i] << endl;
}
Problem p02678 (C++)

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

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
int main() {

  long long n, m, a, b;
  cin >> n >> m;

  vector<vector<long long>> adj(n, vector<long long>());
  while (m--) {
    cin >> a >> b;
    a--;
    b--;

    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  vector<int> d(n, INT_MAX), vis(n, 0), p(n, -1);

  queue<int> q;
  q.push(0);
  vis[0] = 1;

  d[0] = 0;

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

    for (auto u : adj[v]) {
      if (!vis[u]) {
        vis[u] = 1;
        q.push(u);
        d[u] = d[v] + 1;
        p[u] = v;
      }
    }
  }

  if (count(vis.begin(), vis.end(), 0) == 0) {
    cout << "Yes" << endl;
    for (int v = 1; v < n; v++) {
      vector<int> path;
      for (int i = v; i != -1; i = p[i])
        path.push_back(i);
      cout << path[1] + 1 << endl;
    }

  }

  else
    cout << "No" << endl;

  return 0;
}

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

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
int main() {

  long long n, m, a, b;
  cin >> n >> m;

  vector<vector<long long>> adj(n, vector<long long>());
  while (m--) {
    cin >> a >> b;
    a--;
    b--;

    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  vector<int> d(n, INT_MAX), vis(n, 0), p(n, -1);

  queue<int> q;
  q.push(0);
  vis[0] = 1;

  d[0] = 0;

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

    for (auto u : adj[v]) {
      if (!vis[u]) {
        vis[u] = 1;
        q.push(u);
        d[u] = d[v] + 1;
        p[u] = v;
      }
    }
  }

  if (count(vis.begin(), vis.end(), 0) == 0) {
    cout << "Yes" << endl;
    for (int v = 1; v < n; v++)
      cout << p[v] + 1 << endl;
  }

  else
    cout << "No" << 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 = 0x3f3f3f3f;
const int N = 1e5 + 5;
struct Edge {
  int to, w, next;
} edge[N];

struct node {
  int id, d;
  node(int x, int y) {
    id = x;
    d = y;
  }
  bool operator<(const node &a) const { return d > a.d; }
};

int head[N];
int n, m, cnt;
void addedge(int u, int v, int w) {
  edge[cnt].to = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
int dis[N], pre[N];
bool done[N];
void init() {
  for (int i = 0; i < N; i++) {
    edge[i].next = -1;
    head[i] = -1;
  }
  cnt = 0;
}

void dijkstra() {
  int s = 1;
  for (int i = 1; i <= n; i++) {
    dis[i] = INF;
    done[i] = false;
  }
  dis[s] = 0;
  priority_queue<node> q;
  q.push(node(s, 0));
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (done[u.id])
      continue;
    done[u.id] = true;
    for (int i = head[u.id]; ~i; i = edge[i].next) {
      int v = edge[i].to, w = edge[i].w;
      if (done[v])
        continue;
      if (dis[v] > u.d + w) {
        dis[v] = u.d + w;
        q.push(node(v, dis[v]));
        pre[v] = u.id;
      }
    }
  }
  int f = 1;
  for (int i = 2; i <= n; i++) {
    if (dis[i] == INF) {
      puts("No\n");
      f = 0;
    }
  }
  if (f == 1) {
    printf("Yes\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\n", pre[i]);
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  init();
  while (m--) {
    int a, b, c;
    scanf("%d%d", &a, &b);
    addedge(a, b, 1);
    addedge(b, a, 1);
  }
  dijkstra();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 5e5 + 5;
struct Edge {
  int to, w, next;
} edge[N];

struct node {
  int id, d;
  node(int x, int y) {
    id = x;
    d = y;
  }
  bool operator<(const node &a) const { return d > a.d; }
};

int head[N];
int n, m, cnt;
void addedge(int u, int v, int w) {
  edge[cnt].to = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
int dis[N], pre[N];
bool done[N];
void init() {
  for (int i = 0; i < N; i++) {
    edge[i].next = -1;
    head[i] = -1;
  }
  cnt = 0;
}

void dijkstra() {
  int s = 1;
  for (int i = 1; i <= n; i++) {
    dis[i] = INF;
    done[i] = false;
  }
  dis[s] = 0;
  priority_queue<node> q;
  q.push(node(s, 0));
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (done[u.id])
      continue;
    done[u.id] = true;
    for (int i = head[u.id]; ~i; i = edge[i].next) {
      int v = edge[i].to, w = edge[i].w;
      if (done[v])
        continue;
      if (dis[v] > u.d + w) {
        dis[v] = u.d + w;
        q.push(node(v, dis[v]));
        pre[v] = u.id;
      }
    }
  }
  int f = 1;
  for (int i = 2; i <= n; i++) {
    if (dis[i] == INF) {
      puts("No\n");
      f = 0;
    }
  }
  if (f == 1) {
    printf("Yes\n");
    for (int i = 2; i <= n; i++) {
      printf("%d\n", pre[i]);
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  init();
  while (m--) {
    int a, b, c;
    scanf("%d%d", &a, &b);
    addedge(a, b, 1);
    addedge(b, a, 1);
  }
  dijkstra();

  return 0;
}
Problem p02678 (C++)

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

// ダイクストラ
// 負の重みの辺を含むグラフには適用できない
int N, M;
const int INF = 1000000000;
vector<int> p;
vector<vector<pair<int, int>>> G; // first : 頂点, second : 有向辺の重み

void dijkstra(int r) {
  // first : スタート地点からのコスト, second : 頂点
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      PQ;
  vector<int> color(N);
  vector<int> d(N, INF);
  color.at(r) = 1;
  d.at(r) = 0;
  PQ.push(make_pair(0, r));

  while (!PQ.empty()) {
    // 最短経路木に追加する候補からコスト最小の頂点 u を取り出す
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color.at(u) = 2; // 最短経路木に追加

    if (d.at(u) < f.first)
      continue;

    // u に隣接する頂点を候補に追加
    for (auto cand : G.at(u)) {
      int v = cand.first;
      if (color.at(v) == 2)
        continue;
      if (d.at(u) + cand.second < d.at(v)) {
        d.at(v) = d.at(u) + cand.second;
        PQ.push(make_pair(d.at(v), v));
        color.at(v) = 1;
        p.at(v) = u + 1;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  G.resize(M);
  p = vector<int>(N, -1);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).push_back(make_pair(b, 1));
    G.at(b).push_back(make_pair(a, 1));
  }
  dijkstra(0);
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << p.at(i) << endl;
  }
}

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

// ダイクストラ
// 負の重みの辺を含むグラフには適用できない
int N, M;
const int INF = 1000000000;
vector<int> p;
vector<vector<pair<int, int>>> G; // first : 頂点, second : 有向辺の重み

void dijkstra(int r) {
  // first : スタート地点からのコスト, second : 頂点
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      PQ;
  vector<int> color(N);
  vector<int> d(N, INF);
  color.at(r) = 1;
  d.at(r) = 0;
  PQ.push(make_pair(0, r));

  while (!PQ.empty()) {
    // 最短経路木に追加する候補からコスト最小の頂点 u を取り出す
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color.at(u) = 2; // 最短経路木に追加

    if (d.at(u) < f.first)
      continue;

    // u に隣接する頂点を候補に追加
    for (auto cand : G.at(u)) {
      int v = cand.first;
      if (color.at(v) == 2)
        continue;
      if (d.at(u) + cand.second < d.at(v)) {
        d.at(v) = d.at(u) + cand.second;
        PQ.push(make_pair(d.at(v), v));
        color.at(v) = 1;
        p.at(v) = u + 1;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  G.resize(N);
  p = vector<int>(N, -1);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).push_back(make_pair(b, 1));
    G.at(b).push_back(make_pair(a, 1));
  }
  dijkstra(0);
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << p.at(i) << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}

const int N = 2e5 + 5;
int n, m, fa[N], ans[N], vis[N];

struct Edge {
  int v, ne;
} e[N];
int head[N], tot;

inline void add(int u, int v);
void bfs();

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  bfs();
  if (m < n - 1)
    printf("No\n");
  else {
    printf("Yes\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\n", ans[i]);
  }
  return 0;
}

inline void add(int u, int v) {
  e[++tot] = (Edge){v, head[u]};
  head[u] = tot;
}

void bfs() {
  queue<int> qu;
  qu.push(1);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int i = head[u]; i; i = e[i].ne) {
      int v = e[i].v;
      if (!vis[v]) {
        vis[v] = 1;
        ans[v] = u;
        qu.push(v);
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}

const int N = 1e6 + 5;
int n, m, fa[N], ans[N], vis[N];

struct Edge {
  int v, ne;
} e[N];
int head[N], tot;

inline void add(int u, int v);
void bfs();

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    add(u, v);
    add(v, u);
  }
  bfs();
  if (m < n - 1)
    printf("No\n");
  else {
    printf("Yes\n");
    for (int i = 2; i <= n; ++i)
      printf("%d\n", ans[i]);
  }
  return 0;
}

inline void add(int u, int v) {
  e[++tot] = (Edge){v, head[u]};
  head[u] = tot;
}

void bfs() {
  queue<int> qu;
  qu.push(1);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int i = head[u]; i; i = e[i].ne) {
      int v = e[i].v;
      if (!vis[v]) {
        vis[v] = 1;
        ans[v] = u;
        qu.push(v);
      }
    }
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
queue<int> U;
vector<int> dist;
vector<int> d(10101);

void dfs(const Graph &G, int v) {
  U.push(v);
  dist[v] = 0;
  while (!U.empty()) {
    int v_2 = U.front();
    U.pop();
    for (auto next_v : G[v_2]) {
      if (dist[next_v] == -1) {
        U.push(next_v);
        dist[next_v] = dist[v_2] + 1;
        d[next_v] = v_2 + 1;
        // cout << next_v << endl; //通った場所を出力(確認用)
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  // 頂点 s をスタートとした探索
  dist.assign(n + 1, -1); // 全頂点を「未訪問」に初期化
  dfs(G, 0);
  // t に辿り着けるかどうか
  for (int i = 1; i < n; i++) {
    if (dist[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) { cout << d[i + 1] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
queue<int> U;
vector<int> dist;
vector<int> d(101010);

void dfs(const Graph &G, int v) {
  U.push(v);
  dist[v] = 0;
  while (!U.empty()) {
    int v_2 = U.front();
    U.pop();
    for (auto next_v : G[v_2]) {
      if (dist[next_v] == -1) {
        U.push(next_v);
        dist[next_v] = dist[v_2] + 1;
        d[next_v] = v_2 + 1;
        // cout << next_v << endl; //通った場所を出力(確認用)
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  // 頂点 s をスタートとした探索
  dist.assign(n + 1, -1); // 全頂点を「未訪問」に初期化
  dfs(G, 0);
  // t に辿り着けるかどうか
  for (int i = 1; i < n; i++) {
    if (dist[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) { cout << d[i + 1] << endl; }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
//  main.cpp
//  ABC168D

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1001001001;
int main(int argc, const char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> r[m + 1];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    r[a].push_back(b);
    r[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n + 1, INF); // 入り口からの距離
  dist.at(1) = 0;               // 入り口の洞窟
  q.push(1);
  vector<int> pre(n + 1, -1); // 直前の洞窟番号
  while (!q.empty()) {
    int v = q.front(); // 先頭の洞窟番号
    q.pop();
    for (int u : r[v]) {
      if (dist[u] == INF) {
        dist[u] = dist[v] + 1;
        pre[u] = v;
        q.push(u);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << pre.at(i) << endl;
  }

  return 0;
}


Fixed submission:
//  main.cpp
//  ABC168D

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1001001001;
int main(int argc, const char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> r[100005];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    r[a].push_back(b);
    r[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n + 1, INF); // 入り口からの距離
  dist.at(1) = 0;               // 入り口の洞窟
  q.push(1);
  vector<int> pre(n + 1, -1); // 直前の洞窟番号
  while (!q.empty()) {
    int v = q.front(); // 先頭の洞窟番号
    q.pop();
    for (int u : r[v]) {
      if (dist[u] == INF) {
        dist[u] = dist[v] + 1;
        pre[u] = v;
        q.push(u);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << pre.at(i) << endl;
  }

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int V, d[200001], r[200001], z[200001];
vector<int> G[200001];
int bfs(int s) {
  rep(i, V + 1) d[i] = 0, r[i] = 0;
  queue<int> que;
  que.push(s);
  r[s] = 1;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (r[nv])
        continue;
      r[nv]++;
      z[nv] = v;
      d[nv] = d[v] + 1;
      que.push(nv);
    }
  }
};
int main() {
  int n, m, a, b;
  cin >> n >> m;
  rep(i, m) cin >> a >> b, G[a].push_back(b), G[b].push_back(a);
  bfs(1);
  cout << "Yes\n";
  rep(i, n - 1) cout << z[i + 2] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int V, d[200001], r[200001], z[200001];
vector<int> G[200001];
void bfs(int s) {
  rep(i, V + 1) d[i] = 0, r[i] = 0;
  queue<int> que;
  que.push(s);
  r[s] = 1;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      if (r[nv])
        continue;
      r[nv]++;
      z[nv] = v;
      d[nv] = d[v] + 1;
      que.push(nv);
    }
  }
};
int main() {
  int n, m, a, b;
  cin >> n >> m;
  rep(i, m) cin >> a >> b, G[a].push_back(b), G[b].push_back(a);
  bfs(1);
  cout << "Yes\n";
  rep(i, n - 1) cout << z[i + 2] << "\n";
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); ++i)

using namespace std;

int main() {
  ll N, M, count, m;

  cin >> N >> M;

  vector<ll> A(M), B(M), X(N, -1), v;
  vector<vector<ll>> Y(N);

  rep(i, M) { cin >> A[i] >> B[i]; }

  rep(i, N) {
    rep(j, M) {
      if (A[j] == i + 1)
        Y[i].push_back(j);
      if (B[j] == i + 1)
        Y[i].push_back(j);
    }
  }

  v.push_back(1); // 出口まで近い部屋から順に入れていく箱
  X[0] = 0;
  count = 1; // vがいくつ埋まったか
  rep(s, N) {
    if (count == N)
      break;
    rep(i, Y[v[s] - 1].size()) {
      if (count == N)
        break;
      m = Y[v[s] - 1][i];
      if (A[m] == v[s]) {
        if (X[B[m] - 1] == -1) {
          v.push_back(B[m]);
          X[B[m] - 1] = v[s];
          count++;
        }
      } else if (B[m] == v[s]) {
        if (X[A[m] - 1] == -1) {
          v.push_back(A[m]);
          X[A[m] - 1] = v[s];
          count++;
        }
      }
    }
  }

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); ++i)

using namespace std;

int main() {
  ll N, M, count, m;

  cin >> N >> M;

  vector<ll> A(M), B(M), X(N, -1), v;
  vector<vector<ll>> Y(N);

  rep(i, M) { cin >> A[i] >> B[i]; }

  rep(i, M) {
    Y[A[i] - 1].push_back(i);
    Y[B[i] - 1].push_back(i);
  }

  v.push_back(1); // 出口まで近い部屋から順に入れていく箱
  X[0] = 0;
  count = 1; // vがいくつ埋まったか
  rep(s, N) {
    if (count == N)
      break;
    rep(i, Y[v[s] - 1].size()) {
      if (count == N)
        break;
      m = Y[v[s] - 1][i];
      if (A[m] == v[s]) {
        if (X[B[m] - 1] == -1) {
          v.push_back(B[m]);
          X[B[m] - 1] = v[s];
          count++;
        }
      } else if (B[m] == v[s]) {
        if (X[A[m] - 1] == -1) {
          v.push_back(A[m]);
          X[A[m] - 1] = v[s];
          count++;
        }
      }
    }
  }

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

  int N, M;
  cin >> N >> M;
  Graph<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);
  }

  vector<int> dist(N, 1e9);
  vector<int> ans(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  ans[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] < 1e9)
        continue;
      dist[nv] = dist[v] + 1;
      ans[nv] = v;
      que.push(nv);
    }
  }

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

  if (!flag)
    cout << "No" << '\n';
  else {
    cout << "Yes" << '\n';
    for (int i = 1; i < N; ++i)
      cout << ans[i] + 1 << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

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

  vector<int> dist(N, 1e9);
  vector<int> ans(N, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  ans[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] < 1e9)
        continue;
      dist[nv] = dist[v] + 1;
      ans[nv] = v;
      que.push(nv);
    }
  }

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

  if (!flag)
    cout << "No" << '\n';
  else {
    cout << "Yes" << '\n';
    for (int i = 1; i < N; ++i)
      cout << ans[i] + 1 << '\n';
  }

  return 0;
}
Problem p02678 (C++)

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

int N, M;
// std::vector<int> A, B;
int A, B;
std::vector<std::vector<int>> adj_list;
std::vector<int> sign;
std::queue<int> que;

int main() {
  std::cin >> N >> M;
  // A.assign(M, 0);
  // B.assign(M, 0);
  adj_list.assign(M, std::vector<int>());
  sign.assign(N, -1);
  for (int j = 0; j < N; j++) {
    adj_list.at(j).reserve(N);
  }

  for (int i = 0; i < M; i++) {
    std::cin >> A >> B;
    --A;
    --B;
    adj_list.at(A).push_back(B);
    adj_list.at(B).push_back(A);
  }
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : adj_list.at(v)) {
      if (sign.at(nv) == -1) {
        sign.at(nv) = v;
        que.push(nv);
      }
    }
  }

  std::cout << "Yes" << std::endl;
  for (int j = 1; j < N; j++) {
    std::cout << sign.at(j) + 1 << std::endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

int N, M;
// std::vector<int> A, B;
int A, B;
std::vector<std::vector<int>> adj_list;
std::vector<int> sign;
std::queue<int> que;

int main() {
  std::cin >> N >> M;
  // A.assign(M, 0);
  // B.assign(M, 0);
  adj_list.assign(N, std::vector<int>());
  sign.assign(N, -1);
  for (int j = 0; j < N; j++) {
    adj_list.at(j).reserve(N);
  }

  for (int i = 0; i < M; i++) {
    std::cin >> A >> B;
    --A;
    --B;
    adj_list.at(A).push_back(B);
    adj_list.at(B).push_back(A);
  }
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : adj_list.at(v)) {
      if (sign.at(nv) == -1) {
        sign.at(nv) = v;
        que.push(nv);
      }
    }
  }

  std::cout << "Yes" << std::endl;
  for (int j = 1; j < N; j++) {
    std::cout << sign.at(j) + 1 << std::endl;
  }
}
Problem p02678 (C++)

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

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPV(iter, v)                                                          \
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

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

  vector<ll> michishirube(N, -1);
  queue<ll> q;
  vector<bool> isVisited(N, false);
  vector<bool> isAdd(N, false);
  q.push(0);
  isAdd[0] = true;
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    isVisited[p] = true;
    REPV(it, G[p]) {
      if (!isVisited[*it] || !isAdd[*it]) {
        q.push(*it);
        isAdd[*it] = true;
        if (michishirube[*it] == -1) {
          michishirube[*it] = p;
        }
      }
    }
  }

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


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

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPV(iter, v)                                                          \
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

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

  vector<ll> michishirube(N, -1);
  queue<ll> q;
  vector<bool> isVisited(N, false);
  vector<bool> isAdd(N, false);
  q.push(0);
  isAdd[0] = true;
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    isVisited[p] = true;
    REPV(it, G[p]) {
      if (!isVisited[*it] && !isAdd[*it]) {
        q.push(*it);
        isAdd[*it] = true;
        if (michishirube[*it] == -1) {
          michishirube[*it] = p;
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (ll i = 1; i < N; ++i) {
    cout << michishirube[i] + 1 << endl;
  }
}
Problem p02678 (C++)

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

lint gcd(lint x, lint y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}

lint lcm(lint x, lint y) { return abs(x - y) / gcd(x, y); }

int main() {
  lint n, m;
  cin >> n >> m;
  vector<lint> a(m, 0), b(m, 0);
  for (lint i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }

  vector<vector<lint>> mat(m);
  vector<int> check(n, -1);

  for (lint i = 0; i < m; i++) {
    mat[a[i] - 1].push_back(b[i] - 1);
    mat[b[i] - 1].push_back(a[i] - 1);
  }

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

  while (!q.empty()) {
    lint elem = q.front();
    q.pop();
    lint size = (int)mat[elem].size();

    for (lint i = 0; i < size; i++) {
      if (check[mat[elem][i]] == -1) {
        check[mat[elem][i]] = elem;
        q.push(mat[elem][i]);
      }
    }
  }

  for (lint i = 1; i < n; i++) {
    if (check[i] == -1) {
      cout << "No\n";
      return 0;
    }
  }

  cout << "Yes\n";
  for (lint i = 1; i < n; i++) {
    cout << check[i] + 1 << endl;
  }
  return 0;
}

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

lint gcd(lint x, lint y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}

lint lcm(lint x, lint y) { return abs(x - y) / gcd(x, y); }

int main() {
  lint n, m;
  cin >> n >> m;
  vector<lint> a(m, 0), b(m, 0);
  for (lint i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }

  vector<vector<lint>> mat(n);
  vector<int> check(n, -1);

  for (lint i = 0; i < m; i++) {
    mat[a[i] - 1].push_back(b[i] - 1);
    mat[b[i] - 1].push_back(a[i] - 1);
  }

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

  while (!q.empty()) {
    lint elem = q.front();
    q.pop();
    lint size = (int)mat[elem].size();

    for (lint i = 0; i < size; i++) {
      if (check[mat[elem][i]] == -1) {
        check[mat[elem][i]] = elem;
        q.push(mat[elem][i]);
      }
    }
  }

  for (lint i = 1; i < n; i++) {
    if (check[i] == -1) {
      cout << "No\n";
      return 0;
    }
  }

  cout << "Yes\n";
  for (lint i = 1; i < n; i++) {
    cout << check[i] + 1 << endl;
  }
  return 0;
}
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++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> que;
  que.push(0);
  vector<int> ans(n);
  ans[0] = -1;
  while (!que.empty()) {
    auto it = que.front();
    que.pop();
    for (auto i : v[it]) {
      if (ans[i] == 0) {
        ans[i] = it + 1;
        que.push(i);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> que;
  que.push(0);
  vector<int> ans(n);
  ans[0] = -1;
  while (!que.empty()) {
    auto it = que.front();
    que.pop();
    for (auto i : v[it]) {
      if (ans[i] == 0) {
        ans[i] = it + 1;
        que.push(i);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }
}