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

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897932
// to set precision use fixed,setprecision(number_of_points_after_decimal
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define mod 1000000007
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void solution() {
  long double a, b, h, m;
  cin >> a >> b >> h >> m;
  long double md = m * 6.0;
  long double hd = 30.0 * h + md / 12;
  long double deg = abs(hd - md);

  long double codeg = cos(deg * (pi / 180));
  long double answer = a * a + b * b - 2 * a * b * codeg;
  answer = sqrt(answer);
  cout << fixed << setprecision(10) << answer << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int t;
  cin >> t;
  while (t--) {
    solution();
  }
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897932
// to set precision use fixed,setprecision(number_of_points_after_decimal
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define mod 1000000007
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void solution() {
  long double a, b, h, m;
  cin >> a >> b >> h >> m;
  long double md = m * 6.0;
  long double hd = 30.0 * h + md / 12;
  long double deg = abs(hd - md);

  long double codeg = cos(deg * (pi / 180));
  long double answer = a * a + b * b - 2 * a * b * codeg;
  answer = sqrt(answer);
  cout << fixed << setprecision(10) << answer << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  // int t;cin>>t;
  // while(t--){
  solution();
  // }
  return 0;
}
Problem p02677 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <set>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define Rep(i, n) for (ll i = 0; i <= n; ++i)
#define yes "yes"
#define Yes "Yes"
#define no "no"
#define No "No"

namespace mydef {
void say(string some) { cout << some << "\n"; }
} // namespace mydef
using namespace mydef;
typedef long long ll;
const int INF = 1001001001;
const long double PI = 3.14159265358979323846;

// ここからいじる

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

  long double a, b, h, m;
  cin >> a >> b >> h >> m;

  m += h * 60;
  long double limit = 60 * 12 / 11;
  while (m < limit) {
    m -= limit;
  }
  long double rad = m * 11 * PI / 360;
  long double C = pow(a, 2) + pow(b, 2) - 2 * a * b * cos(rad);
  long double c = pow(C, 0.5);
  cout << setprecision(11) << c << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <set>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define Rep(i, n) for (ll i = 0; i <= n; ++i)
#define yes "yes"
#define Yes "Yes"
#define no "no"
#define No "No"

namespace mydef {
void say(string some) { cout << some << "\n"; }
} // namespace mydef
using namespace mydef;
typedef long long ll;
const int INF = 1001001001;
const long double PI = 3.14159265358979323846;

// ここからいじる

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

  long double a, b, h, m;
  cin >> a >> b >> h >> m;

  m += h * 60;
  long double limit = 60 * 12 / 11;
  // while (m < limit)
  // {
  //   m -= limit;
  // }
  long double rad = m * 11 * PI / 360;
  long double C = pow(a, 2) + pow(b, 2) - 2 * a * b * cos(rad);
  long double c = pow(C, 0.5);
  cout << setprecision(11) << c << endl;

  return 0;
}
Problem p02677 (Python)

Buggy submission (status: Runtime Error):
import math


def main():
    a, b, h, m = map(int, input().split())
    if h == 0 and m == 0:
        print(abs(b - a))
        exit()

    h_basho = h * 60 + m
    m_basho = m * 12
    h_m_basho = h_basho - m_basho

    if h_m_basho == 360:
        print(a + b)
        exit()
    elif h_m_basho < 360:
        kakudo = 90 * h_m_basho / 180
    else:
        kakudo = 90 * (720 - h_m_basho) / 180

    ab2 = a**2 + b**2
    cos = 2 * a * b * (round(math.cos(round(math.radians(kakudo)), 20), 20))

    if kakudo <= 90:
        ac = ab2 - cos
        print("%.20f" % math.sqrt(ac))
    else:
        ac = ab2 + cos
        print("%.20f" % math.sqrt(ac))


if __name__ == "__main__":
    main()


Fixed submission:
import math


def main():
    a, b, h, m = map(int, input().split())
    th = (h * 60 + m) / 720 * 2 * math.pi
    tm = m / 60 * 2 * math.pi
    xh = a * math.cos(th)
    yh = a * math.sin(th)
    xm = b * math.cos(tm)
    ym = b * math.sin(tm)
    dx = xh - xm
    dy = yh - ym
    ans = math.sqrt(dx**2 + dy**2)
    print("%.10f" % ans)


if __name__ == "__main__":
    main()
