Problem p03824 (C++)

Buggy submission (status: Runtime Error):
// Be Name Khoda
/*
tane adami sharifast be jane adamiat
na hamin lebase zibast neshane adamiat
agar adami be cheshmasto dahano goosho bini
che miane naghshe divaro miane adamiat
khoro khabo khashmo shahvat shaghab asto jahlo zolmat
hayavan khabar nadarad ze jahane adamiat
be haghighat adami bash vagarna morgh bashad
ke hami sokhan begooyad be zabane adamiat
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

typedef long long LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define MKP make_pair
#define RETD(x)                                                                \
  { cout << fixed << setprecision(15) << x; } // exit(0); }
#define GEI(x) scanf("%d", &x)
#define GEL(x) scanf("%lld", &x)
#define ALL(x) x.begin(), x.end()
const ll M = 5e4 + 5, LG = 32, SM = 2e4 + 5, inf = 1e9 + 5;
ll mod = 1e9 + 7;
#define MOK(x)                                                                 \
  if (x > mod)                                                                 \
    x -= mod;
ll pw(ll x, ll n) {
  return (n == 0 ? 1 : pw(x * x % mod, n / 2) * (n & 1 ? x : 1) % mod);
}

ll n;
vll g[M];
ll cm[M], cn[M];
ll res;

ll lasb(ll x) { return (x < 2 ? x : lasb(x >> 1) + 1); }
ll firb(ll x) { return (x & 1 ? 0 : firb(x >> 1) + 1); }

void dfs(ll x, ll p) {
  ll ba = 0;
  for (ll y : g[x]) {
    if (y == p)
      continue;
    dfs(y, x);
    ba |= cm[x] & cm[y];
    cm[x] |= cm[y];
  }
  ll nd = lasb(ba);
  cm[x] &= -(1 << nd);
  cm[x] += (1 << nd);
  cn[x] = firb(cm[x]);
  smax(res, cn[x]);
}

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  For(i, 1, n) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].PB(b);
    g[b].PB(a);
  }
  dfs(0, 0);
  RET(res);
}

Fixed submission:
// Be Name Khoda
/*
tane adami sharifast be jane adamiat
na hamin lebase zibast neshane adamiat
agar adami be cheshmasto dahano goosho bini
che miane naghshe divaro miane adamiat
khoro khabo khashmo shahvat shaghab asto jahlo zolmat
hayavan khabar nadarad ze jahane adamiat
be haghighat adami bash vagarna morgh bashad
ke hami sokhan begooyad be zabane adamiat
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

typedef long long LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define MKP make_pair
#define RETD(x)                                                                \
  { cout << fixed << setprecision(15) << x; } // exit(0); }
#define GEI(x) scanf("%d", &x)
#define GEL(x) scanf("%lld", &x)
#define ALL(x) x.begin(), x.end()
const ll M = 2e5 + 5, LG = 32, SM = 2e4 + 5, inf = 1e9 + 5;
ll mod = 1e9 + 7;
#define MOK(x)                                                                 \
  if (x > mod)                                                                 \
    x -= mod;
ll pw(ll x, ll n) {
  return (n == 0 ? 1 : pw(x * x % mod, n / 2) * (n & 1 ? x : 1) % mod);
}

ll n;
vll g[M];
ll cm[M], cn[M];
ll res;

ll lasb(ll x) { return (x < 2 ? x : lasb(x >> 1) + 1); }
ll firb(ll x) { return (x & 1 ? 0 : firb(x >> 1) + 1); }

void dfs(ll x, ll p) {
  ll ba = 0;
  for (ll y : g[x]) {
    if (y == p)
      continue;
    dfs(y, x);
    ba |= cm[x] & cm[y];
    cm[x] |= cm[y];
  }
  ll nd = lasb(ba);
  cm[x] &= -(1 << nd);
  cm[x] += (1 << nd);
  cn[x] = firb(cm[x]);
  smax(res, cn[x]);
}

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  For(i, 1, n) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].PB(b);
    g[b].PB(a);
  }
  dfs(0, 0);
  RET(res);
}