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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int main() {
  long long n, a, b, e = 0;
  cin >> n;
  int test[10001] = {0};
  int uiop[10001] = {0};
  for (int h = 1; h <= n; h++) {
    cin >> a >> b;
    test[h] = a;
    uiop[h] = b;
  }
  for (int h = n; h >= 1; h--) {
    if ((test[h] + e) % uiop[h] != 0) {
      e += uiop[h] - ((test[h] + e) % uiop[h]);
    }
  }
  cout << e << endl;
  cin >> e;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int main() {
  long long n, a, b, e = 0;
  cin >> n;
  int test[100001] = {0};
  int uiop[100001] = {0};
  for (int h = 1; h <= n; h++) {
    cin >> a >> b;
    test[h] = a;
    uiop[h] = b;
  }
  for (int h = n; h >= 1; h--) {
    if ((test[h] + e) % uiop[h] != 0) {
      e += uiop[h] - ((test[h] + e) % uiop[h]);
    }
  }
  cout << e << endl;
  cin >> e;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;
ll inf = 1e10;

int main() {
  ll N, A[100000], B[100000];
  cin >> N;
  for (ll i(0); i < N; i++)
    cin >> A[i] >> B[i];
  ll l(0);
  ll ans(0);
  for (ll i(N - 1); i >= 0; i--) {
    ll Aa = A[i];
    while (Aa % B[i] != 0) {
      Aa = (A[i] / B[i]) * B[i] + B[i];
    }
    while (l > Aa - A[i]) {
      Aa += B[i];
    }
    l = Aa - A[i];
    ans += Aa - A[i] - ans;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;
ll inf = 1e10;

int main() {
  ll N, A[100000], B[100000];
  cin >> N;
  for (ll i(0); i < N; i++)
    cin >> A[i] >> B[i];
  ll l(0);
  ll ans(0);
  for (ll i(N - 1); i >= 0; i--) {
    ll Aa = (A[i] + l + B[i] - 1) / B[i] * B[i];
    l = Aa - A[i];
    ans += Aa - A[i] - ans;
  }
  cout << ans << endl;
  return 0;
}
Problem p03821 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = []
b = []
for i in range(n):
    t, m = map(int, input().split())
    a.append(t)
    b.append(m)
ans = 0
for i in range(n):
    while (ans + a[-1 - i]) % b[-1 - i] != 0:
        ans += 1
print(ans)


Fixed submission:
n = int(input())
a = []
b = []
for i in range(n):
    t, m = map(int, input().split())
    a.append(t)
    b.append(m)
ans = 0
for i in range(n):
    if (ans + a[-1 - i]) % b[-1 - i] != 0:
        ans += b[-1 - i] - (ans + a[-1 - i]) % b[-1 - i]
print(ans)
Problem p03822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define LL long long
#define st first
#define nd second
using namespace std;
int n, val[100005], t;
vector<int> v[10005];
void dfs(int x) {
  vector<int> a;
  val[x] = 0;
  if (v[x].size() == 0) {
    return;
  }
  for (int i = 0; i < v[x].size(); ++i) {
    dfs(v[x][i]);
    a.pb(val[v[x][i]]);
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < a.size(); ++i) {
    val[x] = max(val[x], a[i]);
    ++val[x];
  }
}
int main() {
  cin >> n;
  for (int i = 2; i <= n; ++i) {
    cin >> t;
    v[t].pb(i);
  }
  dfs(1);
  cout << val[1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define LL long long
#define st first
#define nd second
using namespace std;
int n, val[100005], t;
vector<int> v[100005];
void dfs(int x) {
  vector<int> a;
  val[x] = 0;
  if (v[x].size() == 0) {
    return;
  }
  for (int i = 0; i < v[x].size(); ++i) {
    dfs(v[x][i]);
    a.pb(val[v[x][i]]);
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < a.size(); ++i) {
    val[x] = max(val[x], a[i]);
    ++val[x];
  }
}
int main() {
  cin >> n;
  for (int i = 2; i <= n; ++i) {
    cin >> t;
    v[t].pb(i);
  }
  dfs(1);
  cout << val[1] << endl;
}
Problem p03822 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

template <typename F> class FixPoint : F {
public:
  explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)) {}

  template <typename... Args>
  constexpr decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
static inline constexpr decltype(auto) makeFixPoint(F &&f) noexcept {
  return FixPoint<F>{std::forward<F>(f)};
}

namespace null_null {

void main_() {
  size_t N;
  std::cin >> N;
  std::vector<std::vector<intmax_t>> child(N);
  for (size_t i = 0; i < N - 1; i++) {
    intmax_t a;
    std::cin >> a;
    a--;
    child[a].push_back(i + 1);
  }

  auto result = makeFixPoint([](auto dfs, auto child, int n) -> intmax_t {
    std::vector<int> v;
    for (auto x : child[n]) {
      v.push_back(dfs(child, x));
    }
    std::sort(v.begin(), v.end(), std::greater<intmax_t>());

    int res = 0;
    for (size_t i = 0; i < v.size(); i++) {
      res = std::max(res, (int)(v[i] + i + 1));
    }

    return res;
  });

  std::cout << result(child, 0) << std::endl;
  return;
}

} // namespace null_null

int main() {
  null_null::main_();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

template <typename F> class FixPoint : F {
public:
  explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)) {}

  template <typename... Args>
  constexpr decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
static inline constexpr decltype(auto) makeFixPoint(F &&f) noexcept {
  return FixPoint<F>{std::forward<F>(f)};
}

namespace null_null {

void main_() {
  size_t N;
  std::cin >> N;
  std::vector<std::vector<intmax_t>> child(N);
  for (size_t i = 0; i < N - 1; i++) {
    intmax_t a;
    std::cin >> a;
    a--;
    child[a].push_back(i + 1);
  }

  auto result = makeFixPoint([](auto dfs, auto &child, int n) -> intmax_t {
    std::vector<int> v;
    for (auto x : child[n]) {
      v.push_back(dfs(child, x));
    }
    std::sort(v.begin(), v.end(), std::greater<intmax_t>());

    int res = 0;
    for (size_t i = 0; i < v.size(); i++) {
      res = std::max(res, (int)(v[i] + i + 1));
    }

    return res;
  });

  std::cout << result(child, 0) << std::endl;
  return;
}

} // namespace null_null

int main() {
  null_null::main_();
  return 0;
}
Problem p03822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 5005
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
vector<int> a[MAXN];
int ans[MAXN];
bool cmp(int x, int y) { return ans[x] < ans[y]; }
void work(int pos) {
  for (unsigned i = 0; i < a[pos].size(); i++)
    work(a[pos][i]);
  sort(a[pos].begin(), a[pos].end(), cmp);
  int tans = 0;
  for (unsigned i = 0; i < a[pos].size(); i++)
    tans = max(tans + 1, ans[a[pos][i]] + 1);
  ans[pos] = tans;
}
int main() {
  int n;
  read(n);
  for (int i = 2; i <= n; i++) {
    int x;
    read(x);
    a[x].push_back(i);
  }
  work(1);
  cout << ans[1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
vector<int> a[MAXN];
int ans[MAXN];
bool cmp(int x, int y) { return ans[x] < ans[y]; }
void work(int pos) {
  for (unsigned i = 0; i < a[pos].size(); i++)
    work(a[pos][i]);
  sort(a[pos].begin(), a[pos].end(), cmp);
  int tans = 0;
  for (unsigned i = 0; i < a[pos].size(); i++)
    tans = max(tans + 1, ans[a[pos][i]] + 1);
  ans[pos] = tans;
}
int main() {
  int n;
  read(n);
  for (int i = 2; i <= n; i++) {
    int x;
    read(x);
    a[x].push_back(i);
  }
  work(1);
  cout << ans[1] << endl;
  return 0;
}
Problem p03822 (C++)

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

using namespace std;

vector<int> G[100000];
vector<int> b[100000];

int dfs(int n) {
  int N = G[n].size();
  if (N == 0)
    return 1;
  int m = 0;

  for (int i = 0; i < N; i++) {
    b[n][i] = dfs(G[n][i]);
  }
  sort(b[n].begin(), b[n].end(), greater<int>());
  for (int i = 0; i < N; i++) {
    m = max(m, b[n][i] + i + 1);
  }
  return m;
}

int main() {
  int N;
  int a[100000];
  cin >> N;
  for (int i = 1; i < N; i++) {
    cin >> a[i];
    a[i]--;
    G[a[i]].push_back(i);
  }
  cout << dfs(0) - 1 << endl;
}

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

using namespace std;

vector<int> G[100000];
vector<int> b[100000];

int dfs(int n) {
  int N = G[n].size();
  if (N == 0)
    return 1;
  int m = 0;

  for (int i = 0; i < N; i++) {
    b[n].push_back(dfs(G[n][i]));
  }
  sort(b[n].begin(), b[n].end(), greater<int>());
  for (int i = 0; i < N; i++) {
    m = max(m, b[n][i] + i + 1);
  }
  return m;
}

int main() {
  int N;
  int a[100000];
  cin >> N;
  for (int i = 1; i < N; i++) {
    cin >> a[i];
    a[i]--;
    G[a[i]].push_back(i);
  }
  cout << dfs(0) - 1 << endl;
}
Problem p03822 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define N 200005

using namespace std;
typedef pair<int, int> Pair;

inline int input() {
  int x;
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48))
    ;
  return x;
}

int n, fa[N], f[N];
vector<int> T[N];

inline bool cmp(int x, int y) { return f[x] < f[y]; }

void dfs(int u) {
  for (int v : T[u])
    dfs(v);
  sort(T[u].begin(), T[u].end(), cmp);
  for (int v : T[u])
    f[u] = max(f[u], f[v]) + 1;
}

int main() {
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
  freopen("debug", "w", stderr);

  n = input();
  for (int i = 2; i <= n; ++i)
    fa[i] = input(), T[fa[i]].push_back(i);

  dfs(1);
  printf("%d\n", f[1]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define N 200005

using namespace std;
typedef pair<int, int> Pair;

inline int input() {
  int x;
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48))
    ;
  return x;
}

int n, fa[N], f[N];
vector<int> T[N];

inline bool cmp(int x, int y) { return f[x] < f[y]; }

void dfs(int u) {
  for (int v : T[u])
    dfs(v);
  sort(T[u].begin(), T[u].end(), cmp);
  for (int v : T[u])
    f[u] = max(f[u], f[v]) + 1;
}

int main() {
  n = input();
  for (int i = 2; i <= n; ++i)
    fa[i] = input(), T[fa[i]].push_back(i);

  dfs(1);
  printf("%d\n", f[1]);
  return 0;
}
Problem p03822 (C++)

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

int main() {
  ll n;
  cin >> n;
  vector<vector<ll>> g(n);

  rep(i, n - 1) {
    ll a;
    cin >> a;
    g[i + 1].push_back(a - 1);
    g[a - 1].push_back(i + 1);
  }
  vector<vector<ll>> st(n);
  vector<ll> par(n);
  vector<ll> ans(n);
  queue<ll> q;
  queue<ll> qq;

  q.push(0);
  par[0] = -1;
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    if (g[p].size() == 1) {
      qq.push(p);
    }
    for (auto r : g[p]) {
      if (r == par[p])
        continue;
      par[r] = p;
      q.push(r);
    }
  }
  while (!qq.empty()) {
    ll p = qq.front();
    qq.pop();
    st[par[p]].push_back(ans[p] + 1);
    if (st[par[p]].size() == g[par[p]].size()) {
      sort(st[par[p]].rbegin(), st[par[p]].rend());
      rep(i, st[par[p]].size()) st[par[p]][i] += i;
      ans[par[p]] = *max_element(st[par[p]].begin(), st[par[p]].end());
    }
    if (st[par[p]].size() == g[par[p]].size() - 1 && par[p] != 0) {
      sort(st[par[p]].rbegin(), st[par[p]].rend());
      rep(i, st[par[p]].size()) st[par[p]][i] += i;
      ans[par[p]] = *max_element(st[par[p]].begin(), st[par[p]].end());
      qq.push(par[p]);
    }
  }
  cout << ans[0] << endl;
  return 0;
}

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

int main() {
  ll n;
  cin >> n;
  vector<vector<ll>> g(n);

  rep(i, n - 1) {
    ll a;
    cin >> a;
    g[i + 1].push_back(a - 1);
    g[a - 1].push_back(i + 1);
  }
  vector<vector<ll>> st(n);
  vector<ll> par(n);
  vector<ll> ans(n);
  queue<ll> q;
  queue<ll> qq;

  q.push(0);
  par[0] = -1;
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    if (g[p].size() == 1 && p != 0) {
      qq.push(p);
    }
    for (auto r : g[p]) {
      if (r == par[p])
        continue;
      par[r] = p;
      q.push(r);
    }
  }
  while (!qq.empty()) {
    ll p = qq.front();
    qq.pop();
    st[par[p]].push_back(ans[p] + 1);
    if (st[par[p]].size() == g[par[p]].size()) {
      sort(st[par[p]].rbegin(), st[par[p]].rend());
      rep(i, st[par[p]].size()) st[par[p]][i] += i;
      ans[par[p]] = *max_element(st[par[p]].begin(), st[par[p]].end());
    }
    if (st[par[p]].size() == g[par[p]].size() - 1 && par[p] != 0) {
      sort(st[par[p]].rbegin(), st[par[p]].rend());
      rep(i, st[par[p]].size()) st[par[p]][i] += i;
      ans[par[p]] = *max_element(st[par[p]].begin(), st[par[p]].end());
      qq.push(par[p]);
    }
  }
  cout << ans[0] << endl;
  return 0;
}
Problem p03822 (C++)

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

const int maxn = (int)(1e5) + 5;
vector<int> nxt[maxn], val[maxn];
int f[maxn];

void dfs(int x) {
  for (int i = 0; i < (int)nxt[x].size(); ++i) {
    dfs(nxt[x][i]);
    val[x].push_back(f[nxt[x][i]]);
  }
  f[x] = val[x].size();
  sort(val[x].begin(), val[x].end(), greater<int>());
  int now = 0;
  for (int i = 0; i < (int)val[x].size(); ++i)
    f[x] = max(f[x], val[x][i] + now + 1), ++now;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 2; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    nxt[x].push_back(i);
  }
  dfs(1);
  return printf("%d\n", f[1]);
}

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

const int maxn = (int)(1e5) + 5;
vector<int> nxt[maxn], val[maxn];
int f[maxn];

void dfs(int x) {
  for (int i = 0; i < (int)nxt[x].size(); ++i) {
    dfs(nxt[x][i]);
    val[x].push_back(f[nxt[x][i]]);
  }
  f[x] = val[x].size();
  sort(val[x].begin(), val[x].end(), greater<int>());
  int now = 0;
  for (int i = 0; i < (int)val[x].size(); ++i)
    f[x] = max(f[x], val[x][i] + now + 1), ++now;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 2; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    nxt[x].push_back(i);
  }
  dfs(1);
  return printf("%d\n", f[1]), 0;
}
Problem p03822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
using namespace std;
int n;
vector<int> win[100000];
int getdep(int x) {
  vector<int> dps;
  if (win[x].size() == 0)
    return 0;
  for (int i = 0; i < win[x].size(); i++) {
    dps.pb(getdep(win[x][i]));
  }
  sort(dps.begin(), dps.end());
  for (int i = 0; i < dps.size(); i++) {
    dps[i] += dps.size() - i;
  }
  sort(dps.begin(), dps.end());
  return dps[dps.size() - 1];
}
int main() {
  cin >> n;
  for (int i = 2; i <= n; i++) {
    int tmp;
    cin >> tmp;
    win[tmp].pb(i);
  }
  cout << getdep(1) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
using namespace std;
int n;
vector<int> win[100009];
int getdep(int x) {
  vector<int> dps;
  if (win[x].size() == 0)
    return 0;
  for (int i = 0; i < win[x].size(); i++) {
    dps.pb(getdep(win[x][i]));
  }
  sort(dps.begin(), dps.end());
  for (int i = 0; i < dps.size(); i++) {
    dps[i] += dps.size() - i;
  }
  sort(dps.begin(), dps.end());
  return dps[dps.size() - 1];
}
int main() {
  cin >> n;
  for (int i = 2; i <= n; i++) {
    int tmp;
    cin >> tmp;
    win[tmp].pb(i);
  }
  cout << getdep(1) << endl;
  return 0;
}
Problem p03822 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
graph = [[] for _ in range(n)]
for i in range(1, n):
    graph[int(input()) - 1].append(i)


def dfs(x):
    arr = [dfs(y) for y in graph[x]]
    if not (arr):
        return 0
    arr.sort(reverse=True)
    return max(i + a for i, a in enumerate(arr, 1))


print(dfs(0))


Fixed submission:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(input())
graph = [[] for _ in range(n)]
for i in range(1, n):
    graph[int(input()) - 1].append(i)


def dfs(x):
    arr = [dfs(y) for y in graph[x]]
    if not (arr):
        return 0
    arr.sort(reverse=True)
    return max(i + a for i, a in enumerate(arr, 1))


print(dfs(0))
Problem p03822 (C++)

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

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, n, m) for (ll i = n; i < (ll)(m); i++)
#define pb push_back
#define INF 1000000007LL
#define all(a) (a).begin(), (a).end()

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

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

#define MAX_N 10

vector<int> child[MAX_N];

ll dfs(ll node) {
  vector<int> v;
  for (auto ch : child[node]) {
    v.pb(dfs(ch));
  }
  sort(v.begin(), v.end(), greater<int>()); // N log N
  ll tmp = 0;
  REP(i, v.size()) { tmp = max(tmp, 1 + i + v[i]); }
  return tmp;
}
int main() {
  int N;
  cin >> N;
  REP(i, N - 1) {
    int a;
    cin >> a;
    child[--a].pb(i + 1);
  }
  cout << dfs(0) << endl;
}

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

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, n, m) for (ll i = n; i < (ll)(m); i++)
#define pb push_back
#define INF 1000000007LL
#define all(a) (a).begin(), (a).end()

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

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

