alexjercan/bugnet huggingface dataset:
Problem p02677 (C++) — buggy submission, verdict: 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;
}

Error: -6
terminate called after throwing an instance of 'std::__ios_failure'
  what():  basic_ios::clear: iostream error


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, verdict: Runtime Error

#include <cmath>
#include <iomanip>
#include <iostream>

struct Vector2 {
  Vector2(double y) : _x(0), _y(y), _length(y) {}
  double _x, _y, _length;

  void Rotate(double rad) {
    _y = std::cos(rad) * _length;
    _x = std::sin(rad) * _length;
  }

  void Output() { std::cout << _x << " : " << _y << std::endl; }
};

int main() {
  double A, B, H, M;
  std::cin >> A >> B >> H >> M;

  Vector2 vecA(A);
  Vector2 vecB(B);

  double radH = ((H + M / 60.0) / 12.0) * 2 * M_PI;
  double radM = (M / 60.0) * 2 * M_PI;

  vecA.Rotate(radH);
  vecB.Rotate(radM);

  auto diffX = (vecA._x - vecB._x);
  auto diffY = (vecA._y - vecB._y);
  auto distance = sqrt(diffX * diffX + diffY * diffY);
  std::cout << std::fixed << std::setprecision(9) << distance << std::endl;
}


Error: WA

Fixed submission:

#include <cmath>
#include <iostream>

struct Vector2 {
  Vector2(double y) : _x(0), _y(y), _length(y) {}
  double _x, _y, _length;

  double Rotate(double rad) {
    _y = std::cos(rad) * _length;
    _x = std::sin(rad) * _length;
  }

  void Output() { std::cout << _x << " : " << _y << std::endl; }
};

int main() {
  double A, B, H, M;
  std::cin >> A >> B >> H >> M;

  Vector2 vecA(A);
  Vector2 vecB(B);

  double radH = ((H + (M / 60.0)) / 12.0) * 2 * M_PI;
  double radM = (M / 60.0) * 2 * M_PI;

  vecA.Rotate(radH);
  vecB.Rotate(radM);

  auto distance = sqrt((vecA._x - vecB._x) * (vecA._x - vecB._x) +
                       (vecA._y - vecB._y) * (vecA._y - vecB._y));
  std::cout << distance << std::endl;
}
Problem p02677 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int a, b;
  int h, m;
  cin >> a >> b >> h >> m;
  long double theta;

  theta = 2.0 * M_PI *
          ((long double)h / 12.0 + ((long double)m / 60.0) / 12.0 -
           (long double)m / 60.0);

  long double c = sqrtl((long double)(a * a + b * b) -
                        (long double)(2.0 * a * b) * cosl(theta));

  printf("%20.20Lf\\n", c);

  return 0;
}

Error: 9

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  double a, b;
  int h, m;
  // static const double pi = 3.141592653589793;
  cin >> a >> b >> h >> m;
  double theta;
  if (m == 0) {
    theta = abs(30 * (h)-6 * m);
  } else {
    theta = abs(30 * (h) + 30 * m / 60 - 6 * m);
  }
  printf("theta %f\\n", theta);
  double o = min(theta, 360 - theta);

  printf("theta %f\\n", o);

  double c = sqrt(a * a + b * b - 2 * a * b * cos(2 * M_PI * o / 360));

  printf("%f\\n", c);

  return 9;
}
Problem p02677 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;

typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

// defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define prev _prev
#define y0 _y0
#define kill _kill
#define MAX 10000
#define pie 3.1415926535898

ll solve(ll t) {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ld hm = 0.5 * (c * 60 + d);
  ld mm = 6 * d;
  hm = (pie * hm) / 180.0;
  mm = (pie * mm) / 180.0;
  ld ans = (a * cos(hm) - b * cos(mm)) * (a * cos(hm) - b * cos(mm)) +
           (a * sin(hm) - b * sin(mm)) * (a * sin(hm) - b * sin(mm));
  ans = sqrt(ans);
  cout << fixed << setprecision(20) << ans;
  if (t > 0)
    cout << "\\n";
  return 0;
}