Problem p02677 (Python)

Buggy submission (status: Runtime Error):
import math

A, B, H, M = map(int, input().split())

KA = H / 12 * 360 + ((M / 60) * (360 / 12))
KB = M / 60 * 360

K = abs(KA - KB)
if K > 180:
    K = 360 - K
# if K == 180:
#     print(A + B)
#     exit()
# if K == 0:
#     print(abs(B - A))
#     exit()
# cosB = round(math.cos(math.radians(K)), 13)
cosB = math.cos(math.radians(K)), 13
cc = A**2 + B**2 - (2 * B * A * cosB)
print(math.sqrt(cc))


Fixed submission:
import math

A, B, H, M = map(int, input().split())

KA = H / 12 * 360 + ((M / 60) * (360 / 12))
KB = M / 60 * 360

K = abs(KA - KB)
if K > 180:
    K = 360 - K
# if K == 180:
#     print(A + B)
#     exit()
# if K == 0:
#     print(abs(B - A))
#     exit()
# cosB = round(math.cos(math.radians(K)), 13)
cosB = math.cos(math.radians(K))
cc = A**2 + B**2 - (2 * B * A * cosB)
print(math.sqrt(cc))
Problem p02677 (Python)

Buggy submission (status: Runtime Error):
k = int(input())
s = list(input())
an_lis = []
if len(s) <= k:
    ans = "".join(s)
    print(ans)
else:
    for i in range(k):
        an_lis.append(s[i])
    an_lis.append("...")
    ans = "".join(an_lis)
    print(ans)


Fixed submission:
a, b, h, m = map(int, input().split())
import math

a_ang = (h / 12 + (1 / 12) * (m / 60)) * 2 * math.pi
b_ang = (m / 60) * 2 * math.pi
ax = a * math.cos(a_ang)
bx = b * math.cos(b_ang)
ay = a * math.sin(a_ang)
by = b * math.sin(b_ang)

x = ax - bx
y = ay - by
ans = (x**2 + y**2) ** 0.5
print(ans)
Problem p02677 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define multitest 0
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pqgreater(x) x, vector<x>, greater<x>
const ll mod = 998244353;
int solve();
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  if (multitest)
    cin >> t;
  while (t--)
    solve();
  return 0;
}
ld pi = acos(-1.0);
int solve() {
  int a, b, h, m;
  cin >> a >> b >> h >> m;
  ld angleh = 0.5 * ((ld)h * 60.0 + (ld)m);
  ld anglem = 6 * (ld)m;
  ld diff = fabs(angleh - anglem);
  ld ans = (ld)sqrt((ld)pow((ld)a, 2) + (ld)pow((ld)b, 2) -
                    2.0 * (ld)a * (ld)b * cos(pi * diff / 180.0));
  cout << fixed << setprecision(10) << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define multitest 0
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pqgreater(x) x, vector<x>, greater<x>
const ll mod = 998244353;
int solve();
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  if (multitest)
    cin >> t;
  while (t--)
    solve();
  return 0;
}
ld pi = acos(-1.0);
int solve() {
  int a, b, h, m;
  cin >> a >> b >> h >> m;
  ld angleh = 0.5 * ((ld)h * 60.0 + (ld)m);
  ld anglem = 6 * (ld)m;
  ld diff = fabs(angleh - anglem);
  ld ans = (ld)sqrt((ld)pow((ld)a, 2) + (ld)pow((ld)b, 2) -
                    2.0 * (ld)a * (ld)b * cos(pi * diff / 180.0));
  cout << fixed << setprecision(10) << ans << endl;
  return 0;
}
Problem p02677 (C++)

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

using namespace std;

#define PI 3.14159265359

int main() {

  double a, b, h, m;
  cin >> a >> b >> h >> m;

  double time = h + m / 60;
  double theta = 2 * PI / 12 * time;
  double phi = 2 * PI * time;

  while (phi > 2 * PI)
    phi = phi / 2 * PI;

  double angle;

  if (phi > theta)
    angle = phi - theta;
  else
    angle = theta - phi;

  if (angle > 180)
    angle = 360 - angle;

  double ans = sqrt(a * a + b * b - 2 * a * b * cos(angle));

  // cout << ans << endl;
  printf("%.10lf\r\n", ans);

  return 0;
}

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

using namespace std;

#define PI 3.14159265359