#define MAX_N 100010

vector<int> child[MAX_N];

ll dfs(ll node) {
  vector<int> v;
  for (auto ch : child[node]) {
    v.pb(dfs(ch));
  }
  sort(v.begin(), v.end(), greater<int>()); // N log N
  ll tmp = 0;
  REP(i, v.size()) { tmp = max(tmp, 1 + i + v[i]); }
  return tmp;
}
int main() {
  int N;
  cin >> N;
  REP(i, N - 1) {
    int a;
    cin >> a;
    child[--a].pb(i + 1);
  }
  cout << dfs(0) << endl;
}
Problem p03822 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> A(n), D(n, 1);
  A[0] = -1;
  D[0] = 0;
  for (int i = 1; i < n; ++i) {
    cin >> A[i];
    --A[i];
    ++D[A[i]];
  }
  vector<int> C = D;
  priority_queue<pair<int, int>> pq;
  // queue< pair<int,int> > pq;
  for (int i = 1; i < n; ++i) {
    if (D[i] == 1)
      pq.emplace(-D[i], i);
    --C[i];
  }
  while (!pq.empty()) {
    int d = -pq.top().first, idx = pq.top().second;
    // int d = -pq.front().first, idx = pq.front().second;
    pq.pop();
    int p = A[idx];
    D[p] = max(D[p], C[p] + d);
    --C[p];
    if (!C[p]) {
      pq.emplace(-D[p], p);
    }
  }
  // for(int i = 0; i < n; ++i) cout << D[i] << " ";cout << endl;
  cout << D[0] - 1 << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> A(n), D(n, 1);
  A[0] = -1;
  D[0] = 0;
  for (int i = 1; i < n; ++i) {
    cin >> A[i];
    --A[i];
    ++D[A[i]];
  }
  vector<int> C = D;
  priority_queue<pair<int, int>> pq;
  // queue< pair<int,int> > pq;
  for (int i = 1; i < n; ++i) {
    if (D[i] == 1)
      pq.emplace(-D[i], i);
    --C[i];
  }
  while (!pq.empty()) {
    int d = -pq.top().first, idx = pq.top().second;
    // int d = -pq.front().first, idx = pq.front().second;
    pq.pop();
    int p = A[idx];
    D[p] = max(D[p], C[p] + d);
    --C[p];
    if (p > 0 and !C[p]) {
      pq.emplace(-D[p], p);
    }
  }
  // for(int i = 0; i < n; ++i) cout << D[i] << " ";cout << endl;
  cout << D[0] - 1 << endl;
  return 0;
}
Problem p03822 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**5)