int32_t main() {
#ifdef ONPC
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve(t);
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;

typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

// defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define prev _prev
#define y0 _y0
#define kill _kill
#define MAX 10000
#define pie 3.1415926535898

ll solve(ll t) {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ld hm = 0.5 * (c * 60 + d);
  ld mm = 6 * d;
  hm = (pie * hm) / 180.0;
  mm = (pie * mm) / 180.0;
  ld ans = (a * cos(hm) - b * cos(mm)) * (a * cos(hm) - b * cos(mm)) +
           (a * sin(hm) - b * sin(mm)) * (a * sin(hm) - b * sin(mm));
  ans = sqrt(ans);
  cout << fixed << setprecision(20) << ans;
  if (t > 0)
    cout << "\\n";
}

int32_t main() {
#ifdef ONPC
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve(t);
  }

  return 0;
}
Problem p02677 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
int a, b, h, m;
double szj, fzj, j;
int main() {
  scanf("%d%d%d%d", &a, &b, &h, &m);
  szj = (h * 60 + m) * 0.5;
  fzj = 6 * m;
  j = fabs(szj - fzj);
  if (j > 180)
    j = 360 - j;
  printf("%.20f", sqrt(a * a + b * b - 2 * cos(j * M_PI / 180) * a * b));
  return 0;
}

Error: -11

Fixed submission:

#include <cmath>
#include <cstdio>
int a, b, h, m;
double szj, fzj, j;
int main() {
  scanf("%d%d%d%d", a, b, h, m);
  szj = (h * 60 + m) * 0.5;
  fzj = 6 * m;
  j = fabs(szj - fzj);
  if (j > 180)
    j = 360 - j;
  printf("%20f", sqrt(a * a + b * b - 2 * cos(j * M_PI / 180) * a * b));
  return 0;
}
Problem p02677 (C++) — buggy submission, verdict: 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;
}

Error: TLE

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 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

