alexjercan/bugnet huggingface dataset:
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

vector<P> G[10000];
int used[10000];
int res1[10000];
int res2[10000];

P dfs(int n) {
  used[n] = 1;
  P res(n, 0);
  for (auto e : G[n]) {
    if (used[e.first] == 0) {
      auto p = dfs(e.first);
      if (p.second + e.second > res.second) {
        res = p;
        res.second += e.second;
      }
    }
  }
  used[n] = 0;
  return res;
}

void dfs1(int n, int d) {
  used[n] = 1;
  res1[n] = d;
  for (auto e : G[n]) {
    if (used[e.first] == 0) {
      dfs1(e.first, d + e.second);
    }
  }
  used[n] = 0;
}

void dfs2(int n, int d) {
  used[n] = 1;
  res2[n] = d;
  for (auto e : G[n]) {
    if (used[e.first] == 0) {
      dfs2(e.first, d + e.second);
    }
  }
  used[n] = 0;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0, s, t, w; i < n - 1; i++) {
    cin >> s >> t >> w;
    G[s].push_back(P(t, w));
    G[t].push_back(P(s, w));
  }
  auto s = dfs(0);
  auto t = dfs(s.first);
  dfs1(s.first, 0);
  dfs2(t.first, 0);
  for (int i = 0; i < n; i++) {
    cout << max(res1[i], res2[i]) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

vector<P> G[100000];
int used[100000];

P dfs(int n) {
  used[n] = 1;
  P res(n, 0);
  for (auto e : G[n]) {
    if (used[e.first] == 0) {
      auto p = dfs(e.first);
      if (p.second + e.second > res.second) {
        res = p;
        res.second += e.second;
      }
    }
  }
  used[n] = 0;
  return res;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0, s, t, w; i < n - 1; i++) {
    cin >> s >> t >> w;
    G[s].push_back(P(t, w));
    G[t].push_back(P(s, w));
  }
  for (int i = 0; i < n; i++) {
    cout << dfs(i).second << endl;
  }
  return 0;
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

// GRL_5_B Height of a Tree

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>

#define MAXN 10010

using namespace std;

struct edge {
  int to, wt;
  edge(int to, int wt) : to(to), wt(wt) {}
};

vector<edge> adj[MAXN];
bool visited[MAXN];
int LD[MAXN];

void dfs(int i, int ld) {
  if (visited[i])
    return;
  visited[i] = true;
  LD[i] = max(LD[i], ld);
  for (auto &e : adj[i])
    if (!visited[e.to])
      dfs(e.to, ld + e.wt);
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0, s, t, wt; i < n - 1; ++i) {
    scanf("%d%d%d", &s, &t, &wt);
    adj[s].emplace_back(t, wt);
    adj[t].emplace_back(s, wt);
  }
  memset(LD, 0, sizeof(LD));

  for (int root = 0; root < n; ++root) {
    if (adj[root].size() == 1) {
      memset(visited, 0, sizeof(visited));
      dfs(root, 0);
    }
  }
  for (int root = 0; root < n; ++root) {
    printf("%d\\n", LD[root]);
  }
}


Error: TLE

Fixed submission:

// GRL_5_B Height of a Tree

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

struct {
  int to, wt, next;
} E[20020];
int LE[10010];

bool visited[10010];
int LD[10010];