N = int(input())
T = [[] for _ in range(N)]
for i in range(1, N):
    x = int(input()) - 1
    T[x].append(i)


def f(x):
    candidates = sorted([f(y) for y in T[x]], reverse=True)
    return max((c + i for i, c in enumerate(candidates, 1)), default=0)


print(f(0))


Fixed submission:
import sys

sys.setrecursionlimit(10**9)

N = int(input())
T = [[] for _ in range(N)]
for i in range(1, N):
    x = int(input()) - 1
    T[x].append(i)


def f(x):
    candidates = sorted([f(y) for y in T[x]], reverse=True)
    return max((c + i for i, c in enumerate(candidates, 1)), default=0)


print(f(0))
Problem p03823 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int n, nex[100005], dp2[100005];
long long s[100005], ab[2];
map<vector<int>, int> dp;
int solve(vector<int> v);
int solve2(int i) {
  if (i == n + 1)
    return 0;
  if (dp2[i] != -1)
    return dp2[i];
  return dp2[i] = (solve2(i + 1) + solve({0, i})) % mod;
}
int solve(vector<int> v) {
  int idx = max(v[0], v[1]) + 1;
  if (idx == n + 1)
    return 1;
  if (dp.count(v))
    return dp[v];
  if (s[idx] - s[idx - 1] >= ab[1])
    return dp[v] = (solve({idx, 0}) + solve({0, idx})) % mod;
  if (s[idx] - s[idx - 1] < ab[0]) {
    bool b = (v[0] == idx - 1);
    if (s[idx] - s[v[b]] >= ab[b]) {
      auto nv = v;
      nv[b] = idx;
      return dp[v] = solve(nv);
    }
    return dp[v] = 0;
  }
  vector<int> nv = v;
  nv[0] = nex[idx];
  int ans = solve(nv), st = lower_bound(s, s + n + 1, s[v[1]] + ab[1]) - s;
  if (st <= nex[idx])
    ans = (1LL * ans + solve2(st) - solve2(nex[idx] + 1) + mod) % mod;
  return dp[v] = ans;
}
int main() {
  scanf("%d%lld%lld", &n, &ab[0], &ab[1]);
  if (ab[0] > ab[1])
    swap(ab[0], ab[1]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]);
  nex[n] = n;
  for (int i = n - 1; i > 0; i--) {
    if (s[i + 1] - s[i] >= ab[0] && s[i + 1] - s[i] < ab[1])
      nex[i] = nex[i + 1];
    else
      nex[i] = i;
  }
  s[0] = -1e18;
  s[n + 1] = 1e18;
  memset(dp2, -1, sizeof(dp2));
  printf("%d", (solve({1, 0}) + solve({0, 1})) % mod);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int n, nex[100005], dp2[100005];
