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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007
#define irep(i, n) for (int i = 0; i < n; i++)
#define irep2(i, a, n) for (int i = a; i <= n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define lrep2(i, a, n) for (long long i = a; i <= n; i++)

typedef long long ll;
typedef std::vector<char> v_char;
typedef std::vector<int> v_int;
typedef std::vector<std::vector<int>> v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll>> v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<std::vector<std::string>> v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<std::vector<bool>> v2_bool;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, int> pii;

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

using namespace std;

ll g(ll n, ll x) {
  if (n == 1) {
    return 0;
  }

  ll i = n / x;

  return i + g(i, x);
}

ll g2(ll n, ll x) {
  if (n % 2) {
    return 0;
  } else {
    ll i = n / 2;
    return 2 / x + g(i, x);
  }
}

int main(void) {
  ll n;
  cin >> n;

  // cout << min(g2(n, 2), g2(n, 5));
  cout << g2(n, 5);
  return 0;
}
/** atcodr **/


Fixed submission:
#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007
#define irep(i, n) for (int i = 0; i < n; i++)
#define irep2(i, a, n) for (int i = a; i <= n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define lrep2(i, a, n) for (long long i = a; i <= n; i++)

typedef long long ll;
typedef std::vector<char> v_char;
typedef std::vector<int> v_int;
typedef std::vector<std::vector<int>> v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll>> v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<std::vector<std::string>> v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<std::vector<bool>> v2_bool;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, int> pii;

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

using namespace std;

ll g(ll n, ll x) {
  if (n == 0) {
    return 0;
  }

  ll i = n / x;

  return i + g(i, x);
}

ll g2(ll n, ll x) {
  if (n % 2) {
    return 0;
  } else {
    ll i = n / 2;
    return 2 / x + g(i, x);
  }
}

int main(void) {
  ll n;
  cin >> n;

  // cout << min(g2(n, 2), g2(n, 5));
  cout << g2(n, 5);
  return 0;
}
/** atcodr **/
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////