int main() {

  double a, b, h, m;
  cin >> a >> b >> h >> m;

  double time = h + m / 60;
  double theta = 2 * PI / 12 * time;
  double phi = 2 * PI * time;

  while (phi > 2 * PI)
    phi = phi - 2 * PI;

  double angle;

  if (phi > theta)
    angle = phi - theta;
  else
    angle = theta - phi;

  if (angle > 180)
    angle = 360 - angle;

  double ans = sqrt(a * a + b * b - 2 * a * b * cos(angle));

  // cout << ans << endl;
  printf("%.10lf\r\n", ans);

  return 0;
}
Problem p02677 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#define PI 3.1415926535
#define eps 1.0e-18
using namespace std;
int main() {
  int a, b, h, m;
  long double s, c, x;
  cin >> a >> b >> h >> m;
  s = (h * 60 + m) / 360.0 - m / 30.0;
  x = abs(a * a + b * b - 2 * a * b * cos(s * PI));
  c = sqrtl(x);
  while (abs(x - c * c) > eps)
    c = (c + x / c) / 2.0;
  cout << fixed << setprecision(10) << c;
  return 0;
}

Fixed submission:
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#define PI 3.1415926535
#define eps 1.0e-18
using namespace std;
int main() {
  int a, b, h, m;
  long double s, c, x;
  cin >> a >> b >> h >> m;
  s = (h * 60 + m) / 360.0 - m / 30.0;
  x = abs(a * a + b * b - 2 * a * b * cos(s * PI));
  c = sqrtl(x);
  cout << fixed << setprecision(10) << c;
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

#define PT pair<ll, int>

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

  int N, M;
  cin >> N >> M;
  vector<PT> E[100000];
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    E[a].pb({1, b});
    E[b].pb({1, a});
  }

  ll D[100001];
  rep1(i, N) D[i] = 1e18;
  D[1] = 0;

  priority_queue<PT, vector<PT>, greater<PT>> que;

  que.push(mp(0, 1));
  while (que.size()) {
    PT p = que.top();
    que.pop();
    int i = p.second;
    if (D[i] != p.first)
      continue;
    for (PT p2 : E[i]) {
      int j = p2.second;
      ll d = D[i] + p2.first;
      if (D[j] > d) {
        D[j] = d;
        que.push(mp(d, j));
      }
    }
  }

  int kotae[100001];
  for (int i = 2; i <= N; i++) {

    ll d = D[i];
    for (PT p2 : E[i]) {
      int j = p2.second;
      if (D[j] == d - 1) {
        kotae[i] = j;
      }
    }
  }

  co("Yes");
  for (int i = 2; i <= N; i++)
    co(kotae[i]);

  Would you please return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

#define PT pair<ll, int>

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

  int N, M;
  cin >> N >> M;
  vector<PT> E[100001];
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    E[a].pb({1, b});
    E[b].pb({1, a});
  }

  ll D[100001];
  rep1(i, N) D[i] = 1e18;
  D[1] = 0;

  priority_queue<PT, vector<PT>, greater<PT>> que;

  que.push(mp(0, 1));
  while (que.size()) {
    PT p = que.top();
    que.pop();
    int i = p.second;
    if (D[i] != p.first)
      continue;
    for (PT p2 : E[i]) {
      int j = p2.second;
      ll d = D[i] + p2.first;
      if (D[j] > d) {
        D[j] = d;
        que.push(mp(d, j));
      }
    }
  }

  int kotae[100001];
  for (int i = 2; i <= N; i++) {

    ll d = D[i];
    for (PT p2 : E[i]) {
      int j = p2.second;
      if (D[j] == d - 1) {
        kotae[i] = j;
      }
    }
  }

  co("Yes");
  for (int i = 2; i <= N; i++)
    co(kotae[i]);

  Would you please return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const int M = 1e9 + 7;
int si[] = {-1, 0, 1, 0};
int sj[] = {0, -1, 0, 1};

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> r(n);
  vector<int> d(n, -1);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    r[a].push_back(b);
    r[b].push_back(a);
  }

  queue<int> q;
  rep(i, r[0].size()) q.push(0);

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

  rep(i, d.size() - 1) if (d[i + 1] == -1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep(i, d.size() - 1) cout << d[i + 1] + 1 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const int M = 1e9 + 7;
int si[] = {-1, 0, 1, 0};
int sj[] = {0, -1, 0, 1};

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> r(n);
  vector<int> d(n, -1);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    r[a].push_back(b);
    r[b].push_back(a);
  }

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

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

  rep(i, d.size() - 1) if (d[i + 1] == -1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep(i, d.size() - 1) cout << d[i + 1] + 1 << endl;

  return 0;
}
Problem p02678 (C++)

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