long long s[100005], ab[2];
map<vector<int>, int> dp;
int solve(vector<int> v);
int solve2(int i) {
  if (i == n + 1)
    return 0;
  if (dp2[i] != -1)
    return dp2[i];
  return dp2[i] = (solve2(i + 1) + solve({0, i})) % mod;
}
int solve(vector<int> v) {
  int idx = max(v[0], v[1]) + 1;
  if (idx == n + 1)
    return 1;
  if (dp.count(v))
    return dp[v];
  if (s[idx] - s[idx - 1] >= ab[1])
    return dp[v] = (solve({idx, 0}) + solve({0, idx})) % mod;
  if (s[idx] - s[idx - 1] < ab[0]) {
    bool b = (v[0] == idx - 1);
    if (s[idx] - s[v[b]] >= ab[b]) {
      auto nv = v;
      nv[b] = idx;
      return dp[v] = solve(nv);
    }
    return dp[v] = 0;
  }
  vector<int> nv = v;
  nv[0] = nex[idx];
  int ans = solve(nv), st = lower_bound(s, s + n + 1, s[v[1]] + ab[1]) - s;
  st = max(st, idx);
  if (st <= nex[idx])
    ans = (1LL * ans + solve2(st) - solve2(nex[idx] + 1) + mod) % mod;
  return dp[v] = ans;
}
int main() {
  scanf("%d%lld%lld", &n, &ab[0], &ab[1]);
  if (ab[0] > ab[1])
    swap(ab[0], ab[1]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]);
  nex[n] = n;
  for (int i = n - 1; i > 0; i--) {
    if (s[i + 1] - s[i] >= ab[0] && s[i + 1] - s[i] < ab[1])
      nex[i] = nex[i + 1];
    else
      nex[i] = i;
  }
  s[0] = -1e18;
  s[n + 1] = 1e18;
  memset(dp2, -1, sizeof(dp2));
  printf("%d", (solve({1, 0}) + solve({0, 1})) % mod);
}
Problem p03823 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;

