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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 200005
typedef long long ll;
typedef pair<int, int> P;
struct data {
  ll x, a, b;
};

int N, Q;
int a, b, c;
vector<int> G[MAX_N];
int p[MAX_N], d[MAX_N], cnt = 0;
vector<int> g, flg;

void rec(int pos, int prev, int depth) {
  d[pos] = depth;
  p[pos] = g.size();
  g.push_back(pos);

  for (int i = 0; i < (int)G[pos].size(); i++) {
    int w = G[pos][i];
    if (w == prev)
      continue;
    rec(w, pos, depth + 1);
    g.push_back(pos);
  }
}

P dp[MAX_N][26];

int lca(int u, int v) {

  u = p[u];
  v = p[v];
  if (u > v)
    swap(u, v);
  int dist = v - u + 1;
  int pos = u;
  P res = P((1 << 26), (1 << 26));

  for (int I = 26; I >= 0; I--) {
    int i = (1 << I);
    if (dist >= i) {
      dist -= i;
      res = min(res, dp[pos][I]);
      pos += i;
    }
  }

  return g[res.second];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%d", &a);
    for (int j = 0; j < a; j++) {
      scanf("%d", &b);
      G[i].push_back(b);
    }
  }

  rec(0, -1, 0);

  for (int i = 0; i < (int)g.size(); i++)
    dp[i][0] = P(d[g[i]], i);

  int K = 1;
  for (int k = 1; k < 26; k++) {
    for (int i = 0; i < (int)g.size(); i++) {
      dp[i][k] = dp[i][k - 1];
      int j = i + K;
      if (j >= (int)g.size())
        j = g.size() - 1;
      dp[i][k] = min(dp[i][k], dp[j][k - 1]);
    }
    K *= 2;
  }

  cin >> Q;

  for (int i = 0; i < Q; i++) {
    scanf("%d %d", &a, &b);
    printf("%d\\n", lca(a, b));
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 200005
typedef long long ll;
typedef pair<int, int> P;
struct data {
  ll x, a, b;
};

int N, Q;
int a, b, c;
vector<int> G[MAX_N];
int p[MAX_N], d[MAX_N], cnt = 0;
vector<int> g, flg;

void rec(int pos, int prev, int depth) {
  d[pos] = depth;
  p[pos] = g.size();
  g.push_back(pos);

  for (int i = 0; i < (int)G[pos].size(); i++) {
    int w = G[pos][i];
    if (w == prev)
      continue;
    rec(w, pos, depth + 1);
    g.push_back(pos);
  }
}

int Depth[MAX_N];
P dp[MAX_N][30];

int lca(int u, int v) {

  u = p[u];
  v = p[v];
  if (u > v)
    swap(u, v);
  int dist = v - u + 1;
  int pos = u;
  P res = P((1 << 26), (1 << 26));

  for (int I = 26; I >= 0; I--) {
    int i = (1 << I);
    if (dist >= i) {
      dist -= i;
      res = min(res, dp[pos][I]);
      pos += i;
    }
  }

  return g[res.second];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%d", &a);
    for (int j = 0; j < a; j++) {
      scanf("%d", &b);
      G[i].push_back(b);
    }
  }

  rec(0, -1, 0);

  for (int i = 0; i < (int)g.size(); i++)
    dp[i][0] = P(d[g[i]], i);

  int K = 1;
  for (int k = 1; k < 30; k++) {
    for (int i = 0; i < (int)g.size(); i++) {
      dp[i][k] = dp[i][k - 1];
      int j = i + K;
      if (j >= (int)g.size())
        j = g.size() - 1;
      dp[i][k] = min(dp[i][k], dp[j][k - 1]);
    }
    K *= 2;
  }

  cin >> Q;

  for (int i = 0; i < Q; i++) {
    scanf("%d %d", &a, &b);
    printf("%d\\n", lca(a, b));
  }

  return 0;
}