void dfs(int i, int parent) {
  if (visited[i])
    return;
  visited[i] = true;
  int ld = 0;
  for (int j = LE[i]; j != -1; j = E[j].next) {
    if (E[j].to == parent)
      continue;
    dfs(E[j].to, i);
    ld = max(ld, E[j].wt + LD[E[j].to]);
  }
  LD[i] = ld;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  fill(LE, LE + 10010, -1);
  for (int i = 0, s, t, wt; i < n - 1; ++i) {
    cin >> s >> t >> wt;
    E[2 * i] = {t, wt, LE[s]};
    E[2 * i + 1] = {s, wt, LE[t]};
    LE[s] = 2 * i;
    LE[t] = 2 * i + 1;
  }

  for (int root = 0; root < n; ++root) {
    memset(visited, 0, sizeof(visited));
    dfs(root, -1);
    cout << LD[root] << endl;
  }
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 2e9;
const int maxv = 10000 + 10;
int V;
struct Edge {
  int to, cost;
};
vector<Edge> g[maxv];

typedef pair<int, int> P;

vector<int> dijkstra(int s) {
  vector<int> d(V);
  fill(d.begin(), d.end(), INF);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  d[s] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < g[v].size(); i++) {
      Edge &e = g[v][i];
      if (d[v] != INF && d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
  return d;
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &V);
  int u, v, cost;
  while (~scanf("%d%d%d", &u, &v, &cost)) {
    g[u].push_back((Edge){v, cost});
    g[v].push_back((Edge){u, cost});
  }
  vector<int> d = dijkstra(0);
  int target = max_element(d.begin(), d.end()) - d.begin();
  d = dijkstra(target);
  target = max_element(d.begin(), d.end()) - d.begin();
  vector<int> d1 = dijkstra(target);
  for (int i = 0; i < V; i++)
    printf("%d\\n", max(d[i], d1[i]));

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 2e9;
const int maxv = 10000 + 10;
int V;
struct Edge {
  int to, cost;
};
vector<Edge> g[maxv];
int d[maxv];
typedef pair<int, int> P;

void dijkstra(int s) {
  fill(d, d + V, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  d[s] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < g[v].size(); i++) {
      Edge &e = g[v][i];
      if (d[v] != INF && d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &V);
  int u, v, cost;
  while (~scanf("%d%d%d", &u, &v, &cost)) {
    g[u].push_back((Edge){v, cost});
    g[v].push_back((Edge){u, cost});
  }
  for (int i = 0; i < V; i++) {
    dijkstra(i);
    int res = 0;
    for (int j = 0; j < V; j++) {
      if (d[j] == INF)
        continue;
      res = max(res, d[j]);
    }
    printf("%d\\n", res);
  }

  return 0;
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 10010
using namespace std;
struct no {
  int to, cost;
};
vector<no> G[N];
int n;
int X[N], Y[N];
int d[N], used[N];
void init() {
  memset(d, 0, sizeof(d));
  fill(used, used + N, false);
}
void dfs(int u) {
  used[u] = true;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i].to;
    if (!used[v]) {
      d[v] = d[u] + G[u][i].cost;
      dfs(v);
    }
  }
}
int findMax(int a[]) {
  int maxi = 0, maxv = 0;
  for (int i = 0; i < n; i++)
    if (a[i] > maxv) {
      maxv = a[i];
      maxi = i;
    }
  return maxi;
}
void solve() {
  init();
  dfs(0);
  int x = findMax(d);

  init();
  dfs(x);
  for (int i = 0; i < n; i++)
    X[i] = d[i];
  int y = findMax(d);

  init();
  dfs(y);
  for (int i = 0; i < n; i++)
    Y[i] = d[i];
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  solve();
  for (int i = 0; i < n; i++) {
    printf("%d\\n", max(X[i], Y[i]));
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 10010
using namespace std;
struct no {
  int to, cost;
};
vector<no> G[N];
int d[N];
bool used[N];
int n;
void dfs(int u) {
  used[u] = true;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i].to;
    if (!used[v]) {
      d[v] = d[u] + G[u][i].cost;
      dfs(v);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }

  for (int i = 0; i < n; i++) {
    memset(d, 0, sizeof(d));
    fill(used, used + N, false);
    dfs(i);
    int maxv = 0;
    for (int j = 0; j < n; j++)
      if (d[j] > maxv)
        maxv = d[j];
    printf("%d\\n", maxv);
  }
  return 0;
}
Problem p02372 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int V, E, s, t, w;
vector<vector<pair<int, int>>> v(10001);
int visited[10001] = {0};
int ans[10001] = {0}, d[10001] = {0};

void di(int n) {
  visited[n] = 1;
  for (int i = 0; i < v[n].size(); i++) {
    if (visited[v[n][i].first] == 0) {
      d[v[n][i].first] = d[n] + v[n][i].second;
      di(v[n][i].first);
    }
  }
}

/*void dfs(int n){
        visited[n] = 1;
        for(int i=0;i<v[n].size();i++){
                if(visited[v[n][i].first]==0){
                        dfs(v[n][i].first);
                        d[n] = max(d[n],d[v[n][i].first]+v[n][i].second);
                }
        }
}
*/
int main() {
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    cin >> s >> t >> w;
    v[s].push_back({t, w});
    v[t].push_back({s, w});
  }
  di(0);
  int a = 0, b = 0, va = 0, vb = 0;
  for (int i = 0; i < V; i++) {
    if (va < d[i]) {
      a = i;
      va = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    d[i] = 0;
  }
  di(a);
  for (int i = 0; i < V; i++) {
    if (vb < d[i]) {
      b = i;
      vb = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    ans[i] = d[i];
    d[i] = 0;
  }
  di(b);
  for (int i = 0; i < V; i++) {
    ans[i] = max(d[i], ans[i]);
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int V, E, s, t, w;
vector<vector<pair<int, int>>> v(10001);
int visited[10001] = {0};
int ans[10001] = {0}, d[10001] = {0};

void di(int n) {
  visited[n] = 1;
  for (int i = 0; i < v[n].size(); i++) {
    if (visited[v[n][i].first] == 0) {
      d[v[n][i].first] = d[n] + v[n][i].second;
      di(v[n][i].first);
    }
  }
}

/*void dfs(int n){
        visited[n] = 1;
        for(int i=0;i<v[n].size();i++){
                if(visited[v[n][i].first]==0){
                        dfs(v[n][i].first);
                        d[n] = max(d[n],d[v[n][i].first]+v[n][i].second);
                }
        }
}
*/
int main() {
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    cin >> s >> t >> w;
    v[s].push_back({t, w});
    v[t].push_back({s, w});
  }
  di(0);
  int a, b, va = 0, vb = 0;
  for (int i = 0; i < V; i++) {
    if (va < d[i]) {
      a = i;
      va = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    d[i] = 0;
  }
  di(a);
  for (int i = 0; i < V; i++) {
    if (vb < d[i]) {
      b = i;
      vb = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    ans[i] = d[i];
    d[i] = 0;
  }
  di(b);
  for (int i = 0; i < V; i++) {
    ans[i] = max(d[i], ans[i]);
    cout << ans[i] << endl;
  }
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef pair<int, int> P;
#define MAX 11451
vector<P> G[MAX];
vector<int> E[MAX];
map<int, int> M[MAX];
int ans[MAX];
int idx;
int dfs(int v, int p) {
  int res = 0;
  E[v].resize(G[v].size());
  for (int i = 0; i < (int)G[v].size(); i++) {
    P e = G[v][i];
    int u = e.first, c = e.second;
    if (u == p)
      continue;
    int tmp = dfs(u, v) + c;
    E[v][i] = tmp;
    M[v][tmp]++;
    res = max(res, tmp);
  }
  return res;
}
void dfs2(int v, int p, int d) {
  int res = d;
  M[v][d]++;
  for (int i = 0; i < (int)G[v].size(); i++) {
    P e = G[v][i];
    int u = e.first, c = e.second;
    if (u == p)
      continue;
    res = max(res, E[v][i]);
    M[v][E[v][i]]--;
    if (M[v][E[v][i]] == 0)
      M[v].erase(M[v].find(E[v][i]));
    int tmp = (--M[v].end())->first;
    dfs2(u, v, tmp + c);
    M[v][E[v][i]]++;
  }
  ans[v] = res;
}
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    // cin>>s>>t>>w;
    scanf("%d %d %d", &s, &t, &w);
    G[s].push_back(P(t, w));
    G[t].push_back(P(s, w));
  }
  dfs(0, -1);
  dfs2(0, -1, 0);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef pair<int, int> P;
vector<P> G[11451];
int dist[11451];
int idx;
void dfs(int v, int p, int d) {
  dist[v] = d;
  if (dist[idx] < dist[v])
    idx = v;
  for (P &e : G[v]) {
    if (e.first == p)
      continue;
    dfs(e.first, v, d + e.second);
  }
}
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    // cin>>s>>t>>w;
    scanf("%d %d %d", &s, &t, &w);
    G[s].push_back(P(t, w));
    G[t].push_back(P(s, w));
  }
  for (int i = 0; i < n; i++) {
    idx = i;
    dist[idx] = 0;
    dfs(i, -1, 0);
    // cout<<dist[idx]<<endl;
    printf("%d\\n", dist[idx]);
  }
  return 0;
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string.h>
#include <vector>
#define INF (1 << 30)
#define P pair<int, int>
using namespace std;

void dfs(int n, vector<vector<P>> &G, vector<int> &dist) {
  for (P p : G[n]) {
    int next = p.first, cost = p.second;
    if (dist[next] == INF) {
      dist[next] = dist[n] + cost;
      dfs(next, G, dist);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int u, v;
  vector<vector<P>> G(n);
  vector<int> dist(n, INF), dist_u(n, INF), dist_v(n, INF);
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].emplace_back(make_pair(t, w));
    G[t].emplace_back(make_pair(s, w));
  }

  dist[0] = 0;
  dfs(0, G, dist);
  auto maxIt = max_element(dist.begin(), dist.end());
  u = distance(dist.begin(), maxIt);

  dist_u[u] = 0;
  dfs(u, G, dist_u);
  maxIt = max_element(dist_u.begin(), dist_u.end());
  v = distance(dist_u.begin(), maxIt);

  dist_v[v] = 0;
  dfs(v, G, dist_v);
  for (int i = 0; i < n; i++) {
    cout << max(dist_u[i], dist_v[i]) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string.h>
#include <vector>
#define INF (1 << 30)
#define P pair<int, int>
using namespace std;

void dfs(int n, vector<vector<P>> &G, vector<int> &dist) {
  for (P p : G[n]) {
    int next = p.first, cost = p.second;
    if (dist[next] == INF) {
      dist[next] = dist[n] + cost;
      dfs(next, G, dist);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<P>> G(n);
  vector<int> dist(n);
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].emplace_back(make_pair(t, w));
    G[t].emplace_back(make_pair(s, w));
  }

  for (int i = 0; i < n; i++) {
    fill(dist.begin(), dist.end(), INF);
    dist[i] = 0;
    dfs(i, G, dist);
    cout << *max_element(dist.begin(), dist.end()) << endl;
  }
}
Problem p02373 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define MAXV 100010
#define MAXLV 20
using namespace std;

vector<int> e[MAXV];
int n, root = 0;

int parent[MAXLV][MAXV];
int depth[MAXV];

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (int i = 0; i < e[v].size(); i++) {
    if (e[v][i] != p)
      dfs(e[v][i], v, d + 1);
  }
}

void init() {
  dfs(root, -1, 0);
  for (int k = 0; k + 1 < MAXLV; k++) {
    for (int v = 0; v < n; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k = 0; k < MAXLV; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v)
    return u;
  for (int k = MAXLV - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

int main() {
  int q, u, v, k, c;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> c;
      e[i].push_back(c);
    }
  }
  init();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define MAXV 1000000
#define MAXLV 20
using namespace std;

vector<int> e[MAXV];
int n, root = 0;

int parent[MAXLV][MAXV];
int depth[MAXV];

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (int i = 0; i < e[v].size(); i++) {
    if (e[v][i] != p)
      dfs(e[v][i], v, d + 1);
  }
}

void init() {
  dfs(root, -1, 0);
  for (int k = 0; k + 1 < MAXLV; k++) {
    for (int v = 0; v < n; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k = 0; k < MAXLV; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v)
    return u;
  for (int k = MAXLV - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

int main() {
  int q, u, v, k, c;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> c;
      e[i].push_back(c);
    }
  }
  init();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
  return 0;
}
Problem p02373 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int N, Q;
vector<int> g[100000];
vector<int> query[100000];
int sz[100000];

int lca[100000];
bool buf[100000];

void sub(int idx) {
  sz[idx] = query[idx].size();
  for (auto &to : g[idx]) {
    sub(to);
    sz[idx] += query[to].size();
  }
}

void dfs(int idx, bool keep) {
  int heavy = -1;
  for (auto &to : g[idx]) {
    if (heavy == -1 || sz[to] > sz[heavy])
      heavy = to;
  }
  for (auto &to : g[idx]) {
    if (heavy != to)
      dfs(to, false);
  }

  if (~heavy) {
    dfs(heavy, true);
    for (int &v : query[idx]) {
      if (~lca[v])
        continue;
      if (buf[v]++)
        lca[v] = idx;
      else
        query[heavy].push_back(v);
    }
    query[idx].swap(query[heavy]);
  } else {
    for (int &v : query[idx]) {
      if (~lca[v])
        continue;
      if (buf[v]++)
        lca[v] = idx;
    }
  }

  for (auto &to : g[idx]) {
    if (to == heavy)
      continue;
    for (int &v : query[to]) {
      if (~lca[v])
        continue;
      if (buf[v]++)
        lca[v] = idx;
      else
        query[idx].push_back(v);
    }
  }

  if (!keep)
    for (auto &v : query[idx])
      buf[v] = false;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int K;
    scanf("%d", &K);
    while (K--) {
      int U;
      scanf("%d", &U);
      g[i].push_back(U);
    }
  }
  scanf("%d", &Q);

  memset(lca, -1, sizeof(lca));

  for (int i = 0; i < Q; i++) {
    int U, V;
    scanf("%d %d", &U, &V);
    if (U == V) {
      lca[i] = V;
    } else {
      query[U].push_back(i);
      query[V].push_back(i);
    }
  }

  sub(0);
  dfs(0, false);
  for (int i = 0; i < Q; i++) {
    printf("%d\\n", lca[i]);
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N, Q;
vector<int> g[100000];
vector<int> query[100000];
int sz[100000];

int lca[100000];
bool buf[100000];

void sub(int idx) {
  sz[idx] = query[idx].size();
  for (auto &to : g[idx]) {
    sub(to);
    sz[idx] += query[to].size();
  }
}

void dfs(int idx, bool keep) {
  int heavy = -1;
  for (auto &to : g[idx]) {
    if (heavy == -1 || sz[to] > sz[heavy])
      heavy = to;
  }
  for (auto &to : g[idx]) {
    dfs(to, false);
  }
  for (int &v : query[idx]) {
    buf[v] = true;
  }
  for (auto &to : g[idx]) {
    for (int &v : query[to]) {
      if (~lca[v])
        continue;
      if (buf[v]++)
        lca[v] = idx;
      query[idx].push_back(v);
    }
  }
  if (!keep)
    for (auto &v : query[idx])
      buf[v] = false;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int K;
    scanf("%d", &K);
    while (K--) {
      int U;
      scanf("%d", &U);
      g[i].push_back(U);
    }
  }
  scanf("%d", &Q);

  memset(lca, -1, sizeof(lca));

  for (int i = 0; i < Q; i++) {
    int U, V;
    scanf("%d %d", &U, &V);
    if (U == V) {
      lca[i] = V;
    } else {
      query[U].push_back(i);
      query[V].push_back(i);
    }
  }

  sub(0);
  dfs(0, 0);
  for (int i = 0; i < Q; i++) {
    printf("%d\\n", lca[i]);
  }
}
Problem p02373 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define fi first
#define se second

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vii> vvii;
typedef vector<int>::iterator vit;

const int MAXN = 100005;
const int LOGN = 20;

int idx[MAXN]; // i : index of node i in dfs path
ii path[MAXN << 1];
vi adjList[MAXN];

ii ST[LOGN][MAXN << 1];

void build(int n) {
  int h = (n == 1 ? 1 : ceil(log2(n)));

  for (int i = 0; i < n; i++)
    ST[0][i] = path[i];
  for (int i = 1; i < h; i++)
    for (int j = 0; j + (1 << i) <= n; j++)
      ST[i][j] = min(ST[i - 1][j], ST[i - 1][j + (1 << (i - 1))]);
}

int RMQ(int l, int r) { // range [l,r)
  int p = 31 - __builtin_clz(r - l);
  return min(ST[p][l], ST[p][r - (1 << p)]).se;
}

// build dfs path, depth
int cnt = 0;
void dfs(int curr, int d) {
  idx[curr] = cnt;

  path[cnt++] = ii(d, curr);
  for (vit it = adjList[curr].begin(); it != adjList[curr].end(); it++) {
    dfs(*it, d + 1);

    path[cnt++] = ii(d, curr);
  }
}

int N, M, Q, v;

int main() {

  scanf("%d", &N);
  for (int u = 0; u < N; u++) {
    scanf("%d", &M);
    for (int i = 0; i < M; i++) {
      scanf("%d", &v);
      adjList[u].push_back(v);
    }
  }

  // rooted as 0
  dfs(0, 0);
  build(cnt);

  scanf("%d", &Q);
  for (int i = 0, u, v, l, r; i < Q; i++) {
    scanf("%d%d", &u, &v);

    l = idx[u], r = idx[v];
    if (l > r)
      swap(l, r);

    printf("%d\\n", RMQ(l, r + 1));
  }

  return 0;
}

Error: MLE

Fixed submission:

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

#define fi first
#define se second

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vii> vvii;
typedef vector<int>::iterator vit;

const int MAXN = 100005;
const int LOGN = 50;

int idx[MAXN]; // i : index of node i in dfs path
ii path[MAXN << 1];
vi adjList[MAXN];

ii ST[LOGN][MAXN << 1];

void build(int n) {
  int h = (n == 1 ? 1 : ceil(log2(n)));

  for (int i = 0; i < n; i++)
    ST[0][i] = path[i];
  for (int i = 1; i < h; i++)
    for (int j = 0; j + (1 << i) <= n; j++)
      ST[i][j] = min(ST[i - 1][j], ST[i - 1][j + (1 << (i - 1))]);
}

int RMQ(int l, int r) { // range [l,r)
  int p = 31 - __builtin_clz(r - l);
  return min(ST[p][l], ST[p][r - (1 << p)]).se;
}

// build dfs path, depth
int cnt = 0;
void dfs(int curr, int d) {
  idx[curr] = cnt;

  path[cnt++] = ii(d, curr);
  for (vit it = adjList[curr].begin(); it != adjList[curr].end(); it++) {
    dfs(*it, d + 1);

    path[cnt++] = ii(d, curr);
  }
}

int N, M, Q, v;

int main() {

  scanf("%d", &N);
  for (int u = 0; u < N; u++) {
    scanf("%d", &M);
    for (int i = 0; i < M; i++) {
      scanf("%d", &v);
      adjList[u].push_back(v);
    }
  }

  // rooted as 0
  dfs(0, 0);
  build(cnt);

  scanf("%d", &Q);
  for (int i = 0, u, v, l, r; i < Q; i++) {
    scanf("%d%d", &u, &v);

    l = idx[u], r = idx[v];
    if (l > r)
      swap(l, r);

    printf("%d\\n", RMQ(l, r + 1));
  }

  return 0;
}