inline ll read() {
  ll x = 0;
  char ch = getchar();
  while (ch < '0' || '9' < ch)
    ch = getchar();
  while ('0' <= ch && ch <= '9')
    x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}

const int N = 1e3 + 9;
const int md = 1e9 + 7;

ll n, x[N], a[2], p[2], sp[2];
ll f[N][2], sumf[N][2];

int main() {
  n = read();
  a[0] = read();
  a[1] = read();

  for (int i = 1; i <= n; i++)
    x[i] = read();
  x[0] = -1e18;

  p[0] = p[1] = sp[0] = sp[1] = 0;
  f[0][0] = f[0][1] = sumf[0][0] = sumf[0][1] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 1; j++) {
      while (p[j] < i && x[i + 1] - x[p[j]] >= a[j])
        p[j]++;
      f[i][j] = sumf[p[j] - 1][j ^ 1];
    }
    for (int j = 0; j <= 1; j++) {
      if (x[i + 1] - x[i] < a[j])
        for (; sp[j] < i; sp[j]++)
          f[sp[j]][j] = sumf[sp[j]][j] = 0;
      sumf[i][j] = (sumf[i - 1][j] + f[i][j]) % md;
    }
  }

  printf("%lld\n", (sumf[n - 1][0] + sumf[n - 1][1]) % md);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;

inline ll read() {
  ll x = 0;
  char ch = getchar();
  while (ch < '0' || '9' < ch)
    ch = getchar();
  while ('0' <= ch && ch <= '9')
    x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}

const int N = 1e5 + 9;
const int md = 1e9 + 7;

ll n, x[N], a[2], p[2], sp[2];
ll f[N][2], sumf[N][2];