vector<vector<int>> to(100010);
int dist[100010];
int pre[100010];
const int INF = 20000000;

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }

  REP(i, N) dist[i] = INF;
  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    for (auto nv : to[v]) {
      if (dist[nv] < dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      q.push(nv);
    }
  }

  int ok = 1;
  REP(i, N) if (dist[i] == INF) ok = 0;
  //  REP(i,N) cout << dist[i] << endl;
  if (ok == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << (pre[i] + 1) << endl;
  }
}


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

vector<vector<int>> to(100010);
int dist[100010];
int pre[100010];
const int INF = 20000000;

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }

  REP(i, N) dist[i] = INF;
  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    for (auto nv : to[v]) {
      if (dist[nv] <= dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      pre[nv] = v;
      q.push(nv);
    }
  }

  int ok = 1;
  REP(i, N) if (dist[i] == INF) ok = 0;
  //  REP(i,N) cout << dist[i] << endl;
  if (ok == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
      cout << (pre[i] + 1) << endl;
  }
}
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>;

const int INF = 1001001001;
vector<int> to[100005]; // インデックスに対応する部屋が隣接している部屋

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  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]) {
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < m; i++) {
    if (i == 0)
      continue;
    int ans = pre[i];
    ++ans;
    cout << ans << 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>;

const int INF = 1001001001;
vector<int> to[100005]; // インデックスに対応する部屋が隣接している部屋

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  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]) {
      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: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define int long long
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

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

  int N, M;
  cin >> N >> M;

  int A[M], B[M];
  Graph G(N);
  rep(i, M) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  queue<int> q;
  vector<int> dist(N, INF);
  vector<int> an(N, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : G[v]) {
      if (dist[nv] < dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      an[nv] = v;
      q.push(nv);
    }
  }
  string ans = "Yes";
  rep(i, N) {
    if (dist[i] == INF) {
      ans = "No";
      break;
    }
  }
  cout << ans << endl;
  if (ans == "Yes") {
    for (int i = 1; i < N; i++) {
      cout << an[i] + 1 << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define int long long
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

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

  int N, M;
  cin >> N >> M;

  int A[M], B[M];
  Graph G(N);
  rep(i, M) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  queue<int> q;
  vector<int> dist(N, INF);
  vector<int> an(N, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : G[v]) {
      if (dist[nv] <= dist[v] + 1)
        continue;
      dist[nv] = dist[v] + 1;
      an[nv] = v;
      q.push(nv);
    }
  }
  string ans = "Yes";
  rep(i, N) {
    if (dist[i] == INF) {
      ans = "No";
      break;
    }
  }
  cout << ans << endl;
  if (ans == "Yes") {
    for (int i = 1; i < N; i++) {
      cout << an[i] + 1 << endl;
    }
  }
}
Problem p02678 (C++)

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

int main() {
  int N, M, a, b, p, nxt;
  cin >> N >> M;
  queue<int> q, next_pos;
  vector<queue<int>> G(M);
  vector<int> done(N, 0);
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    G[a - 1].emplace(b - 1);
    G[b - 1].emplace(a - 1);
  }

  q.emplace(0);
  done[0] = 1;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    next_pos = G[p];
    while (!next_pos.empty()) {
      nxt = next_pos.front();
      next_pos.pop();
      if (done[nxt] == 0) {
        done[nxt] = p + 1;
        q.emplace(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << done[i] << endl;
  }
  return 0;
}


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

int main() {
  int N, M, a, b, p, nxt;
  cin >> N >> M;
  queue<int> q, next_pos;
  vector<queue<int>> G(N);
  vector<int> done(N, 0);
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    G[a - 1].emplace(b - 1);
    G[b - 1].emplace(a - 1);
  }

  q.emplace(0);
  done[0] = 1;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    next_pos = G[p];
    while (!next_pos.empty()) {
      nxt = next_pos.front();
      next_pos.pop();
      if (done[nxt] == 0) {
        done[nxt] = p + 1;
        q.emplace(nxt);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << done[i] << endl;
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 100005;
int head[MAX], ver[MAX], next1[MAX], a[MAX];
int cnt = 0;
int n, m;
void add(int u, int v) {
  ver[cnt] = v;
  next1[cnt] = head[u];
  head[u] = cnt++;
}
bool bfs(int x) {
  queue<int> q;
  q.push(x);
  a[x] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = next1[i]) {
      int v = ver[i];
      if (a[v] == -1) {
        a[v] = u;
        q.push(v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (a[i] == -1) {
      return false;
    }
  }
  return true;
}
int main(void) {
  cin >> n >> m;
  cnt = 0;
  memset(head, -1, sizeof(head));
  memset(a, -1, sizeof(a));
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }

  if (!bfs(1)) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << a[i] << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 1000005;
int head[MAX], ver[MAX], next1[MAX], a[MAX];
int cnt = 0;
int n, m;
void add(int u, int v) {
  ver[cnt] = v;
  next1[cnt] = head[u];
  head[u] = cnt++;
}
bool bfs(int x) {
  queue<int> q;
  q.push(x);
  a[x] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = next1[i]) {
      int v = ver[i];
      if (a[v] == -1) {
        a[v] = u;
        q.push(v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (a[i] == -1) {
      return false;
    }
  }
  return true;
}
int main(void) {
  cin >> n >> m;
  cnt = 0;
  memset(head, -1, sizeof(head));
  memset(a, -1, sizeof(a));
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }

  if (!bfs(1)) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i <= n; i++) {
      cout << a[i] << endl;
    }
  }

  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <queue>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<queue<int>> ca(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ca[a].push(b);
    ca[b].push(a);
  }
  vector<bool> vis(N, false);
  queue<int> que;
  que.push(0);
  vector<int> ans(N, -1);
  while (!que.empty()) {
    int u = que.front();
    // cout << u << "  ";
    que.pop();
    vis[u] = true;
    int n = ca[u].size();
    for (int i = 0; i < n; i++) {
      int v = ca[u].front();
      // cout << v << ' ';
      ca[u].pop();
      ca[u].push(v);
      if (vis[v])
        continue;
      if (ans[v] == -1)
        ans[v] = u;
      que.push(v);
    }
    // cout << endl;
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++)
    cout << ans[i] + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <queue>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<queue<int>> ca(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ca[a].push(b);
    ca[b].push(a);
  }
  vector<bool> vis(N, false);
  queue<int> que;
  que.push(0);
  vector<int> ans(N, -1);
  while (!que.empty()) {
    int u = que.front();
    // cout << u << "  ";
    que.pop();
    vis[u] = true;
    int n = ca[u].size();
    for (int i = 0; i < n; i++) {
      int v = ca[u].front();
      // cout << v << ' ';
      ca[u].pop();
      // ca[u].push(v);
      if (vis[v])
        continue;
      if (ans[v] == -1)
        ans[v] = u;
      que.push(v);
    }
    // cout << endl;
  }
  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;
using lint = long long int;
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

  cin >> n >> m;
  vector<int> to[m]; // 二次元配列
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b); // 道筋をtoに入れる
    to[b].push_back(a); // v[][]のi番目の末尾に入れる.つまり二次元配列
  }
  // cout << to[2][1] << endl;
  queue<int> q; // 先入先出
  vector<int> dist(n, INF); // 最小の距離を求めるから初期値はINFに設定しておく
  vector<int> pre(n, -1); // 直前の頂点.0があるから初期値は−１
  dist[0] = 0;            // 頂点０から０へのの距離は０
  q.push(0);
  while (!q.empty()) {    // qが空でない間
    int v = q.front();    // vにqの先頭を入れる
    q.pop();              // 先頭を消す
    for (int u : to[v]) { // ex.)to[0] = [1,2,3] uに1,2,3が順に入る
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v; // 一つ前の頂点を入れる
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << ++pre[i] << endl; // インクリメント忘れずに
  }
  return 0;
}

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

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

  cin >> n >> m;
  vector<int> to[100005]; // 二次元配列
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b); // 道筋をtoに入れる
    to[b].push_back(a); // v[][]のi番目の末尾に入れる.つまり二次元配列
  }
  // cout << to[2][1] << endl;
  queue<int> q; // 先入先出
  vector<int> dist(n, INF); // 最小の距離を求めるから初期値はINFに設定しておく
  vector<int> pre(n, -1); // 直前の頂点.0があるから初期値は−１
  dist[0] = 0;            // 頂点０から０へのの距離は０
  q.push(0);
  while (!q.empty()) {    // qが空でない間
    int v = q.front();    // vにqの先頭を入れる
    q.pop();              // 先頭を消す
    for (int u : to[v]) { // ex.)to[0] = [1,2,3] uに1,2,3が順に入る
      if (dist[u] != INF)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v; // 一つ前の頂点を入れる
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << ++pre[i] << endl; // インクリメント忘れずに
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////

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> dist(n, -1);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (int i = 0; i < g[node].size(); i++) {
      int next = g[node][i];
      if (dist[next] == -1) {
        dist[next] = node;
        q.push(next);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << dist[i] + 1 << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////

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);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (int i = 0; i < g[node].size(); i++) {
      int next = g[node][i];
      if (dist[next] == -1) {
        dist[next] = node;
        q.push(next);
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << dist[i] + 1 << endl;
  }
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int inf = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    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]) {
      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;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int inf = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    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]) {
      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;
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, 100005);
  vector<int> pre(n, -1);
  dist[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u = 0; u < n; v++) {
      if (dist[u] != 100005)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m, a, b;
  cin >> n >> m;
  vector<int> to[100005];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  queue<int> q;
  vector<int> dist(n, 100005);
  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]) {
      if (dist[u] != 100005)
        continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      q.push(u);
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    int ans = pre[i];
    ans++;
    cout << ans << endl;
  }
  return 0;
}
Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll GCD(ll a, ll b) { return a ? GCD(b % a, a) : b; }
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];
  vector<vector<int>> G(n);
  for (int i = 0; i < n; i++) {
    G[a[i] - 1].push_back(b[i] - 1);
    G[b[i] - 1].push_back(a[i] - 1);
  }
  vector<int> color(n, 1e9);
  vector<bool> seen(n, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : G[v]) {
      if (seen[i])
        continue;
      q.push(i);
      seen[i] = true;
      color[i] = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << color[i] + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll GCD(ll a, ll b) { return a ? GCD(b % a, a) : b; }
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];
  vector<vector<int>> G(n);
  for (int i = 0; i < m; i++) {
    G[a[i] - 1].push_back(b[i] - 1);
    G[b[i] - 1].push_back(a[i] - 1);
  }
  vector<int> color(n, 1e9);
  vector<bool> seen(n, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : G[v]) {
      if (seen[i])
        continue;
      q.push(i);
      seen[i] = true;
      color[i] = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << color[i] + 1 << endl;
  }
}
Problem p02678 (C++)

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