int main() {
  ll N;
  cin >> N;
  if (N % 2 == 1)
    cout << 0 << endl;
  else {
    ll ans = 0;
    ll n = N;
    if (N % 10 == 0) {
      while (N % 10 == 0) {
        ans++;
        N /= 5;
      }
    }
    ll key = 10;
    while (key <= n - 2) {
      ans += (n - 2) / key;
      key *= 5;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////

int main() {
  ll N;
  cin >> N;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (N % 2 == 1)
    cout << 0 << endl;
  else {
    ll ans = 0;
    ll n = N;
    if (N % 10 == 0) {
      while (N % 10 == 0) {
        ans++;
        N /= 5;
      }
    }
    ll key = 10;
    while (key <= n - 2) {
      ans += (n - 2) / key;
      key *= 5;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = (0); i < (n); ++i)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define show(x) cout << #x << " = " << (x) << endl;
#define show2(x, y)                                                            \
  cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define uni(x) x.erase(unique(rng(x)), x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = 1 << 30;
const ll INFLL = 1LL << 60;
// cout << fixed << setprecision(10);

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

ll g1(ll n, int p) {
  if (n == 0)
    return 0;
  return n / p + g1(n / p, p);
}
ll g2(ll n, int p) {
  if (n % 2 == 1)
    return g1(n, p) - g2(n - 1, p);
  ll res = g1(n / 2, p);
  if (p == 2)
    res += 2 / n;
  return res;
}

int main() {
  ll n;
  cin >> n;
  ll ans = min(g2(n, 2), g2(n, 5));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = (0); i < (n); ++i)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define show(x) cout << #x << " = " << (x) << endl;
#define show2(x, y)                                                            \
  cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define uni(x) x.erase(unique(rng(x)), x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = 1 << 30;
const ll INFLL = 1LL << 60;
// cout << fixed << setprecision(10);

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

ll g1(ll n, int p) {
  if (n == 0)
    return 0;
  return n / p + g1(n / p, p);
}
ll g2(ll n, int p) {
  if (n % 2 == 1)
    return g1(n, p) - g2(n - 1, p);
  ll res = g1(n / 2, p);
  if (p == 2)
    res += n / 2;
  return res;
}

int main() {
  ll n;
  cin >> n;
  ll ans = min(g2(n, 2), g2(n, 5));
  cout << ans << endl;
  return 0;
}
Problem p02833 (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 long long ll;

int main() {
  ll N;
  cin >> N;
  if (N % 2 == 1) {
    puts(0);
    return 0;
  } else {
    ll x = 5;
    ll ans = 0;
    while (N >= x) {
      ans += N / x / 2;
      x *= 5;
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main() {
  ll N;
  cin >> N;
  if (N % 2 == 1) {
    cout << 0 << endl;
    return 0;
  } else {
    ll x = 5;
    ll ans = 0;
    while (N >= x) {
      ans += N / x / 2;
      x *= 5;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

long long int solve(long long int n) {
  long long int ans = 0;
  if (n % 2 != 0) {
    return 0;
  }

  for (int i = 10; i <= n; i *= 5) {
    ans += n / i;
  }

  return ans;
}

int main() {
  long long int n, ans;
  cin >> n;
  ans = solve(n);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

long long int solve(long long int n) {
  long long int ans = 0;
  if (n % 2 != 0) {
    return 0;
  }

  for (long long int i = 10; i <= n; i *= 5) {
    ans += n / i;
  }

  return ans;
}

int main() {
  long long int n, ans;
  cin >> n;
  ans = solve(n);
  cout << ans << endl;
  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, i, nr, x, aux;
int main() {
  cin >> n;
  if (n % (long long)2 == 1) {
    cout << 0;
    return 1;
  }
  x = 2;
  while (x * 5 <= n) {
    x = x * 5;
    nr = nr + n / x;
  }
  cout << nr;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, i, nr, x, aux;
int main() {
  cin >> n;
  if (n % 2 == 0)
    for (i = 5; i <= n; i = i * 5)
      nr = nr + n / (i * 2);
  cout << nr;
  return 0;
}
Problem p02833 (C++)

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

typedef long long ll;
typedef long double ld;
typedef string str;

#define f(i, begin, end)                                                       \
  for (auto i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end));   \
       i += 1 - 2 * ((begin) > (end)))
#define trav(v) for (auto &x : v)
#define dis(v) trav(v) cout << x << " ";
#define deb(x) cerr << #x << ":" << x << "\n"
#define debv(v) trav(v) cerr << x << " ";
#define all(v) v.begin(), v.end()
#define nl cout << "\n"

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll t = 1;
  // cin >> t;
  while (t--) {
    ll i, j, k;
    ll n, ans = 0;
    cin >> n;
    k = 10;
    if (!(n & 1))
      while (n / k > 0) {
        ans += (n / k);
        k *= 5;
      }
    cout << ans << "\n";
  }
  return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef string str;

#define f(i, begin, end)                                                       \
  for (auto i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end));   \
       i += 1 - 2 * ((begin) > (end)))
#define trav(v) for (auto &x : v)
#define dis(v) trav(v) cout << x << " ";
#define deb(x) cerr << #x << ":" << x << "\n"
#define debv(v) trav(v) cerr << x << " ";
#define all(v) v.begin(), v.end()
#define nl cout << "\n"

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll t = 1;
  // cin >> t;
  while (t--) {
    ll i, j, k;
    ll n, ans = 0;
    cin >> n;
    k = 10;
    if (!(n & 1))
      while (n / k > 0) {
        ans += (n / k);
        k *= 5;
      }
    cout << ans << "\n";
  }
  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repr(i, n) for (int i = (n)-1; 0 <= i; --i)
#define each(e, v) for (auto &&e : (v))
#define all(v) begin(v), end(v)
#define dump(x) cerr << #x << ": " << (x) << endl
using namespace std;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
void yes() {
  cout << "Yes" << endl;
  exit(0);
}
void no() {
  cout << "No" << endl;
  exit(0);
}

ll cnt2(ll n) {
  if (n % 2LL != 0)
    return 0;
  return (n / 2LL) + cnt2(n / 2LL);
}

ll cnt5(ll n) {
  if (n < 5LL)
    return 0;
  return (n / 10LL) + cnt5(n / 5LL);
}

int main() {
  ll n;
  cin >> n;
  cout << min(cnt2(n), cnt5(n)) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repr(i, n) for (int i = (n)-1; 0 <= i; --i)
#define each(e, v) for (auto &&e : (v))
#define all(v) begin(v), end(v)
#define dump(x) cerr << #x << ": " << (x) << endl
using namespace std;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
void yes() {
  cout << "Yes" << endl;
  exit(0);
}
void no() {
  cout << "No" << endl;
  exit(0);
}

ll cnt2(ll n) {
  if (n == 0LL)
    return 0;
  if (n % 2LL != 0)
    return 0;
  return (n / 2LL) + cnt2(n / 2LL);
}

ll cnt5(ll n) {
  if (n < 5LL)
    return 0;
  return (n / 10LL) + cnt5(n / 5LL);
}

int main() {
  ll n;
  cin >> n;
  cout << min(cnt2(n), cnt5(n)) << endl;
  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Runtime Error):
//
// Created by Mahmoud Rashad on 11/27/19.
//

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> ii;

long long count_P_in_nfact(long long n, long long p) {
  long long res = 0;
  while (n) {
    n /= p;
    res += n;
  }
  return res;
}
long long count_5_in_nfact(long long n, long long p) {
  long long res = 0;
  while (n) {
    n /= p;
    res += n / 2;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
//    freopen("output.out", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  long long n;
  cin >> n;

  if (n & 1) {
    cout << 0 << '\n';
    return 0;
  }

  cout << min(count_5_in_nfact(n, 5), count_P_in_nfact(n, 2)) << '\n';

  return 0;
}

Fixed submission:
//
// Created by Mahmoud Rashad on 11/27/19.
//

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> ii;

long long count_P_in_nfact(long long n, long long p) {
  long long res = 0;
  while (n) {
    n /= p;
    res += n;
  }
  return res;
}
long long count_5_in_nfact(long long n, long long p) {
  long long res = 0;
  while (n) {
    n /= p;
    res += n / 2;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "r", stdin);
//    freopen("output.out", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  long long n;
  cin >> n;

  if (n & 1) {
    cout << 0 << '\n';
    return 0;
  }

  cout << min(count_5_in_nfact(n, 5), count_P_in_nfact(n, 2)) << '\n';

  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
long cntfive(long n) {
  long ans = 0;
  while (n % 5 == 0) {
    ans++;
    n /= 5;
  }
  return ans;
}
int main(void) {
  long n;
  cin >> n;
  long ans = 0;
  if (n % 2 == 0) {
    for (long i = n; i >= 10; i -= 2) {
      ans += cntfive(i);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
long cntfive(long n) {
  long ans = 0;
  while (n % 5 == 0) {
    ans++;
    n /= 5;
  }
  return ans;
}
int main(void) {
  long n;
  cin >> n;
  long ans = 0;
  if (n % 2 == 0) {
    for (long i = 10; i <= n; i *= 5) {
      ans += n / i;
    }
  }
  cout << ans << endl;
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int main() {
  long long n;
  cin >> n;
  if (n % 2 != 0)
    cout << "0";
  else {
    for (int i = 10; i <= n; i *= 5) {
      ans += (n / i);
    }
    cout << ans;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int main() {
  long long n;
  cin >> n;
  if (n % 2 != 0)
    cout << "0";
  else {
    for (long long i = 10; i <= n; i *= 5) {
      ans += (n / i);
    }
    cout << ans;
  }
  return 0;
}
Problem p02833 (Python)

Buggy submission (status: Runtime Error):
import math


def main():
    N = int(input())

    if N % 2 == 0:
        B = N // 2
        n = math.floor(math.log(B) / math.log(5))

        ans = 0
        for i in range(1, n + 1):
            ans += B // (5**i)

    else:
        ans = 0

    print(int(ans))


main()


Fixed submission:
import math


def main():
    N = int(input())

    if N == 0 or N == 1:
        ans = 0

    elif N % 2 == 0:
        B = N // 2
        n = math.floor(math.log(B) / math.log(5))

        ans = 0
        for i in range(1, n + 1):
            ans += B // (5**i)

    else:
        ans = 0

    print(int(ans))


main()
Problem p02833 (Python)

Buggy submission (status: Runtime Error):
import math

N = int(input())

ans = 0

if N % 2 != 0:
    pass
else:
    max_pow_five = int(math.log(N, 5))
    for i in range(1, max_pow_five + 1):
        ans += N // ((5**i) * 2)

print(ans)


Fixed submission:
import math

N = int(input())

ans = 0

if N % 2 != 0 or N == 0:
    pass
else:
    max_pow_five = int(math.log(N, 5))
    for i in range(1, max_pow_five + 1):
        ans += N // ((5**i) * 2)

print(ans)
Problem p02833 (Python)

Buggy submission (status: Runtime Error):
import math


def run(N):
    ans = 0
    if N % 2 == 0:
        for i in range(int(math.log(N, 5))):
            ans += N // (10 * (5**i))
    return ans


def main():
    N = int(input())
    print(run(N))


if __name__ == "__main__":
    main()


Fixed submission:
import math


def run(N):
    if N == 0:
        return 0
    ans = 0
    if N % 2 == 0:
        for i in range(int(math.log(N, 5))):
            ans += N // (10 * (5**i))
    return ans


def main():
    N = int(input())
    print(run(N))


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

Buggy submission (status: Runtime Error):
N = int(input())
if N % 2 == 1:
    print(0)
else:
    ans = 0
    for i in range(1, 30):
        ans += (N // 2) // (5**i)

print(ans)


Fixed submission:
N = int(input())
if N % 2 == 1:
    print(0)
else:
    ans = 0
    for i in range(1, 30):
        ans += (N // 2) // (5**i)
    print(ans)
Problem p02833 (Python)

Buggy submission (status: Runtime Error):
import math

N = int(input())
if N % 2 == 1:
    print(0)
else:
    div10 = N // 10
    sum_val = div10
    count_5 = math.floor(math.log(div10, 5))
    for i in range(count_5):
        sum_val += div10 // pow(5, i + 1)
    print(sum_val)


Fixed submission:
import math

N = int(input())
if N % 2 == 1:
    print(0)
else:
    div10 = N // 10
    sum_val = div10
    if sum_val > 0:
        count_5 = math.floor(math.log(div10, 5))
        for i in range(count_5):
            sum_val += div10 // pow(5, i + 1)
    print(sum_val)
Problem p02833 (C++)

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

int main() {
  long long a;
  cin >> a;
  if (a % 2 != 0) {
    cout << 0 << endl;
  } else {
    long long c = 0;
    long long d = 5;
    long long j = a;
    while (j / d > 0) {
      if (j % (d * 2) == 0) {
        c += j / (d * 2);
        d *= 5;
        j = a;
      } else {
        j -= 2;
      }
    }
    cout << min(a / 2, c) << endl;
  }
}

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

int main() {
  long long a;
  cin >> a;
  if (a % 2 != 0) {
    cout << 0 << endl;
  } else {
    long long c = 0;
    long long d = 5;
    while (a / d > 0) {
      c += a / (d * 2);
      d *= 5;
    }
    cout << min(a / 2, c) << endl;
  }
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// ifstream cin("lineup.in");
// ofstream cout("lineup.out");
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
int main() {
  IOS;
  long long n;
  cin >> n;
  if (n % 2) {
    cout << 0 << endl;
  } else {
    n /= 2;
    long long ans = 0;
    while (n != 1) {
      ans += n / 5;
      n /= 5;
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// ifstream cin("lineup.in");
// ofstream cout("lineup.out");
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
int main() {
  IOS;
  long long n;
  cin >> n;
  if (n % 2) {
    cout << 0 << endl;
  } else {
    n /= 2;
    long long ans = 0;
    while (n > 1) {
      ans += n / 5;
      n /= 5;
    }
    cout << ans << endl;
  }
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Vec = vector<int>;

ll five_count(ll n) {
  ll c = 0;
  ll f = 5;
  for (; f <= n; f *= 5) {
    if (!(n % f))
      c++;
    else
      break;
  }
  return (c);
}

int main(void) {
  ll n;
  cin >> n;
  if (n % 2) {
    cout << 0 << endl;
    return (0);
  }
  n /= 2;
  ll c = 0;
  for (ll i = 1; i <= n; i++)
    c += five_count(i);
  cout << c << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Vec = vector<int>;

ll five_count(ll n) {
  ll c = 0;
  ll f = 5;
  for (; f <= n; f *= 5) {
    if (!(n % f))
      c++;
    else
      break;
  }
  return (c);
}

int main(void) {
  ll n;
  cin >> n;
  if (n % 2) {
    cout << 0 << endl;
    return (0);
  }
  n /= 2;
  ll c = 0;

  ll five = 1;
  while (n >= 5) {
    c += n / 5;
    n /= 5;
    five++;
  }

  cout << c << endl;
}
Problem p02833 (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;

ll f_2(ll n) {
  if (n == 1)
    return 0;
  return f_2(n / 2) + n / 2;
}

ll f_5(ll n) {
  if (n <= 1)
    return 0;
  return f_5(n / 5) + n / 5;
}

signed main(void) {
  ll n, ans;
  cin >> n;
  ans = 0;
  if (n % 2 == 0) {
    ans = min(f_2(n), f_5(n / 2));
  }
  cout << ans << endl;
  return 0;
}


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

ll f_2(ll n) {
  if (n == 1)
    return 0;
  return f_2(n / 2) + n / 2;
}

ll f_5(ll n) {
  if (n <= 1)
    return 0;
  return f_5(n / 5) + n / 5;
}

signed main(void) {
  ll n, ans;
  cin >> n;
  ans = 0;
  if (n % 2 == 0 && n != 0) {
    ans = min(f_2(n), f_5(n / 2));
  }
  cout << ans << endl;
  return 0;
}
Problem p02833 (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 ll long long
#define all(x) (x).begin(), (x).end()
#define int long long

signed main() {
  int n;
  cin >> n;
  // 奇数なら約数に2がないので0
  if (n % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  // 偶数なら明らかに約数は5の数が少ないので5の数を調べれば良い
  int res = 0;
  while (n > 10) {
    n -= 10;
    res++;
  }

  cout << res << endl;
}


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

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define int long long

signed main() {
  int n;
  cin >> n;
  // 奇数なら約数に2がないので0
  if (n % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  // 偶数なら明らかに約数は5の数が少ないので5の数を調べれば良い
  int res = 0;
  int divide = 10;
  while (divide <= n) {
    res += n / divide;
    divide *= 5;
  }

  cout << res << endl;
}
Problem p02833 (C++)

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

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back

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

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

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

ll ans = 0;
ll x = 5;
unsigned long long doublefactorial(long long a) {

  while (x >= a) {
    ans++;
    x *= x;
  }

  if (a == 0 || a == 1)
    return 1;
  return a * doublefactorial(a - 2);
}

int main() {
  ll n;
  cin >> n;

  ll x = 5;
  ll sum = 0;
  ll sum2 = 0;
  if (n % 2 == 0) {
    while (x <= n / 2) {
      sum += n / 2 / x;
      x *= 5;
    }
    cout << sum << endl;
    return 0;
  } else {
    while (x <= n + 1) {
      sum += (n + 1) / x;
      x *= 5;
    }
    x = 5;
    while (x <= n / 2) {
      sum += n / 2 / x;
      x *= 5;
    }
    cout << sum / sum2 << endl;
  }

  return 0;
}

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

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back

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

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

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

ll ans = 0;
ll x = 5;
unsigned long long doublefactorial(long long a) {

  while (x >= a) {
    ans++;
    x *= x;
  }

  if (a == 0 || a == 1)
    return 1;
  return a * doublefactorial(a - 2);
}

int main() {
  ll n;
  cin >> n;

  ll x = 5;
  ll sum = 0;
  ll sum2 = 0;
  if (n % 2 == 0) {
    while (x <= n / 2) {
      sum += n / 2 / x;
      x *= 5;
    }
    cout << sum << endl;
    return 0;
  } else {
    cout << 0 << endl;
    return 0;
  }

  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
const ll mod = 5e18;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll N;
  cin >> N;
  ll ans = 0;
  if (N % 2) {
    cout << ans << endl;
    return 0;
  }
  ll jyuubai = 10;
  ll now = N;
  if (N == 1e18) {
    while (jyuubai <= N) {
      ans += N / jyuubai;
      if (jyuubai == 1e18)
        break;
      jyuubai *= 5;
    }
  } else {
    while (jyuubai <= N) {
      ans += N / jyuubai;
      if (jyuubai <= 1e17)
        jyuubai *= 5;
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
const ll mod = 5e18;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll N;
  cin >> N;
  ll ans = 0;
  if (N % 2) {
    cout << ans << endl;
    return 0;
  }
  ll jyuubai = 10;
  ll now = N;
  if (N == 1e18) {
    while (jyuubai <= N) {
      ans += N / jyuubai;
      if (jyuubai == 1e18)
        break;
      jyuubai *= 5;
    }
  } else {
    while (jyuubai <= N) {
      ans += N / jyuubai;
      jyuubai *= 5;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
long long ans = 0;
long long a;
int main() {
  cin >> a;
  if (a % 2 == 1) {
    cout << 0;
    return 0;
  } else {
    while (a >= 0) {
      a /= 10;
      ans += a;
    }
    cout << ans;
    return 0;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
long long ans = 0;
long long a;
int main() {
  cin >> a;
  if (a % 2 == 1) {
    cout << 0;
    return 0;
  } else {
    a /= 10;
    ans += a;
    while (a > 0) {
      a /= 5;
      ans += a;
    }
    cout << ans;
    return 0;
  }
}
Problem p02833 (C++)

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

using namespace std;
typedef long long ll;
typedef long double ld;
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)

// Write From This Line
int main() {
  ll n;
  cin >> n;
  if (n % 2) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  ll five = 10;
  while (five > 0) {
    ans += (n / five);
    five *= 5;
  }
  cout << ans << endl;
}


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

using namespace std;
typedef long long ll;
typedef long double ld;
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)

// Write From This Line
int main() {
  ll n;
  cin >> n;
  if (n % 2) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  n /= 2;
  ll five = 5;
  while (n) {
    ans += n / five;
    n /= 5;
  }
  cout << ans << endl;
}
Problem p02833 (C++)

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

int main() {
  int64_t N, count;
  cin >> N;
  count = 0;
  if (N % 2 != 0) {
    cout << count << endl;
  } else {
    for (int i = 10; i <= N; i = i * 10) {
      count += N / i;
    }
    cout << count << endl;
  }
}


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

int main() {
  int64_t N, count;
  cin >> N;
  count = 0;
  if (N % 2 != 0) {
    cout << count << endl;
  } else {
    for (int64_t i = 5; i <= N; i = i * 5) {
      count += N / (i * 2);
    }
    cout << count << endl;
  }
}
Problem p02833 (C++)

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

long long n, res = 0;

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

  if (n % 2) {
    cout << 0;
    return 0;
  }
  n /= 2;
  for (int i = 5; i <= n; i *= 5)
    res += n / i;
  cout << res;
}


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

long long n, res = 0;

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

  if (n % 2) {
    cout << 0;
    return 0;
  }
  n /= 2;
  for (long long i = 5; i <= n; i *= 5)
    res += n / i;
  cout << res;
}
Problem p02833 (C++)

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

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

  ll n;
  cin >> n;

  if (n % 2) {
    cout << 0 << endl;
    return 0;
  }

  n /= 2;
  ll most = 1;
  while (most <= n) {
    most *= 5;
  }

  most /= 5;

  ll res = 0;
  while (most != 1) {
    // cout << n/most << " " << n << " " << most << endl;
    res += n / most;
    most /= 5;
  }

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;

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

  ll n;
  cin >> n;

  if (n % 2 || n == 0) {
    cout << 0 << endl;
    return 0;
  }

  n /= 2;
  ll most = 1;
  while (most <= n) {
    most *= 5;
  }

  most /= 5;

  ll res = 0;
  while (most != 1) {
    // cout << n/most << " " << n << " " << most << endl;
    res += n / most;
    most /= 5;
  }

  cout << res << endl;

  return 0;
}
Problem p02833 (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 rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;

int main() {

  ll n;
  cin >> n;
  if (n % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  for (int i = 1; pow(5, i) <= n; i++) {
    long long l = (long double)ans + n / (pow((long double)5, i) * 2);
    long long r =
        (long double)ans + n / (long long)(pow((long double)5, i) * 2);
    assert(l == r);
    ans += n / (long long)(pow((long double)5, i) * 2);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;

int main() {

  ll n;
  cin >> n;
  if (n % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  for (int i = 1; pow(5, i) <= n; i++) {
    long long l = n / (pow((long double)5, i) * 2);
    long long r = n / (long long)(pow((long double)5, i) * 2);
    assert(l == r);
    ans += n / (long long)(pow((long double)5, i) * 2);
  }
  cout << ans << endl;
}
Problem p02834 (Python)

Buggy submission (status: Runtime Error):
n, u, v = map(int, input().split())
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1

g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    g[a].append(b)
    g[b].append(a)

INF = 1 << 30
t = [INF] * n
a = [INF] * n

t[u] = 0
a[v] = 0


def dfs(v, p, d):
    for nv in g[v]:
        if nv == p:
            continue
        if d[nv] == INF:
            d[nv] = d[v] + 1
            dfs(nv, v, d)


dfs(u, -1, t)
dfs(v, -1, a)

ans = 0
for i in range(n):
    if t[i] < a[i]:
        ans = max(ans, a[i] - 1)
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**6)

n, u, v = map(int, input().split())
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1

g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    g[a].append(b)
    g[b].append(a)

INF = 1 << 30
t = [INF] * n
a = [INF] * n

t[u] = 0
a[v] = 0


def dfs(v, p, d):
    for nv in g[v]:
        if nv == p:
            continue
        if d[nv] == INF:
            d[nv] = d[v] + 1
            dfs(nv, v, d)


dfs(u, -1, t)
dfs(v, -1, a)

ans = 0
for i in range(n):
    if t[i] < a[i]:
        ans = max(ans, a[i] - 1)
print(ans)
Problem p02834 (C++)

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

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const int MOD = 1e9 + 7;

vector<int> g[100100];
int dist[100100] = {};
int ans[100100] = {};

int tak, ao;

void dfs(int u, int pa, int d) {
  for (auto v : g[u]) {
    if (v == pa)
      continue;
    dfs(v, u, d + 1);
  }
  dist[u] = d;
}

void dfs2(int u, int pa, int d) {
  for (auto v : g[u]) {
    if (v == u)
      continue;
    if (d + 1 >= dist[v]) {
      continue;
    } else {
      dfs2(v, u, d + 1);
    }
  }
  ans[u] = dist[u];
}

int main() {
  int N;
  cin >> N >> tak >> ao;
  tak--;
  ao--;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(ao, -1, 0);
  dfs2(tak, -1, 0);
  int f_a = 0;
  for (int i = 0; i < N; i++) {
    f_a = max(ans[i], f_a);
  }
  cout << f_a - 1 << endl;
}

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

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const int MOD = 1e9 + 7;

vector<int> g[100100];
int dist[100100] = {};
int ans[100100] = {};

int tak, ao;

void dfs(int u, int pa, int d) {
  for (auto v : g[u]) {
    if (v == pa)
      continue;
    dfs(v, u, d + 1);
  }
  dist[u] = d;
}

void dfs2(int u, int pa, int d) {
  for (auto v : g[u]) {
    if (v == pa)
      continue;
    if (d + 1 >= dist[v]) {
      continue;
    } else {
      dfs2(v, u, d + 1);
    }
  }
  ans[u] = dist[u];
}

int main() {
  int N;
  cin >> N >> tak >> ao;
  tak--;
  ao--;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(ao, -1, 0);
  dfs2(tak, -1, 0);
  int f_a = 0;
  for (int i = 0; i < N; i++) {
    f_a = max(ans[i], f_a);
  }
  cout << f_a - 1 << endl;
}
Problem p02834 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
using namespace std;
vector<ll> adj[100009];
ll d[100009][2];

void fun(ll x, bool f) {
  for (ll y : adj[x])
    if (d[y][f] == -1) {
      d[y][f] = d[x][f] + 1;
      fun(y, f);
    }
}
int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;
  ll n, u, v;
  cin >> n >> u >> v;
  u--, v--;

  ll x, y;
  for (ll i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  memset(d, -1, sizeof(d));
  d[u][0] = d[v][1] = 0;
  fun(u, 0);
  fun(v, 1);
  ll ans = 0;

  for (ll i = 0; i < n; i++)
    if (d[i][0] < d[i][1])
      ans = max(ans, d[i][1]);
  cout << ans - 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
using namespace std;
vector<ll> adj[100009];
ll d[100009][2];

void fun(ll x, bool f) {
  for (ll y : adj[x])
    if (d[y][f] == -1) {
      d[y][f] = d[x][f] + 1;
      fun(y, f);
    }
}
int main() {
  fast;
  ll n, u, v;
  cin >> n >> u >> v;
  u--, v--;

  ll x, y;
  for (ll i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  memset(d, -1, sizeof(d));
  d[u][0] = d[v][1] = 0;
  fun(u, 0);
  fun(v, 1);
  ll ans = 0;

  for (ll i = 0; i < n; i++)
    if (d[i][0] < d[i][1])
      ans = max(ans, d[i][1]);
  cout << ans - 1 << endl;
}
Problem p02834 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define INF (ll)1e18 + 50
#define MAXN (int)1e5 + 50
#define fi first
#define se second
#define MOD 1000000007
#define all(v) ((v).begin()), ((v).end())
#define A 911382323
#define B 972663749

vector<vector<int>> G;
int dist[101010][2];

void dfs(int cur, int pre, int d, int id) {
  dist[cur][id] = d;

  for (const auto next : G[cur]) {
    if (next != pre) {
      dfs(next, cur, d + 1, id);
    }
  }
}
int main() {
  int n, u, v;
  cin >> n >> u >> v;
  u--;
  v--;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(u, u, 0, 0);
  dfs(v, v, 0, 1);

  int ans = 0;
  for (int i = 0; i < n; i++)
    if (dist[i][0] < dist[i][1])
      ans = max(ans, dist[i][1] - 1);

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define INF (ll)1e18 + 50
#define MAXN (int)1e5 + 50
#define fi first
#define se second
#define MOD 1000000007
#define all(v) ((v).begin()), ((v).end())
#define A 911382323
#define B 972663749

vector<int> G[101010];
int dist[101010][2];

void dfs(int cur, int pre, int d, int id) {
  dist[cur][id] = d;

  for (const auto next : G[cur]) {
    if (next != pre) {
      dfs(next, cur, d + 1, id);
    }
  }
}
int main() {
  int n, u, v;
  cin >> n >> u >> v;
  u--;
  v--;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(u, u, 0, 0);
  dfs(v, v, 0, 1);

  int ans = 0;
  for (int i = 0; i < n; i++)
    if (dist[i][0] < dist[i][1])
      ans = max(ans, dist[i][1] - 1);

  cout << ans;
}
Problem p02834 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18;
const int MOD = 1e9 + 7;

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

  queue<int> que;
  vector<int> dist1(N, -1);
  que.push(u);
  dist1[u] = 0;
  while (!que.empty()) {
    int k = que.front();
    que.pop();

    for (auto nk : G[k]) {
      if (dist1[nk] != -1)
        continue;
      dist1[nk] = dist1[k] + 1;
      que.push(nk);
    }
  }

  vector<int> dist2(N, -1);
  que.push(v);
  dist2[v] = 0;
  int mdist = 0, lv, dif = 0;
  while (!que.empty()) {
    int k = que.front();
    que.pop();

    for (auto nk : G[k]) {
      if (dist2[nk] != -1)
        continue;
      dist2[nk] = dist2[k] + 1;
      que.push(nk);
    }
  }

  int ans = 0;
  for (int i = 0; i < N; ++i)
    if (dist1[i] < dist2[i])
      ans = max(dist2[i] - 1, ans);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18;
const int MOD = 1e9 + 7;

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

  queue<int> que;
  vector<int> dist1(N, -1);
  que.push(u);
  dist1[u] = 0;
  while (!que.empty()) {
    int k = que.front();
    que.pop();

    for (auto nk : G[k]) {
      if (dist1[nk] != -1)
        continue;
      dist1[nk] = dist1[k] + 1;
      que.push(nk);
    }
  }

  vector<int> dist2(N, -1);
  que.push(v);
  dist2[v] = 0;
  int mdist = 0, lv, dif = 0;
  while (!que.empty()) {
    int k = que.front();
    que.pop();

    for (auto nk : G[k]) {
      if (dist2[nk] != -1)
        continue;
      dist2[nk] = dist2[k] + 1;
      que.push(nk);
    }
  }

  int ans = 0;
  for (int i = 0; i < N; ++i)
    if (dist1[i] < dist2[i])
      ans = max(dist2[i] - 1, ans);
  cout << ans << endl;

  return 0;
}
Problem p02834 (C++)

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

vector<int> to[10005];
int n;

vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
  dist[v] = d;
  for (int u : to[v]) {
    if (u == p)
      continue;
    dfs(u, d + 1, v);
  }
}

vector<int> calcDist(int s) {
  dist = vector<int>(n);
  dfs(s);
  return dist;
}

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

  vector<int> distS = calcDist(s);
  vector<int> distT = calcDist(t);

  int mx = 0;
  for (int i = 0; i < n; i++) {
    if (distS[i] < distT[i]) {
      mx = max(mx, distT[i]);
    }
  }
  int ans = mx - 1;
  cout << ans << endl;
  return 0;
}


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

vector<int> to[100005];
int n;

vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
  dist[v] = d;
  for (int u : to[v]) {
    if (u == p)
      continue;
    dfs(u, d + 1, v);
  }
}

vector<int> calcDist(int s) {
  dist = vector<int>(n);
  dfs(s);
  return dist;
}

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

  vector<int> distS = calcDist(s);
  vector<int> distT = calcDist(t);

  int mx = 0;
  for (int i = 0; i < n; i++) {
    if (distS[i] < distT[i]) {
      mx = max(mx, distT[i]);
    }
  }
  int ans = mx - 1;
  cout << ans << endl;
  return 0;
}
Problem p02834 (C++)

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

typedef long long int ll;
const ll maxs = 1e5;

#define pb push_back

ll n, a, b;
std::vector<ll> Tree[maxs];
ll dist[2][maxs];

void go(ll s, ll p, ll ind) {
  dist[ind][s] = dist[ind][p] + 1;
  for (auto d : Tree[s]) {
    if (d != p)
      go(d, s, ind);
  }
}

int main() {
  cin >> n >> a >> b;
  for (ll i = 0; i < n - 1; i++) {
    ll s, d;
    cin >> s >> d;
    Tree[s].pb(d);
    Tree[d].pb(s);
  }
  dist[0][0] = dist[1][0] = -1;
  go(a, 0, 0);
  go(b, 0, 1);

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (dist[0][i] < dist[1][i]) {
      ans = max(ans, dist[1][i] - 1);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

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

typedef long long int ll;
const ll maxs = 1e5 + 5;

#define pb push_back

ll n, a, b;
std::vector<ll> Tree[maxs];
ll dist[2][maxs];

void go(ll s, ll p, ll ind) {
  dist[ind][s] = dist[ind][p] + 1;
  for (auto d : Tree[s]) {
    if (d != p)
      go(d, s, ind);
  }
}

int main() {
  cin >> n >> a >> b;
  for (ll i = 0; i < n - 1; i++) {
    ll s, d;
    cin >> s >> d;
    Tree[s].pb(d);
    Tree[d].pb(s);
  }
  dist[0][0] = dist[1][0] = -1;
  go(a, 0, 0);
  go(b, 0, 1);

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (dist[0][i] < dist[1][i]) {
      ans = max(ans, dist[1][i] - 1);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}