int main() {
  n = read();
  a[0] = read();
  a[1] = read();

  for (int i = 1; i <= n; i++)
    x[i] = read();
  x[0] = -1e18;

  p[0] = p[1] = sp[0] = sp[1] = 0;
  f[0][0] = f[0][1] = sumf[0][0] = sumf[0][1] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 1; j++) {
      while (p[j] < i && x[i + 1] - x[p[j]] >= a[j])
        p[j]++;
      f[i][j] = sumf[p[j] - 1][j ^ 1];
    }
    for (int j = 0; j <= 1; j++) {
      if (x[i + 1] - x[i] < a[j])
        for (; sp[j] < i; sp[j]++)
          f[sp[j]][j] = sumf[sp[j]][j] = 0;
      sumf[i][j] = (sumf[i - 1][j] + f[i][j]) % md;
    }
  }

  printf("%lld\n", (sumf[n - 1][0] + sumf[n - 1][1]) % md);
  return 0;
}
Problem p03823 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll a, b;
const ll INF = 1000000000000000000;
const int mod = 1000000007;
const int N = 505;
ll s[N];
int dp[N];
int p1, p2;
int ans = 0;
int sum = 0;
int main() {
  scanf("%d%lld%lld", &n, &a, &b);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]);
  if (a >= b)
    swap(a, b);
  dp[0] = 1;
  s[0] = -INF;
  s[n + 1] = INF * 2;
  for (int i = 2; i <= n; i++) {
    if (s[i] - s[i - 2] < a) {
      printf("0");
      return 0;
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    while (p2 < i && s[i] - s[p2] >= b) {
      // printf("%lld: can extend to %lld, add %d\n",s[i],s[p2],dp[p2]);
      sum += dp[p2];
      sum %= mod;
      p2++;
    }
    // printf("dp[%d] = %d\n",i,sum);
    dp[i] = sum;
    if (s[i] - s[i - 1] < a) {
      while (p1 < i - 1) {
        // printf("shrink to %lld, remove %d\n",s[p1],dp[p1]);
        if (p1 < p2) {
          sum += mod - dp[p1];
          sum %= mod;
        }
        dp[p1] = 0;
        p1++;
      }
    }
    /*for (int j = 0; j <= i; j++){
        printf("%d ",dp[j]);
    }
    printf("\n");
    printf("now sum = %d\n",sum);*/
  }
  printf("%d", dp[n + 1]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll a, b;
const ll INF = 1000000000000000000;
const int mod = 1000000007;
const int N = 100005;
ll s[N];
int dp[N];
int p1, p2;
int ans = 0;
int sum = 0;
int main() {
  scanf("%d%lld%lld", &n, &a, &b);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]);
  if (a >= b)
    swap(a, b);
  dp[0] = 1;
  s[0] = -INF;
  s[n + 1] = INF * 2;
  for (int i = 2; i <= n; i++) {
    if (s[i] - s[i - 2] < a) {
      printf("0");
      return 0;
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    while (p2 < i && s[i] - s[p2] >= b) {
      // printf("%lld: can extend to %lld, add %d\n",s[i],s[p2],dp[p2]);
      sum += dp[p2];
      sum %= mod;
      p2++;
    }
    // printf("dp[%d] = %d\n",i,sum);
    dp[i] = sum;
    if (s[i] - s[i - 1] < a) {
      while (p1 < i - 1) {
        // printf("shrink to %lld, remove %d\n",s[p1],dp[p1]);
        if (p1 < p2) {
          sum += mod - dp[p1];
          sum %= mod;
        }
        dp[p1] = 0;
        p1++;
      }
    }
    /*for (int j = 0; j <= i; j++){
        printf("%d ",dp[j]);
    }
    printf("\n");
    printf("now sum = %d\n",sum);*/
  }
  printf("%d", dp[n + 1]);
}
Problem p03824 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 50010
using namespace std;
int n, tote, to[N << 1], nxt[N << 1], con[N], w[N], ans;
void ins(int x, int y) {
  to[++tote] = y;
  nxt[tote] = con[x];
  con[x] = tote;
}
void dfs(int v, int fa) {
  int dep = -1;
  bool flag = 0;
  for (int p = con[v]; p; p = nxt[p])
    if (to[p] != fa) {
      dfs(to[p], v);
      int tmp = w[v] & w[to[p]];
      for (int i = 30; i >= 0; i--)
        if ((tmp >> i) & 1) {
          dep = max(dep, i);
          break;
        }
      w[v] = w[v] | w[to[p]];
      flag = 1;
    }
  if (!flag) {
    w[v] = 1;
    return;
  }
  for (int i = dep + 1; i <= 30; i++)
    if ((w[v] >> i) & 1)
      dep = max(dep, i);
    else
      break;
  ans = max(ans, dep + 1);
  int R = (1 << (dep + 1)), T = ((1 << 30) - 1) ^ (R - 1);
  w[v] = (w[v] & T) | R;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  dfs(1, 0);
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 100010
using namespace std;
int n, tote, to[N << 1], nxt[N << 1], con[N], w[N], ans;
void ins(int x, int y) {
  to[++tote] = y;
  nxt[tote] = con[x];
  con[x] = tote;
}
void dfs(int v, int fa) {
  int dep = -1;
  bool flag = 0;
  for (int p = con[v]; p; p = nxt[p])
    if (to[p] != fa) {
      dfs(to[p], v);
      int tmp = w[v] & w[to[p]];
      for (int i = 30; i >= 0; i--)
        if ((tmp >> i) & 1) {
          dep = max(dep, i);
          break;
        }
      w[v] = w[v] | w[to[p]];
      flag = 1;
    }
  if (!flag) {
    w[v] = 1;
    return;
  }
  for (int i = dep + 1; i <= 30; i++)
    if ((w[v] >> i) & 1)
      dep = max(dep, i);
    else
      break;
  ans = max(ans, dep + 1);
  int R = (1 << (dep + 1)), T = ((1 << 30) - 1) ^ (R - 1);
  w[v] = (w[v] & T) | R;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  dfs(1, 0);
  printf("%d", ans);
  return 0;
}
Problem p03824 (C++)

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

int n, f[55000], g[22];
vector<int> G[55000];

int mg(int x, int y) {
  for (int t; t = (x & y);) {
    x += t & -t;
  }
  return min(10001000, x | y);
}

void dfs(int x, int fff) {
  for (auto y : G[x])
    if (y != fff)
      dfs(y, x);
  memset(g, 0, sizeof g);
  for (auto y : G[x])
    if (y != fff) {
      for (int i = 0; i <= 20; ++i) {
        int tmp = f[y] >> i << i;
        if (tmp >> i & 1)
          tmp += 1 << i;
        g[i] = mg(g[i], tmp);
      }
    }
  f[x] = 998244353;
  for (int i = 0; i <= 20; ++i)
    f[x] = min(f[x], g[i] | 1 << i);
}

int main() {
  cin >> n;
  int x, y;
  for (int tim = n - 1; tim--;) {
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
  int ans = 0;
  for (int t = f[1]; t; t >>= 1)
    ++ans;
  cout << ans - 1 << endl;
}


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

int n, f[505000], g[22];
vector<int> G[505000];

int mg(int x, int y) {
  for (int t; t = (x & y);) {
    x += t & -t;
  }
  return min(10001000, x | y);
}

void dfs(int x, int fff) {
  for (auto y : G[x])
    if (y != fff)
      dfs(y, x);
  memset(g, 0, sizeof g);
  for (auto y : G[x])
    if (y != fff) {
      for (int i = 0; i <= 20; ++i) {
        int tmp = f[y] >> i << i;
        if (tmp >> i & 1)
          tmp += 1 << i;
        g[i] = mg(g[i], tmp);
      }
    }
  f[x] = 998244353;
  for (int i = 0; i <= 20; ++i)
    f[x] = min(f[x], g[i] | 1 << i);
}

int main() {
  cin >> n;
  int x, y;
  for (int tim = n - 1; tim--;) {
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
  int ans = 0;
  for (int t = f[1]; t; t >>= 1)
    ++ans;
  cout << ans - 1 << endl;
}
Problem p03824 (C++)

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

const int MAXN = 100010;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * f;
}

int e, st[MAXN], to[MAXN];
int nxt[MAXN], l[MAXN][20];
inline void Add(int u, int v) {
  to[++e] = v, nxt[e] = st[u];
  st[u] = e;
}

int n, ans;

void dfs(int u, int fa) {
  int i, j;
  for (i = st[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa)
      continue;
    dfs(v, u);
    for (j = 0; j < 20; j++)
      l[u][j] += l[v][j];
  }
  int loc = -1;
  for (j = 0; j < 20; j++)
    if (l[u][j] >= 2)
      loc = j;
  for (j = loc + 1; j < 20; j++)
    if (!l[u][j])
      break;
  loc = j;
  ans = max(ans, loc);
  l[u][loc] = 1;
  for (j = 0; j < loc; j++)
    l[u][j] = 0;
}

int main() {

  int i;

  n = read();
  for (i = 1; i < n; i++) {
    int u = read(), v = read();
    Add(u, v), Add(v, u);
  }

  dfs(1, 0);

  printf("%d\n", ans);
  return 0;
}


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

const int MAXN = 100010;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * f;
}

int e, st[MAXN], to[MAXN << 1];
int nxt[MAXN << 1], l[MAXN][20];
inline void Add(int u, int v) {
  to[++e] = v, nxt[e] = st[u];
  st[u] = e;
}

int n, ans;

void dfs(int u, int fa) {
  int i, j;
  for (i = st[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa)
      continue;
    dfs(v, u);
    for (j = 0; j < 20; j++)
      l[u][j] += l[v][j];
  }
  int loc = -1;
  for (j = 0; j < 20; j++)
    if (l[u][j] >= 2)
      loc = j;
  for (j = loc + 1; j < 20; j++)
    if (!l[u][j])
      break;
  loc = j;
  ans = max(ans, loc);
  l[u][loc] = 1;
  for (j = 0; j < loc; j++)
    l[u][j] = 0;
}

int main() {

  int i;

  n = read();
  for (i = 1; i < n; i++) {
    int u = read(), v = read();
    Add(u, v), Add(v, u);
  }

  dfs(1, 0);

  printf("%d\n", ans);
  return 0;
}
Problem p03824 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
#define N 100050
int head[N], to[N << 1], nxt[N << 1], cnt, n, tmp[22], f[22][N], ans;
inline void add(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
}
void merge(int *a, int *b) {
  int i;
  for (i = 0; i <= 20; i++)
    a[i] += b[i];
}
void dfs(int x, int y) {
  int i;
  for (i = head[x]; i; i = nxt[i])
    if (to[i] != y)
      dfs(to[i], x);
  memset(tmp, 0, sizeof(tmp));
  for (i = head[x]; i; i = nxt[i])
    if (to[i] != y)
      merge(tmp, f[to[i]]);
  int k = 0;
  for (i = 20; i >= 0; i--) {
    if (tmp[i] > 1) {
      k = i + 1;
      break;
    }
  }
  for (; tmp[k]; k++)
    ;
  memcpy(f[x] + k, tmp + k, sizeof(int) * (20 - k));
  f[x][k]++;
  ans = max(ans, k);
}
int main() {
  scanf("%d", &n);
  int i, x, y;
  for (i = 1; i < n; i++)
    scanf("%d%d", &x, &y), add(x, y), add(y, x);
  dfs(1, 0);
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
#define N 100050
int head[N], to[N << 1], nxt[N << 1], cnt, n, tmp[22], f[N][22], ans;
inline void add(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
}
void merge(int *a, int *b) {
  int i;
  for (i = 0; i <= 20; i++)
    a[i] += b[i];
}
void dfs(int x, int y) {
  int i;
  for (i = head[x]; i; i = nxt[i])
    if (to[i] != y)
      dfs(to[i], x);
  memset(tmp, 0, sizeof(tmp));
  for (i = head[x]; i; i = nxt[i])
    if (to[i] != y)
      merge(tmp, f[to[i]]);
  int k = 0;
  for (i = 20; i >= 0; i--) {
    if (tmp[i] > 1) {
      k = i + 1;
      break;
    }
  }
  for (; tmp[k]; k++)
    ;
  memcpy(f[x] + k, tmp + k, sizeof(int) * (20 - k));
  f[x][k]++;
  ans = max(ans, k);
}
int main() {
  scanf("%d", &n);
  int i, x, y;
  for (i = 1; i < n; i++)
    scanf("%d%d", &x, &y), add(x, y), add(y, x);
  dfs(1, 0);
  printf("%d\n", ans);
}
Problem p03824 (C++)

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

int n, x[N], y[N], next[N], p[N], f[N], g[N], ans;

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -f;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

void travel(int x) {
  int tmp = 0, t = 0;
  for (int t = p[x]; t; t = next[t])
    if (f[x] != y[t]) {
      f[y[t]] = x;
      travel(y[t]);
      tmp |= g[x] & g[y[t]];
      g[x] |= g[y[t]];
    }
  while ((1 << t) <= tmp || (1 << t) & g[x])
    t++;
  g[x] = (g[x] >> t << t) | (1 << t);
  ans = max(ans, t);
}

int main() {
  n = read();
  for (int i = 1; i <= n - 1; i++) {
    x[2 * i - 1] = y[2 * i] = read();
    y[2 * i - 1] = x[2 * i] = read();
    next[2 * i - 1] = p[x[2 * i]];
    p[x[2 * i - 1]] = 2 * i - 1;
    next[2 * i] = p[x[2 * i]];
    p[x[2 * i]] = 2 * i;
  }
  travel(1);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 1111116
#define next nxt

int n, x[N], y[N], next[N], p[N], f[N], g[N], ans;

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -f;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

void travel(int x) {
  int tmp = 0, t = 0;
  for (int t = p[x]; t; t = next[t])
    if (f[x] != y[t]) {
      f[y[t]] = x;
      travel(y[t]);
      tmp |= g[x] & g[y[t]];
      g[x] |= g[y[t]];
    }
  while ((1 << t) <= tmp || (1 << t) & g[x])
    t++;
  g[x] = (g[x] >> t << t) | (1 << t);
  ans = max(ans, t);
}

int main() {
  n = read();
  for (int i = 1; i <= n - 1; i++) {
    x[2 * i - 1] = y[2 * i] = read();
    y[2 * i - 1] = x[2 * i] = read();
    next[2 * i - 1] = p[x[2 * i - 1]];
    p[x[2 * i - 1]] = 2 * i - 1;
    next[2 * i] = p[x[2 * i]];
    p[x[2 * i]] = 2 * i;
  }
  travel(1);
  printf("%d\n", ans);
  return 0;
}
Problem p03824 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 50000 + 5;
struct Edge {
  int to, next;
} e[N << 1];
int head[N], cnt;
void ins(int u, int v) {
  e[++cnt] = (Edge){v, head[u]};
  head[u] = cnt;
}
int f[N], bin[30], s[N], m, c[30];
void dp(int u, int fa) {
  int sv = 0, cu = 0, son = 0;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa)
      continue;
    dp(v, u);
    sv |= s[v];
    son++;
  }
  if (!son) {
    f[u] = 0;
    s[u] = 1;
    return;
  }
  memset(c, 0, sizeof(c));
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].to != fa)
      for (int j = 0; j < m; j++)
        if (s[e[i].to] & (1 << j))
          c[j]++;
  if (son > 1)
    for (int i = m - 1; i >= 0; i--)
      if (c[i] >= 2) {
        cu = i + 1;
        break;
      }
  for (int i = cu; i < m; i++)
    if (!(sv & (1 << i))) {
      cu = i;
      break;
    }
  s[u] = ((sv >> cu) | 1) << cu;
  f[u] = cu;
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].to != fa)
      f[u] = max(f[u], f[e[i].to]);
}
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  int n;
  scanf("%d", &n);
  m = log2(n) + 1;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ins(u, v);
    ins(v, u);
  }
  dp(1, 0);
  printf("%d\n", f[1]);
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100000 + 5;
struct Edge {
  int to, next;
} e[N << 1];
int head[N], cnt;
void ins(int u, int v) {
  e[++cnt] = (Edge){v, head[u]};
  head[u] = cnt;
}
int f[N], bin[30], s[N], m, c[30];
void dp(int u, int fa) {
  int sv = 0, cu = 0, son = 0;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa)
      continue;
    dp(v, u);
    sv |= s[v];
    son++;
  }
  if (!son) {
    f[u] = 0;
    s[u] = 1;
    return;
  }
  memset(c, 0, sizeof(c));
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].to != fa)
      for (int j = 0; j < m; j++)
        if (s[e[i].to] & (1 << j))
          c[j]++;
  if (son > 1)
    for (int i = m - 1; i >= 0; i--)
      if (c[i] >= 2) {
        cu = i + 1;
        break;
      }
  for (int i = cu; i < m; i++)
    if (!(sv & (1 << i))) {
      cu = i;
      break;
    }
  s[u] = ((sv >> cu) | 1) << cu;
  f[u] = cu;
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].to != fa)
      f[u] = max(f[u], f[e[i].to]);
}
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  int n;
  scanf("%d", &n);
  m = log2(n) + 1;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ins(u, v);
    ins(v, u);
  }
  dp(1, 0);
  printf("%d\n", f[1]);
  return 0;
}
Problem p03824 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int finder(vector<vector<int>> G, int now, int prv) {
  vector<int> bits;
  for (auto nxt : G[now]) {
    if (nxt == prv)
      continue;
    bits.push_back(finder(G, nxt, now));
  }
  int res = 0;
  for (int bit = 20; bit >= 0; bit--) {
    int cnt = 0;
    for (auto p : bits) {
      if (p & (1 << bit))
        cnt++;
    }
    if (cnt == 1)
      res += 1 << bit;
    else if (cnt > 1) {
      res += 1 << (bit + 1);
      return res;
    }
  }
  res++;
  return res;
}

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

  int res = finder(G, 0, -1);
  int ret = 0;
  while (res > 1) {
    res /= 2;
    ret++;
  }
  printf("%d\n", ret);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int finder(const vector<vector<int>> &G, int now, int prv) {
  vector<int> bits;
  for (auto nxt : G[now]) {
    if (nxt == prv)
      continue;
    bits.push_back(finder(G, nxt, now));
  }
  int res = 0;
  for (int bit = 20; bit >= 0; bit--) {
    int cnt = 0;
    for (auto p : bits) {
      if (p & (1 << bit))
        cnt++;
    }
    if (cnt == 1)
      res += 1 << bit;
    else if (cnt > 1) {
      res += 1 << (bit + 1);
      return res;
    }
  }
  res++;
  return res;
}

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

  int res = finder(G, 0, -1);
  int ret = 0;
  while (res > 1) {
    res /= 2;
    ret++;
  }
  printf("%d\n", ret);
}
Problem p03824 (C++)