void solve() {
  long double a, b, h, m;
  cin >> a >> b >> h >> m;
  long double x = h * 30 + m * 0.5;
  long double y = m * 6.0;
  long double z = abs(x - y);
  if (z > 180)
    z = 360.0 - z;
  assert(z >= 0);
  long double ans = sqrtl(
      a * a + b * b - a * b * 2.0 * cos((z * acos((long double)-1.0)) / 180.0));
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

void solve() {
  long double a, b, h, m;
  cin >> a >> b >> h >> m;
  long double x = h * 30.0 + m * 0.5;
  long double y = m * 6.0;
  long double z = x - y;
  if (z > 180)
    z -= 180.0;
  else if (z < 0) {
    z += 180.0;
  }
  assert(z >= 0);
  long double ans = sqrtl(
      a * a + b * b - a * b * 2.0 * cos((z * acos((long double)-1.0)) / 180.0));
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p02677 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll double
#define endl '\\n'

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  ll pi = acos(-1);
  ll min = m * 6;
  ll hr = h * 30 + m / 2;
  ll angle = fabs(min - hr);
  if (angle > 180)
    angle = 360 - angle;
  ll val = pi / 180.0;
  ll res = cos(val * angle);
  ll ans = sqrt(a * a + b * b - res * 2 * a * b);
  cout << fixed << setprecision(10) << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define Mod 998244353

using namespace std;

void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
double cal_cos(double n, double a, double b) {
  double accuracy = 0.0000000001, x1, denominator, cosx, cosval;

  // Converting degrees to radian
  double pi = acos(-1);
  n = n * (pi / 180.0);

  x1 = 1;

  // maps the sum along the series
  cosx = x1;

  // holds the actual value of sin(n)
  cosval = cos(n);
  int i = 1;
  do {
    denominator = 2 * i * (2 * i - 1);
    x1 = -x1 * n * n / denominator;
    cosx = cosx + x1;
    i = i + 1;
  } while (accuracy <= fabs(cosval - cosx));
  // cout<<cosx;
  double ans = sqrt(a * a * 1.0 + b * b * 1.0 - cosx * 2 * a * b * 1.0);
  cout << fixed << setprecision(10) << ans;
}
int main() {
  fast();
  double a, b, h, m;
  cin >> a >> b >> h >> m;
  double pi = acos(-1);
  ll min = m * 6;
  ll hr = h * 30 + m / 2;
  double deg = (float)abs(min * 1.0 - hr * 1.0);

  if (deg > 180)
    deg = 360 - deg;
  // cout<<deg;

  cal_cos(deg, a, b);

  //    printf("%lf",res);
}
Problem p02677 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  double a, b, h, m, r;
  cin >> a >> b >> h >> m;
  h += m / 60;
  h /= 12;
  m /= 60;
  if (h >= 0.5 && m >= 0.5) {
    h -= 0.5;
    m -= 0.5;
    r = h - m;
    if (r < 0)
      r = -r;

  } else if (h > 0.5 && m < 0.5) {
    h -= 0.5;
    r = h - m;
    if (r < 0)
      r = -r;
    r = 0.5 - r;
  } else if (h < 0.5 && m > 0.5) {
    m -= 0.5;
    r = h - m;
    if (r < 0)
      r = -r;
    r = 0.5 - r;
  } else {
    r = h - m;
    if (r < 0)
      r = -r;
  }

  //	cout<<h<<m<<endl;
  //	if(h>m&&((h<0.5&&m<0.5)||(h>0.5&&m>=0.5)))
  //	{
  //		r=h-m;
  //
  //	}
  //	else
  //	if(h<=m&&((h<0.5&&m<=0.5)||(h>0.5&&m>0.5)))
  //	{
  //		r=m-h;
  //	}
  //	else
  //	if(h>m&&(h>0.5&&m<=0.5))
  //	{
  //		r=m+(1-h);
  //	}
  //	else
  //	if(h<m&&(h<0.5&&m>=0.5))
  //	{
  //		r=h+(1-m);
  //	}

  printf("%.20lf\\n", sqrt(a * a + b * b - 2 * a * b * cos(r * acos(-1) * 2)));
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  double a, b, h, m, r;
  cin >> a >> b >> h >> m;
  h += m / 60;
  h /= 12;
  m /= 60;
  if (h >= 0.5)
    h -= 0.5;
  if (m >= 0.5)
    m -= 0.5;

  r = fabs(h - m);

  //	cout<<h<<m<<endl;
  //	if(h>m&&((h<0.5&&m<0.5)||(h>0.5&&m>=0.5)))
  //	{
  //		r=h-m;
  //
  //	}
  //	else
  //	if(h<=m&&((h<0.5&&m<=0.5)||(h>0.5&&m>0.5)))
  //	{
  //		r=m-h;
  //	}
  //	else
  //	if(h>m&&(h>0.5&&m<=0.5))
  //	{
  //		r=m+(1-h);
  //	}
  //	else
  //	if(h<m&&(h<0.5&&m>=0.5))
  //	{
  //		r=h+(1-m);
  //	}

  printf("%.20lf\\n", sqrt(a * a + b * b - 2 * a * b * cos(r * acos(-1) * 2)));
  main();
}
Problem p02677 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
int a, b, h, m;
double szj, fzj, j, ans;
int main() {
  scanf("%d%d%d%d", &a, &b, &h, &m);
  szj = (h * 60 + m) * 0.5;
  fzj = m * 6;
  j = fabs(szj - fzj);
  if (j > 180)
    j = 360 - j;
  ans = sqrt(a * a + b * b - 2 * a * b * cos(j * M_PI / 180));
  printf("%.20f", ans);
  return 0;
}

Error: TLE

Fixed submission:

int main() {
  while (1)
    ;
  return 0;
}
Problem p02677 (C++) — buggy submission, verdict: 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;
}

Error: TLE

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, verdict: Runtime Error

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

int main() {
  float A, B, H, M;
  cin >> A >> B >> H >> M;

  float angle;
  float HtoM = H * 60;
  float angleH = (HtoM + M) * 0.5;
  float angleM = M * 6;

  float diff = abs(angleH - angleM);

  if (diff > 180) {
    angle = 360 - diff;
  } else {
    angle = diff;
  }
  cout << fixed << setprecision(9)
       << sqrt(pow(A, 2) + pow(B, 2) - 2 * A * B * cos(M_PI * angle / 180))
       << endl;
}

Error: 5

Fixed submission:

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

double calAngle(double H, double M) {
  // てっぺんを0度として
  double angle;
  double angleH = H * (360 / 12);
  double angleM = M * (360 / 60);

  double diff = abs(angleH - angleM);

  if (diff > 180) {
    angle = 360 - diff;
  } else {
    angle = diff;
  }
  return angle;
}

int main() {
  int A, B, H, M;
  cin >> A >> B >> H >> M;

  double angle = calAngle(H, M);

  return sqrt(pow(A, 2.0) + pow(B, 2.0) - 2 * A * B * cos(angle));
}
Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
using lint = long long;
vector<vector<int>> G;
vector<int> field;
vector<bool> visited;
int n, m;

void bfs() {
  queue<int> que;
  que.push(0);
  visited[0] = true;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (auto next : G[now]) {
      if (!visited[next]) {
        que.push(next);
        field[next] = now;
        visited[next] = true;
      }
    }
  }
}