using namespace std;
using ll = long long;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, n) for (int i = (a); i >= (n); i--)
#define REPD(i, n) FOR(i, n, 0)

#define ALL(o) (o).begin(), (o).end()

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

const string YES = "Yes";
V<V<int>> m;

void solve(int N, int M) {
  V<int> ans(N + 1);
  queue<int> q;
  q.push(1);
  int tmp, n;
  while (!q.empty()) {
    n = q.front();
    q.pop();
    REP(i, m[n].size()) {
      tmp = m[n][i];
      if (ans[tmp] == 0) {
        ans[tmp] = n;
        q.push(tmp);
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 2, N + 1) { cout << ans[i] << endl; }
}

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, M + 1) {
    V<int> a;
    m.push_back(a);
  }

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


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

using namespace std;
using ll = long long;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, n) for (int i = (a); i >= (n); i--)
#define REPD(i, n) FOR(i, n, 0)

#define ALL(o) (o).begin(), (o).end()

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

const string YES = "Yes";
V<V<int>> m;

void solve(int N, int M) {
  V<int> ans(N + 1);
  queue<int> q;
  q.push(1);
  int tmp, n;
  while (!q.empty()) {
    n = q.front();
    q.pop();
    REP(i, m[n].size()) {
      tmp = m[n][i];
      if (ans[tmp] == 0) {
        ans[tmp] = n;
        q.push(tmp);
      }
    }
  }
  cout << "Yes" << endl;
  FOR(i, 2, N + 1) { cout << ans[i] << endl; }
}

