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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define eps 1e-3
#define pi acos(-1.0)
#define inf 0x3f
#define INF 0x3f3f3f3f
#define pb push_back
#define debug1 cout << "&&";
#define debug2 cout << "**";
#define ms(a, x) memset(a, x, sizeof(a))
#define for0(i, n) for (int i = 0; i < n; ++i)
#define for1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef pair<long long, long long> pll;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
/*=================================================================================*/

int n, m, head[N], ver[N << 1], nex[N << 1], tot;
int vis[N], ans[N], sum;

void add(int a, int b) {
  ver[++tot] = b;
  nex[tot] = head[a];
  head[a] = tot;
}

int main() {
  cin >> n >> m;
  for1(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i; i = nex[i]) {
      int y = ver[i];
      if (!vis[y]) {
        ans[y] = x;
        sum++, vis[y] = 1;
        q.push(y);
      }
    }
  }
  if (sum != n)
    puts("No");
  else {
    puts("Yes");
    for (int i = 2; i <= n; ++i)
      printf("%d\n", ans[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define eps 1e-3
#define pi acos(-1.0)
#define inf 0x3f
#define INF 0x3f3f3f3f
#define pb push_back
#define debug1 cout << "&&";
#define debug2 cout << "**";
#define ms(a, x) memset(a, x, sizeof(a))
#define for0(i, n) for (int i = 0; i < n; ++i)
#define for1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef pair<long long, long long> pll;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
/*=================================================================================*/

int n, m, head[N], ver[M], nex[M], tot;
int vis[N], ans[N], sum;

void add(int a, int b) {
  ver[++tot] = b;
  nex[tot] = head[a];
  head[a] = tot;
}

int main() {
  cin >> n >> m;
  for1(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i; i = nex[i]) {
      int y = ver[i];
      if (!vis[y]) {
        ans[y] = x;
        sum++, vis[y] = 1;
        q.push(y);
      }
    }
  }
  if (sum != n)
    puts("No");
  else {
    puts("Yes");
    for (int i = 2; i <= n; ++i)
      printf("%d\n", ans[i]);
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))

template <class T> T gcd(T a, T b) { return ((b == 0) ? a : gcd(b, a % b)); }

vi g[100005];
int vis[100005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  queue<int> q;
  q.push(0);
  vis[0] = 1;
  vi ans(n, 0);
  while (!q.empty()) {
    queue<int> q1;
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      for (int u : g[cur]) {
        if (!vis[u]) {
          q1.push(u);
          vis[cur] = 1;
          if (!ans[u])
            ans[u] = cur + 1;
        }
      }
    }
    q = q1;
  }
  cout << "Yes" << endl;
  repn(i, 1, n) cout << ans[i] << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/


Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))

template <class T> T gcd(T a, T b) { return ((b == 0) ? a : gcd(b, a % b)); }

vi g[100005];
int vis[100005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  queue<int> q;
  q.push(0);
  vis[0] = 1;
  vi ans(n, 0);
  while (!q.empty()) {
    queue<int> q1;
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      for (int u : g[cur]) {
        if (!vis[u]) {
          q1.push(u);
          vis[u] = 1;
          if (!ans[u])
            ans[u] = cur + 1;
        }
      }
    }
    q = q1;
  }
  cout << "Yes" << endl;
  repn(i, 1, n) cout << ans[i] << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/
Problem p02678 (C++)

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

vector<int> v[10001] = {};
vector<int> vs(10001, -1);

