Problem p02373 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
int binary[64][maxn];
vector<int> v[maxn];
int tuvshin[maxn];

string lol(int n) {
  string s;
  while (n > 0) {
    s += to_string(n % 2);
    n /= 2;
  }
  return s;
}

int main() {
  int n;
  cin >> n;
  memset(binary, -1, sizeof binary);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    for (int j = 1; j <= a; j++) {
      int x;
      cin >> x;
      v[i].push_back(x);
      binary[0][x] = i;
    }
  }

  for (int i = 1; i <= 32; i++)
    for (int j = 1; j < n; j++)
      binary[i][j] = binary[i - 1][binary[i - 1][j]];

  queue<pair<int, int>> pq;
  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    int x = pq.front().first;
    int k = pq.front().second;
    tuvshin[x] = k;
    pq.pop();
    for (int i = 0; i < v[x].size(); i++) {
      pq.push(make_pair(v[x][i], k + 1));
    }
  }
  int t;
  cin >> t;
  while (t--) {
    int a, b;
    cin >> a >> b;
    if (tuvshin[a] > tuvshin[b]) {
      int k = tuvshin[a] - tuvshin[b];
      string s = lol(k);
      for (int i = 0; i < s.size(); i++)
        if (s[i] == '1')
          a = binary[i][a];
    }
    if (tuvshin[b] > tuvshin[a]) {
      int k = tuvshin[b] - tuvshin[a];
      string s = lol(k);
      for (int i = 0; i < s.size(); i++)
        if (s[i] == '1')
          b = binary[i][b];
    }
    while (a != b) {
      int l = 0, r = 64;
      while (l <= r) {
        int mid = (r + l) >> 1;
        if (binary[mid][a] == binary[mid][b]) {
          r = mid - 1;
        } else
          l = mid + 1;
      }
      if (l != 0)
        l--;
      a = binary[l][a];
      b = binary[l][b];
    }
    cout << a << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define si(i) scanf("%d", &i)
#define sll(i) scanf("%lld", &i)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl '\\n'
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

vector<vector<int>> v;
int n, l[100005];
int f[100005][17];

int main() {
  si(n);
  v.resize(n);
  int nn = 32 - __builtin_clz(n);
  memset(f[0], -1, sizeof f[0]);
  for (int i = 0; i < n; i++) {
    int k;
    si(k);
    for (int j = 0; j < k; j++) {
      int c;
      si(c);
      v[k].pb(c);
      v[c].pb(k);
      f[0][c] = i;
    }
  }

  memset(l, -1, sizeof(l));
  queue<int> q;
  q.push(0);
  l[0] = 0;
  while (!q.empty()) {
    int u = q.front();
    for (int x : v[u]) {
      if (l[x] == -1) {
        l[x] = l[u] + 1;
        q.push(x);
      }
    }
  }

  for (int i = 1; i <= nn; i++) {
    for (int j = 0; j < n; j++) {
      int x = f[i - 1][j];
      if (x == -1) {
        f[i][j] = -1;
        continue;
      }
      int y = f[i - 1][x];
      f[i][j] = y;
    }
  }
  int _q;
  si(_q);
  while (_q--) {
    int u, v;
    si(u);
    si(v);
    if (l[u] < l[v])
      swap(u, v);
    for (int i = nn; i >= 0; i--) {
      if ((1 << i) <= l[u] - l[v]) {
        u = f[i][u];
      }
    }
    if (u == v) {
      cout << u << nl;
      continue;
    }
    for (int i = nn; i >= 0; i--) {
      if (f[i][u] == f[i][v])
        continue;
      u = f[i][u];
      v = f[i][v];
    }
    cout << f[0][u] << nl;
  }
}