int main() {
  int N, M;
  cin >> N >> M;
  REP(i, N + 1) {
    V<int> a;
    m.push_back(a);
  }

  int a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    m[a].push_back(b);
    m[b].push_back(a);
  }
  solve(N, M);
  return 0;
}
Problem p02678 (C++)

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

using namespace std;

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

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

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

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

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

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

  return 0;
}

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

using namespace std;

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

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

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

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

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

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

  return 0;
}
Problem p02678 (C++)

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

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

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

  vector<int> ans(N, 0);
  for (int i = 1; i < N; i++) {
    for (int v : graph[i]) {
      if (dist[v] == dist[i] - 1) {
        ans[i] = v + 1;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << ans[i] << endl;
  }
}


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

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

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

  vector<int> ans(N, 0);
  for (int i = 1; i < N; i++) {
    for (int v : graph[i]) {
      if (dist[v] == dist[i] - 1) {
        ans[i] = v + 1;
      }
    }
  }
  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 long long int lli;
#define MAX 300005
#define endl "\n"
vector<lli> adj[MAX + 5];
lli ans[MAX];
queue<lli> q;
lli N, M;
void BFS(int source, vector<lli> adj[MAX + 5], vector<bool> &visited) {
  visited[source] = true;
  q.push(source);
  while (!q.empty()) {
    source = q.front();
    visited[source] = true;
    for (auto i = adj[source].begin(); i != adj[source].end(); i++) {
      if (!visited[*i]) {
        BFS(*i, adj, visited);
        ans[*i] = source;
        q.push(*i);
      }
    }
    q.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> N >> M;
  for (lli i = 0; i < M; i++) {
    lli A, B;
    cin >> A >> B;
    adj[A].push_back(B);
    adj[B].push_back(A);
  }
  vector<bool> visited(N + 5, 0);
  BFS(1, adj, visited);
  // cout<<endl;
  bool flag = true;
  for (lli i = 2; i <= N; i++) {
    if (!ans[i])
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (lli i = 2; i <= N; i++)
      cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define MAX 300005
#define endl "\n"
vector<lli> adj[MAX + 5];
lli ans[MAX];
queue<lli> q;
lli N, M;
void BFS(int source, vector<lli> adj[MAX + 5], vector<bool> &visited) {
  visited[source] = true;
  q.push(source);
  while (!q.empty()) {
    source = q.front();
    visited[source] = true;
    for (auto i = adj[source].begin(); i != adj[source].end(); i++) {
      if (!visited[*i]) {
        visited[*i] = true;
        ans[*i] = source;
        q.push(*i);
      }
    }
    q.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> N >> M;
  for (lli i = 0; i < M; i++) {
    lli A, B;
    cin >> A >> B;
    adj[A].push_back(B);
    adj[B].push_back(A);
  }
  vector<bool> visited(N + 5, 0);
  BFS(1, adj, visited);
  // cout<<endl;
  bool flag = true;
  for (lli i = 2; i <= N; i++) {
    if (!ans[i])
      flag = false;
  }
  if (flag) {
    cout << "Yes" << endl;
    for (lli i = 2; i <= N; i++)
      cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }
}
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;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> prev(n, -1);
  vector<int> dist(n, INF);
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : g[v]) {
      if (dist[nv] == INF) {
        dist[nv] = dist[v] + 1;
        q.push(nv);
        prev[nv] = v;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (prev[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << prev[i] + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> prev(n, -1);
  vector<int> dist(n, INF);
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto nv : g[v]) {
      if (dist[nv] == INF) {
        dist[nv] = dist[v] + 1;
        q.push(nv);
        prev[nv] = v;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (prev[i] == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << prev[i] + 1 << endl;
  }
}
Problem p02678 (C++)

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

const ll N = 1e5;

ll n, m, cnt, a;
vector<int> g[N];
ll used[N], p[N];
string s;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  queue<int> q;
  q.push(1);
  p[1] = 1;
  used[1] = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i];
      if (!used[to]) {
        used[to]++;
        q.push(to);
        p[to] = (v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (!used[i])
      return cout << "No", 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << p[i] << "\n";
  }
}


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

const ll N = 2e6 + 7;

ll n, m, cnt, a;
vector<int> g[N];
ll used[N], p[N];
string s;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  queue<int> q;
  q.push(1);
  p[1] = 1;
  used[1] = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i];
      if (!used[to]) {
        used[to]++;
        q.push(to);
        p[to] = (v);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (!used[i])
      return cout << "No", 0;
  }
  cout << "Yes" << endl;
  for (int i = 2; i <= n; i++) {
    cout << p[i] << "\n";
  }
}