void addEdge(vector<int> edges[], int u, int v) {
  edges[u].push_back(v);
  edges[v].push_back(u);
}

int main() {
  cin >> n >> m;
  vector<lint> a(m), b(m);
  G.resize(n);
  field.resize(n);
  visited.resize(n, false);

  vector<int> v;

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

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

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long

bool BFS(vector<int> adj[], int src, int dest, int v, int pred[], int dist[]) {

  list<int> queue;

  bool visited[v];

  for (int i = 0; i < v; i++) {
    visited[i] = false;
    dist[i] = INT_MAX;
    pred[i] = -1;
  }

  visited[src] = true;
  dist[src] = 0;
  queue.push_back(src);

  // standard BFS algorithm
  while (!queue.empty()) {
    int u = queue.front();
    queue.pop_front();
    for (int i = 0; i < adj[u].size(); i++) {
      if (visited[adj[u][i]] == false) {
        visited[adj[u][i]] = true;
        dist[adj[u][i]] = dist[u] + 1;
        pred[adj[u][i]] = u;
        queue.push_back(adj[u][i]);

        // We stop BFS when we find
        // destination.
        if (adj[u][i] == dest)
          return true;
      }
    }
  }

  return false;
}

int printShortestDistance(vector<int> adj[], int s, int dest, int v) {
  int pred[v], dist[v];

  if (BFS(adj, s, dest, v, pred, dist) == false) {
    return -1;
  }

  vector<int> path;
  int crawl = dest;
  path.push_back(crawl);
  while (pred[crawl] != -1) {
    path.push_back(pred[crawl]);
    crawl = pred[crawl];
  }
  return path[path.size() - 2];
}

void addEdge(vector<int> edges[], int u, int v) {
  edges[u].push_back(v);
  edges[v].push_back(u);
}

int main() {
  int n, e;
  cin >> n >> e;

  vector<int> edges[n];
  int flag = 1;
  vector<int> v;

  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    addEdge(edges, a - 1, b - 1);
  }

  for (int i = 1; i < n; i++) {
    int x = printShortestDistance(edges, i, 0, e);
    if (x == -1) {
      flag = 0;
      break;
    }

    else
      v.push_back(x + 1);
  }

  if (flag == 0)
    cout << "No";
  else {
    cout << "Yes \\n";
    for (int i = 0; i < v.size(); i++)
      cout << v[i] << "\\n";
  }

  return 0;
}
Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \\
  for (i = 0; i < end; i++)                                                    \\
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\\n", i0, i1)

using namespace std;
typedef long long int ll;
const int inf = 100000000;
const int mod = 1000000007;
const int nil = -1;
int i, n, m, a, b, ans;
int nxt[100001];
bool con[100001];
vector<int> adj[100001];

void search() {
  queue<int> toVisit;
  toVisit.push(1);
  while (toVisit.size() > 0) {
    int parent = toVisit.front();
    toVisit.pop();
    for (auto it = adj[parent].begin(); it != adj[parent].end(); it++) {
      if (!con[*it]) {
        nxt[*it] = parent;
        con[*it] = true;
        toVisit.push(*it);
      }
    }
  }
}

int main() {
  scanf(" %d %d", &n, &m);
  rep(0, m) {
    scanf(" %d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  con[1] = true;
  rep(2, n + 1) con[i] = false;
  search();

  printf("Yes\\n");
  rep(2, n + 1) printf("%d\\n", nxt[i]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \\
  for (i = 0; i < end; i++)                                                    \\
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\\n", i0, i1)

using namespace std;
typedef long long int ll;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
int i, n, m, a, b, ans;
int d[100001], nxt[100001];
vector<int> adj[100001];

void search(int dist, int parent) {
  if (dist > n)
    return;
  for (auto it = adj[parent].begin(); it != adj[parent].end(); it++) {
    if (d[*it] > dist) {
      d[*it] = dist;
      nxt[*it] = parent;
    }
  }
  for (auto it = adj[parent].begin(); it != adj[parent].end(); it++) {
    search(dist + 1, *it);
  }
}

int main() {
  scanf(" %d %d", &n, &m);
  rep(0, m) {
    scanf(" %d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  d[1] = 0;
  rep(2, n + 1) d[i] = inf;
  search(1, 1);

  printf("Yes\\n");
  rep(2, n + 1) printf("%d\\n", nxt[i]);
}