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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

typedef VVI Graph;
Graph G;
const int N = 100000;
const int LOG_N = 20;
int depth[N];
int parent[LOG_N][N];
void dfs(int node, int p = -1, int d = 0) {
  if (depth[node] >= 0)
    return;
  depth[node] = d;
  parent[0][node] = p;
  for (int i = 0; i < SZ(G[node]); ++i)
    dfs(G[node][i], node, d + 1);
}

void init(int V) {
  fill(depth, depth + N, -1);
  dfs(0);
  for (int i = 0; i + 1 < LOG_N; ++i) {
    for (int v = 0; v < V; ++v) {
      if (parent[i][v] == -1)
        parent[i + 1][v] = -1;
      else
        parent[i + 1][v] = parent[i][parent[i][v]];
    }
  }
}

int LCA(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  // depth[u] <= depth[v]
  for (int i = 0; i < LOG_N; ++i)
    if ((depth[v] - depth[u]) >> i & 1)
      v = parent[i][v];
  if (u == v)
    return u;

  for (int i = LOG_N - 1; i >= 0; --i) {
    if (parent[i][u] != parent[i][v]) {
      u = parent[i][u];
      v = parent[i][v];
    }
  }
  return parent[0][u];
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int V;
  cin >> V;
  G.assign(V, VI());
  REP(i, V) {
    int k;
    cin >> k;
    REP(j, k) {
      int c;
      cin >> c;
      G[i].PB(c);
    }
  }
  init(V);
  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << LCA(u, v) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

typedef VVI Graph;

Graph G;
const int N = 100000;
const int LOG_N = 350;
int depth[N];
int parent[LOG_N][N];
void dfs(int node, int p = -1, int d = 0) {
  if (depth[node] >= 0)
    return;
  depth[node] = d;
  parent[0][node] = p;
  for (int i = 0; i < SZ(G[node]); ++i)
    dfs(G[node][i], node, d + 1);
}

void init(int V) {
  fill(depth, depth + N, -1);
  dfs(0);
  for (int i = 0; i + 1 < LOG_N; ++i) {
    for (int v = 0; v < V; ++v) {
      if (parent[i][v] == -1)
        parent[i + 1][v] = -1;
      else
        parent[i + 1][v] = parent[i][parent[i][v]];
    }
  }
}

int LCA(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  // depth[u] <= depth[v]
  for (int i = 0; i < LOG_N; ++i)
    if ((depth[v] - depth[u]) >> i & 1)
      v = parent[i][v];
  if (u == v)
    return u;

  for (int i = LOG_N - 1; i >= 0; --i) {
    if (parent[i][u] != parent[i][v]) {
      u = parent[i][u];
      v = parent[i][v];
    }
  }
  return parent[0][u];
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int V;
  cin >> V;
  G.assign(V, VI());
  REP(i, V) {
    int k;
    cin >> k;
    REP(j, k) {
      int c;
      cin >> c;
      G[i].PB(c);
    }
  }
  init(V);
  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << LCA(u, v) << endl;
  }

  return 0;
}