Buggy submission (status: Runtime Error):
// ¹Ø¼üÐÔÖÊ£ºÁ½¸öÉî¶ÈÏàÍ¬µÄµãµÄÂ·¾¶ÉÏ±ØÓÐÒ»¸öÉî¶È¸üÐ¡µÄµã
#include <bits/stdc++.h>
using namespace std;

const int MaxN = 1e5 + 5;

int N, Ans;
int Cnt[MaxN][20];

vector<int> To[MaxN];

void DFS(int u, int fa) {
  int i, k;
  for (auto v : To[u])
    if (v ^ fa) {
      DFS(v, u);
      for (i = 0; i < 20; i++)
        Cnt[u][i] += Cnt[v][i];
    }
  for (i = 19; ~i; i--)
    if (Cnt[u][i] > 1) {
      k = i + 1;
      break;
    }
  for (; Cnt[u][k]; k++)
    ;
  Ans = max(Ans, k);
  for (i = 0; i < k; i++)
    Cnt[u][i] = 0;
  Cnt[u][k]++;
}

int main() {
  int i, u, v;
  scanf("%d", &N);
  for (i = 1; i < N; i++) {
    scanf("%d%d", &u, &v);
    To[u].push_back(v);
    To[v].push_back(u);
  }
  DFS(1, 0);
  printf("%d\n", Ans);
  return 0;
}