void bfs() {
  int s = 1;
  vs[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    int num = (int)v[t].size();
    for (int i = 0; i < num; i++) {
      if (vs[v[t][i]] == -1) {
        q.push(v[t][i]);
        vs[v[t][i]] = t;
      }
    }
  }
  return;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    v[A].push_back(B);
    v[B].push_back(A);
  }
  bfs();
  for (int i = 2; i <= N; i++) {
    if (vs[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << vs[i] << endl;
  }
  return 0;
}


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

vector<int> v[100001] = {};
vector<int> vs(100001, -1);

void bfs() {
  int s = 1;
  vs[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    int num = (int)v[t].size();
    for (int i = 0; i < num; i++) {
      if (vs[v[t][i]] == -1) {
        q.push(v[t][i]);
        vs[v[t][i]] = t;
      }
    }
  }
  return;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    v[A].push_back(B);
    v[B].push_back(A);
  }
  bfs();
  for (int i = 2; i <= N; i++) {
    if (vs[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << vs[i] << endl;
  }
  return 0;
}
Problem p02678 (C++)

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

#define ll long long
// #define MULTI_TEST

int n, m;
vector<int> p;
vector<vector<int>> to;
vector<int> ans;
vector<int> a;

void path(int par, int r, int d) {
  if (p[r])
    return;
  p[r] = 1;
  if (a[r] >= d) {
    a[r] = d;
    ans[r] = par;
  }
  for (int i : to[r]) {
    path(r, i, d + 1);
  }
  p[r] = 0;
}

void solve() {
  p.clear();
  ans.clear();
  to.clear();
  a.clear();
  cin >> n >> m;

  p.assign(n + 1, 0);
  to.assign(n + 1, vector<int>());
  ans.assign(n + 1, -1);
  a.assign(n + 1, INT_MAX);

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

  path(0, 1, 0);

  int poss = 1;
  for (int i = 2; i <= n; i++) {
    if (ans[i] == -1) {
      poss = 0;
      break;
    }
  }

  if (poss) {
    cout << "Yes\n";
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << "\n";
    }
  } else {
    cout << "No\n";
  }
}

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

#ifdef MULTI_TEST
  int t;
  cin >> t;
  while (t--) {
    solve();
    // cout << "\n";
  }
#else
  solve();
// 	cout << "\n";
#endif

  return 0;
}


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

#define ll long long
// #define MULTI_TEST

int n, m;
vector<int> p;
vector<vector<int>> to;
vector<int> ans;
vector<int> a;

void path(int par, int r, int d) {
  if (p[r])
    return;
  p[r] = 1;
  if (a[r] >= d) {
    a[r] = d;
    ans[r] = par;
  }
  for (int i : to[r]) {
    path(r, i, d + 1);
  }
  p[r] = 0;
}

void solve() {
  p.clear();
  ans.clear();
  to.clear();
  a.clear();
  cin >> n >> m;

  p.assign(n + 1, 0);
  to.assign(n + 1, vector<int>());
  ans.assign(n + 1, -1);
  a.assign(n + 1, INT_MAX);

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

  /*path(0, 1, 0);

  int poss = 1;
  for(int i=2; i<=n; i++) {
          if(ans[i] == -1) {
                  poss = 0;
                  break;
          }
  }*/

  queue<pair<int, int>> q;
  q.push(make_pair(0, 1));

  while (q.size()) {
    int i = q.front().second;
    int par = q.front().first;
    q.pop();
    if (p[i])
      continue;

    p[i] = 1;
    ans[i] = par;

    for (int t : to[i]) {
      q.push(make_pair(i, t));
    }
  }

  int poss = 1;
  for (int i = 2; i <= n; i++) {
    if (ans[i] == -1) {
      poss = 0;
      break;
    }
  }

  if (poss) {
    cout << "Yes\n";
    for (int i = 2; i <= n; i++) {
      cout << ans[i] << "\n";
    }
  } else {
    cout << "No\n";
  }
}

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

#ifdef MULTI_TEST
  int t;
  cin >> t;
  while (t--) {
    solve();
    // cout << "\n";
  }
#else
  solve();