Fixed submission:
// ¹Ø¼üÐÔÖÊ£ºÁ½¸öÉî¶ÈÏàÍ¬µÄµãµÄÂ·¾¶ÉÏ±ØÓÐÒ»¸öÉî¶È¸üÐ¡µÄµã
#include <bits/stdc++.h>
using namespace std;

const int MaxN = 1e5 + 5;

int N, Ans;
int Cnt[MaxN][20];

vector<int> To[MaxN];

void DFS(int u, int fa) {
  int i, k = 0;
  for (auto v : To[u])
    if (v ^ fa) {
      DFS(v, u);
      for (i = 0; i < 20; i++)
        Cnt[u][i] += Cnt[v][i];
    }
  for (i = 19; ~i; i--)
    if (Cnt[u][i] > 1) {
      k = i + 1;
      break;
    }
  for (; Cnt[u][k]; k++)
    ;
  Ans = max(Ans, k);
  for (i = 0; i < k; i++)
    Cnt[u][i] = 0;
  Cnt[u][k]++;
}

int main() {
  int i, u, v;
  scanf("%d", &N);
  for (i = 1; i < N; i++) {
    scanf("%d%d", &u, &v);
    To[u].push_back(v);
    To[v].push_back(u);
  }
  DFS(1, 0);
  printf("%d\n", Ans);
  return 0;
}