// 	cout << "\n";
#endif

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

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

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

  priority_queue<P> q;
  q.push(P(0, 0));
  int dp[N];
  fill(dp, dp + N, 1000000005);
  dp[0] = 0;
  while (!q.empty()) {
    int now = q.top().second;
    int d = q.top().first;
    q.pop();
    if (dp[now] != d)
      continue;

    for (auto next : G[now]) {
      if (dp[next] > dp[now] + 1) {
        dp[next] = dp[now] + 1;
        q.push(P(dp[now] + 1, next));
      }
    }
  }

  bool fl = true;
  for (int i = 0; i < N; i++) {
    if (dp[i] == 1000000005)
      fl = false;
  }

  if (!fl) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  int ans[N];
  for (int i = 0; i < M; i++) {
    if (dp[b[i]] == dp[a[i]] - 1)
      ans[a[i]] = b[i];
    if (dp[a[i]] == dp[b[i]] - 1)
      ans[b[i]] = a[i];
  }

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

  return 0;
}


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

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

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

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

  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 0));
  int dp[N];
  fill(dp, dp + N, 1000000005);
  dp[0] = 0;
  while (!q.empty()) {
    int now = q.top().second;
    int d = q.top().first;
    q.pop();
    if (dp[now] != d)
      continue;

    for (auto next : G[now]) {
      if (dp[next] > dp[now] + 1) {
        dp[next] = dp[now] + 1;
        q.push(P(dp[now] + 1, next));
      }
    }
  }

  bool fl = true;
  for (int i = 0; i < N; i++) {
    if (dp[i] == 1000000005)
      fl = false;
  }

  if (!fl) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  int ans[N];
  for (int i = 0; i < M; i++) {
    if (dp[b[i]] == dp[a[i]] - 1)
      ans[a[i]] = b[i];
    if (dp[a[i]] == dp[b[i]] - 1)
      ans[b[i]] = a[i];
  }

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

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

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

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

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

  return 0;
}

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

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

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

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

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int INF = 2e6;
ll MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }

  queue<int> que;
  que.push(0);
  vector<bool> vis(n, false);
  vis[0] = true;
  vector<int> dist(n);
  vector<int> ans(n);

  while (que.size()) {
    int p = que.front();
    que.pop();
    for (auto i : g[p]) {
      if (!vis[i]) {
        vis[i] = true;
        dist[i] = dist[p] + 1;
        que.push(i);
        ans[i] = p + 1;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (dist[i] == 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int INF = 2e6;
ll MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }

  queue<int> que;
  que.push(0);
  vector<bool> vis(n, false);
  vis[0] = true;
  vector<int> dist(n);
  vector<int> ans(n);

  while (que.size()) {
    int p = que.front();
    que.pop();
    for (auto i : g[p]) {
      if (!vis[i]) {
        vis[i] = true;
        dist[i] = dist[p] + 1;
        que.push(i);
        ans[i] = p + 1;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (dist[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: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;

int nextr[100010];
vector<int> rt[10010];
int record[100010];

int main() {
  int N, M;
  cin >> N >> M;
  /*
  for (int i =1;i<N+1;i++){
      for(int j=1;j<N+1;j++){
          route[i][j]=0;
      }
  }*/
  for (int i = 1; i < N + 1; i++)
    nextr[i] = record[i] = 0;
  int A[M], B[M];
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    //  route[A[i]][B[i]]= route[B[i]][A[i]]=1;
    rt[A[i]].push_back(B[i]);
    rt[B[i]].push_back(A[i]);
  }
  queue<int> q;
  q.push(1);
  record[1] = 1;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (const auto &e : rt[cur]) {
      if (record[e] == 0) {
        nextr[e] = cur;
        record[e] = 1; // このタイミングで訪問済みにする
        q.push(e);
      }
    }

    /*        for(int i=1;i<N+1;i++){
               if(route[cur][i]==1 && record[i]==0){
                   nextr[i]=cur;
                   record[i]=1;//このタイミングで訪問済みにする
                   q.push(i);
               }
            }
            */
  }
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++) {
    cout << nextr[i] << endl;
  }
  return 0;
}


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

int nextr[100010];
vector<int> rt[100100];
int record[100010];

int main() {
  int N, M;
  cin >> N >> M;
  /*
  for (int i =1;i<N+1;i++){
      for(int j=1;j<N+1;j++){
          route[i][j]=0;
      }
  }*/
  for (int i = 1; i < N + 1; i++)
    nextr[i] = record[i] = 0;
  int A[M], B[M];
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    //  route[A[i]][B[i]]= route[B[i]][A[i]]=1;
    rt[A[i]].push_back(B[i]);
    rt[B[i]].push_back(A[i]);
  }
  queue<int> q;
  q.push(1);
  record[1] = 1;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (const auto &e : rt[cur]) {
      if (record[e] == 0) {
        nextr[e] = cur;
        record[e] = 1; // このタイミングで訪問済みにする
        q.push(e);
      }
    }

    /*        for(int i=1;i<N+1;i++){
               if(route[cur][i]==1 && record[i]==0){
                   nextr[i]=cur;
                   record[i]=1;//このタイミングで訪問済みにする
                   q.push(i);
               }
            }
            */
  }
  cout << "Yes" << endl;
  for (int i = 2; i < N + 1; i++) {
    cout << nextr[i] << endl;
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define INF 1e+9
#define MAX_V 20002

struct edge {
  int to;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];
int mark[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;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + 1) {
        d[e.to] = d[v] + 1;
        mark[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge ea = {a};
    edge eb = {b};
    G[a].push_back(eb);
    G[b].push_back(ea);
  }
  dijkstra(0);
  rep(i, V) {
    if (d[i] == INF) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < V; i++) {
    cout << mark[i] + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define INF 1e+9
#define MAX_V 200002

struct edge {
  int to;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];
int mark[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;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + 1) {
        d[e.to] = d[v] + 1;
        mark[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  int E;
  cin >> E;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge ea = {a};
    edge eb = {b};
    G[a].push_back(eb);
    G[b].push_back(ea);
  }
  dijkstra(0);
  rep(i, V) {
    if (d[i] == INF) {
      cout << "No" << endl;
      exit(0);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < V; i++) {
    cout << mark[i] + 1 << endl;
  }
}
Problem p02678 (C++)

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

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> v(m);
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> que;
  que.push(0);
  vector<bool> fin(n);
  vector<int> flag(n);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    rep(i, v[now].size()) {
      int nxt = v[now][i];
      if (!fin[nxt]) {
        flag[nxt] = now;
        fin[nxt] = true;
        que.push(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) cout << flag[i + 1] + 1 << endl;
}

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

int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<vector<int>> v(n);
  rep(i, m) {
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  queue<int> que;
  que.push(0);
  vector<bool> fin(n);
  vector<int> flag(n);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    rep(i, v[now].size()) {
      int nxt = v[now][i];
      if (!fin[nxt]) {
        flag[nxt] = now;
        fin[nxt] = true;
        que.push(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n - 1) cout << flag[i + 1] + 1 << endl;
}
Problem p02678 (C++)

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

const string YES = "Yes";
const string NO = "No";

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  vector<vector<long long>> lis(N + 1);
  vector<long long> path(N + 1, LLONG_MAX);
  for (long long i = 0; i < N; i++) {
    lis[A[i]].push_back(B[i]);
    lis[B[i]].push_back(A[i]);
  }

  queue<long long> q;
  q.push(1);
  path[0] = 0;
  path[1] = 0;

  while (!q.empty()) {
    long long edge = q.front();
    // cout << edge << ": ";
    for (auto i : lis[edge]) {
      // cout << i << " ";
      if (path[i] == LLONG_MAX) {
        path[i] = edge;
        q.push(i);
      }
    }
    // cout << endl;
    q.pop();
  }

  if (find(path.begin(), path.end(), LLONG_MAX) == path.end()) {
    cout << YES << endl;
    for (long long i = 2; i <= N; i++) {
      cout << path[i] << endl;
    }
  } else {
    cout << NO << endl;
  }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}


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

const string YES = "Yes";
const string NO = "No";

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  vector<vector<long long>> lis(N + 1);
  vector<long long> path(N + 1, LLONG_MAX);
  for (long long i = 0; i < M; i++) {
    lis[A[i]].push_back(B[i]);
    lis[B[i]].push_back(A[i]);
  }

  queue<long long> q;
  q.push(1);
  path[0] = 0;
  path[1] = 0;

  while (!q.empty()) {
    long long edge = q.front();
    // cout << edge << ": ";
    for (auto i : lis[edge]) {
      // cout << i << " ";
      if (path[i] == LLONG_MAX) {
        path[i] = edge;
        q.push(i);
      }
    }
    // cout << endl;
    q.pop();
  }

  if (find(path.begin(), path.end(), LLONG_MAX) == path.end()) {
    cout << YES << endl;
    for (long long i = 2; i <= N; i++) {
      cout << path[i] << endl;
    }
  } else {
    cout << NO << endl;
  }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}
Problem p02678 (C++)

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

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<vector<int>> e(m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    e[a].emplace_back(b);
    e[b].emplace_back(a);
  }
  vector<int> nx(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (auto &&i : e[t]) {
      if (nx[i] != -1) {
        continue;
      }
      nx[i] = t;
      q.push(i);
    }
  }
  bool f = true;
  for (int i = 1; i < n; i++) {
    if (nx[i] == -1) {
      f = false;
      break;
    }
  }
  if (f) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << nx[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}


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

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<vector<int>> e(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    e[a].emplace_back(b);
    e[b].emplace_back(a);
  }
  vector<int> nx(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (auto &&i : e[t]) {
      if (nx[i] != -1) {
        continue;
      }
      nx[i] = t;
      q.push(i);
    }
  }
  bool f = true;
  for (int i = 1; i < n; i++) {
    if (nx[i] == -1) {
      f = false;
      break;
    }
  }
  if (f) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << nx[i] + 1 << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p02678 (C++)

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

using namespace std;

const int N = 5e3 + 5;

int t;
int n, m;
int par[N];
bool vis[N];

vector<vector<int>> v;

void fail() {
  cout << "No";
  exit(0);
}

void solve() {
  cin >> n >> m;
  v.resize(n + 5);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  queue<int> q;

  q.push(1);
  vis[1] = true;
  while (q.size()) {
    int node = q.front();
    q.pop();
    for (int to : v[node]) {
      if (vis[to])
        continue;
      else {
        q.push(to);
        vis[to] = true;
        par[to] = node;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      fail();
  }
  cout << "Yes" << endl;

  for (int i = 2; i <= n; i++) {
    cout << par[i] << endl;
  }
}

int main() {
  t = 1;

  while (t--) {
    solve();
  }
}

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

using namespace std;

const int N = 1e5 + 5;

int t;
int n, m;
int par[N];
bool vis[N];

vector<vector<int>> v;

void fail() {
  cout << "No";
  exit(0);
}

void solve() {
  cin >> n >> m;
  v.resize(n + 5);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  queue<int> q;

  q.push(1);
  vis[1] = true;
  while (q.size()) {
    int node = q.front();
    q.pop();
    for (int to : v[node]) {
      if (vis[to])
        continue;
      else {
        q.push(to);
        vis[to] = true;
        par[to] = node;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      fail();
  }
  cout << "Yes" << endl;

  for (int i = 2; i <= n; i++) {
    cout << par[i] << endl;
  }
}

int main() {
  t = 1;

  while (t--) {
    solve();
  }
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
constexpr auto INFI = 2147483647;
const ll INFL = 9223372036854775807;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> parent(n, -1);
  vector<vector<int>> route(m);
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    route[a].emplace_back(b);
    route[b].emplace_back(a);
  }
  queue<int> que;
  que.push(0);
  parent[0] = INFI;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    if (route[now].empty())
      continue;
    int size_route = (int)route[now].size();
    rep(i, size_route) {
      int next = route[now][i];
      if (parent[next] == -1) {
        parent[next] = now;
        que.push(next);
      }
    }
  }

  rep(i, n) {
    if (parent[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << parent[i] + 1 << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
constexpr auto INFI = 2147483647;
const ll INFL = 9223372036854775807;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> parent(n, -1);
  vector<vector<int>> route(n);
  int a, b;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    route[a].emplace_back(b);
    route[b].emplace_back(a);
  }
  queue<int> que;
  que.push(0);
  parent[0] = INFI;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    if (route[now].empty())
      continue;
    int size_route = (int)route[now].size();
    rep(i, size_route) {
      int next = route[now][i];
      if (parent[next] == -1) {
        parent[next] = now;
        que.push(next);
      }
    }
  }

  rep(i, n) {
    if (parent[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << parent[i] + 1 << endl;
  }

  return 0;
}
Problem p02678 (C++)

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

using namespace std;

#define ll long long

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; ++i) {
    int tmpa, tmpb;
    cin >> tmpa >> tmpb;
    a[i] = tmpa - 1;
    b[i] = tmpb - 1;
  }

  vector<int> dist(n, 1000000000);

  vector<vector<int>> coef(n);
  for (int i = 0; i < m; ++i) {
    coef[a[i]].push_back(b[i]);
    coef[b[i]].push_back(a[i]);
  }

  // for(int i = 0;i < n;++i){
  //     cout << coef[i].size() << endl;
  // }

  queue<int> next;
  next.push(0);
  dist[0] = 0;
  vector<int> ans(n);
  for (int i = next.front(); next.size() > 0; i = next.front()) {
    // cout << i << endl;
    for (int j = 0; j < coef[i].size(); ++j) {
      if (dist[coef[i][j]] > dist[i] + 1) {
        dist[coef[i][j]] = dist[i] + 1;
        // cout << "next push : " << coef[i][j] << endl;
        next.push(coef[i][j]);
        ans[j] = i;
      }
    }
    next.pop();
    // cout << "next is : " << next.front() << endl;
  }

  int flag = 1;
  for (int i = 1; i < n; ++i) {
    if (dist[i] == 1000000000) {
      flag = 0;
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define ll long long

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; ++i) {
    int tmpa, tmpb;
    cin >> tmpa >> tmpb;
    a[i] = tmpa - 1;
    b[i] = tmpb - 1;
  }

  vector<int> dist(n, 1000000000);

  vector<vector<int>> coef(n);
  for (int i = 0; i < m; ++i) {
    coef[a[i]].push_back(b[i]);
    coef[b[i]].push_back(a[i]);
  }

  // for(int i = 0;i < n;++i){
  //     cout << coef[i].size() << endl;
  // }

  queue<int> next;
  next.push(0);
  dist[0] = 0;
  vector<int> ans(n);
  for (int i = next.front(); next.size() > 0; i = next.front()) {
    // cout << i << endl;
    for (int j = 0; j < coef[i].size(); ++j) {
      if (dist[coef[i][j]] > dist[i] + 1) {
        dist[coef[i][j]] = dist[i] + 1;
        // cout << "next push : " << coef[i][j] << endl;
        next.push(coef[i][j]);
        ans[coef[i][j]] = i;
      }
    }
    next.pop();
    // cout << "next is : " << next.front() << endl;
  }

  int flag = 1;
  for (int i = 1; i < n; ++i) {
    if (dist[i] == 1000000000) {
      flag = 0;
    }
  }

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

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
ll const mod = 998244353;
const ll MAX = 300000;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
int main() {
  int n, m;
  cin >> n >> m;
  vvi ab(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    ab[a].push_back(b);
    ab[b].push_back(a);
  }
  vi ans(n);
  vi used(n);

  queue<int> q;
  q.push(0);
  while (q.size()) {
    int now = q.front();
    q.pop();
    used[now] = 1;
    for (int x : ab[now]) {
      if (used[x] == 1)
        continue;
      used[x] = 1;
      ans[x] = now + 1;
      q.push(x);
    }
    // cout<<"aaa";
  }
  cout << "Yes" << endl;
  arep(i, 1, n) { cout << ans[i] << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
ll const mod = 998244353;
const ll MAX = 300000;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
int main() {
  int n, m;
  cin >> n >> m;
  vvi ab(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    ab[a].push_back(b);
    ab[b].push_back(a);
  }
  vi ans(n);
  vi used(n);

  queue<int> q;
  q.push(0);
  while (q.size()) {
    int now = q.front();
    q.pop();
    used[now] = 1;
    for (int x : ab[now]) {
      if (used[x] == 1)
        continue;
      used[x] = 1;
      ans[x] = now + 1;
      q.push(x);
    }
    // cout<<"aaa";
  }
  cout << "Yes" << endl;
  arep(i, 1, n) { cout << ans[i] << endl; }
  return 0;
}
Problem p02678 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(10005)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print("Yes")
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v] + 1
        pre[i] = v
        q.append(i)

for i in range(1, n):
    ans = pre[i]
    ans += 1
    print(ans)


Fixed submission:
from collections import deque

n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(100005)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print("Yes")
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v] + 1
        pre[i] = v
        q.append(i)

for i in range(1, n):
    ans = pre[i]
    ans += 1
    print(ans)
Problem p02678 (Python)

Buggy submission (status: Time Limit Exceeded):
from dataclasses import dataclass
from collections import deque


@dataclass
class Query:
    now: int
    prev: int
    cost: int


N, M = map(int, input().split())
E = [[] for _ in range(N + 1)]

for e in range(M):
    a, b = map(int, input().split())
    E[a].append(b)
    E[b].append(a)

res = [-1 for _ in range(N + 1)]
cost = [float("inf") for _ in range(N + 1)]
cost[1] = 0

queue = deque()

for next in E[1]:
    queue.append(Query(next, 1, 1))

while len(queue):
    q = queue.popleft()
    if cost[q.now] > q.cost:
        res[q.now] = q.prev
        cost[q.now] = q.cost

    for next in E[q.now]:
        if cost[next] > q.cost + 1:
            queue.append(Query(next, q.now, q.cost + 1))

for r in res[2:]:
    if r == -1:
        print("No")
        exit()

print("Yes")
for r in res[2:]:
    print(r)


Fixed submission:
from dataclasses import dataclass
from collections import deque


@dataclass
class Query:
    now: int
    prev: int
    cost: int


N, M = map(int, input().split())
E = [[] for _ in range(N + 1)]

for e in range(M):
    a, b = map(int, input().split())
    E[a].append(b)
    E[b].append(a)

res = [-1 for _ in range(N + 1)]
cost = [float("inf") for _ in range(N + 1)]
cost[1] = 0

queue = deque()

for next in E[1]:
    queue.append(Query(next, 1, 1))

while len(queue):
    q = queue.popleft()
    if cost[q.now] > q.cost:
        res[q.now] = q.prev
        cost[q.now] = q.cost

        for next in E[q.now]:
            if cost[next] > q.cost + 1:
                queue.append(Query(next, q.now, q.cost + 1))

for r in res[2:]:
    if r == -1:
        print("No")
        exit()

print("Yes")
for r in res[2:]:
    print(r)
Problem p02678 (Python)

Buggy submission (status: Runtime Error):
import heapq


def dijkstra_heap(s):
    d = [float("inf")] * n
    prev = [float("inf")] * n
    d[s] = 0
    edgelist = [[d[s], s]]
    heapq.heapify(edgelist)

    while edgelist:
        dis, v = heapq.heappop(edgelist)
        if d[v] < dis:
            continue

        for e in edge[v]:
            if d[e] > dis + 1:
                d[e] = dis + 1
                heapq.heappush(edgelist, [dis + 1, e])
                prev[e] = v
    return d, prev


n, w = map(int, input().split())

edge = [[] for i in range(w)]
for i in range(w):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    edge[x].append(y)
    edge[y].append(x)
d, prev = dijkstra_heap(0)

if float("inf") in prev[1:]:
    print("No")
else:
    print("Yes")
    for i in prev[1:]:
        print(i + 1)


Fixed submission:
import heapq


def dijkstra_heap(s):
    d = [float("inf")] * n
    prev = [float("inf")] * n
    d[s] = 0
    edgelist = [[d[s], s]]
    heapq.heapify(edgelist)

    while edgelist:
        dis, v = heapq.heappop(edgelist)
        if d[v] < dis:
            continue

        for e in edge[v]:
            if d[e] > dis + 1:
                d[e] = dis + 1
                heapq.heappush(edgelist, [dis + 1, e])
                prev[e] = v
    return d, prev


n, w = map(int, input().split())

edge = [[] for i in range(n)]
for i in range(w):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    edge[x].append(y)
    edge[y].append(x)
d, prev = dijkstra_heap(0)

if float("inf") in prev[1:]:
    print("No")
else:
    print("Yes")
    for i in prev[1:]:
        print(i + 1)
Problem p02678 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

n, m = map(int, input().split())

to = [[] for _ in range(n + 1)]
for i in range(n):
    a, b = map(int, input().split())
    to[a].append(b)
    to[b].append(a)

q = deque([1])
dist = [-1] * (n + 1)
# print(to)

while q:
    v = q.popleft()

    for u in to[v]:
        if dist[u] != -1:
            continue
        q.append(u)
        dist[u] = v

print("Yes")
# print(dist)

for i in range(2, n + 1):
    print(dist[i])


Fixed submission:
from collections import deque

n, m = map(int, input().split())

to = [[] for _ in range(n + 1)]
for i in range(m):
    a, b = map(int, input().split())
    to[a].append(b)
    to[b].append(a)

q = deque([1])
dist = [-1] * (n + 1)
# print(to)

while q:
    v = q.popleft()

    for u in to[v]:
        if dist[u] != -1:
            continue
        q.append(u)
        dist[u] = v

print("Yes")
# print(dist)

for i in range(2, n + 1):
    print(dist[i])
Problem p02678 (Python)

Buggy submission (status: Time Limit Exceeded):
def solve(string):
    from collections import deque

    n, m, *ab = map(int, string.split())
    p = [[] for _ in range(n)]
    for a, b in zip(*[iter(ab)] * 2):
        p[a - 1].append(b - 1)
        p[b - 1].append(a - 1)
    s, ans, d = deque([0]), [0] * n, [0] + [n] * (n - 1)
    while s:
        c = s.popleft()
        for t in p[c]:
            if d[c] + 1 < d[t]:
                d[t] = d[c] + 1
                ans[t] = c + 1
                if t not in s:
                    s.append(t)
    return "Yes\n" + "\n".join(map(str, ans[1:]))


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))


Fixed submission:
def solve(string):
    from collections import deque

    n, m, *ab = map(int, string.split())
    p = [[] for _ in range(n)]
    for a, b in zip(*[iter(ab)] * 2):
        p[a - 1].append(b - 1)
        p[b - 1].append(a - 1)
    s, ans, d = deque([0]), [0] * n, [0] + [n] * (n - 1)
    while s:
        c = s.popleft()
        for t in p[c]:
            if d[c] + 1 < d[t]:
                d[t] = d[c] + 1
                ans[t] = c + 1
                s.append(t)
    return "Yes\n" + "\n".join(map(str, ans[1:]))


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

vector<vector<int>> to;

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

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

  vector<bool> done(n, false);
  vector<int> ans(n, -1);

  while (!que.empty()) {
    int id = que.front();
    que.pop();
    done[id] = true;

    for (int i = 0; i < to[id].size(); i++) {
      int nx = to[id][i];
      if (done[nx])
        continue;
      if (ans[nx] == -1)
        ans[nx] = id;
      que.push(nx);
    }
  }

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

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

using namespace std;

vector<vector<int>> to;

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

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

  vector<bool> done(n, false);
  vector<int> ans(n, -1);

  while (!que.empty()) {
    int id = que.front();
    que.pop();
    done[id] = true;

    for (int i = 0; i < to[id].size(); i++) {
      int nx = to[id][i];
      if (done[nx])
        continue;
      if (ans[nx] == -1)
        ans[nx] = id;
      que.push(nx);
      done[nx] = true;
    }
  }

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