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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct LCA {
  const int LOGM = 30;
  vector<int> depth;
  vector<vector<int>> parent;
  vector<pair<int, long long>> pare;
  vector<vector<long long>> parmax;
  LCA(int root, const vector<vector<int>> &g,
      const vector<pair<int, long long>> &pare)
      : pare(pare) {
    int n = g.size();
    depth.resize(n);
    parent.resize(LOGM);
    parmax.resize(LOGM);
    for (int i = 0; i < LOGM; i++) {
      parent[i].resize(n);
      parmax[i].resize(n);
    }
    function<void(int, int, int)> dfs = [&](int u, int prev, int d) {
      parent[0][u] = prev;
      parmax[0][u] = pare[u].second;
      depth[u] = d;
      for (auto v : g[u])
        if (v != prev)
          dfs(v, u, d + 1);
    };
    dfs(root, -1, 0);
    for (int k = 0; k < LOGM - 1; k++) {
      for (int i = 0; i < n; i++) {
        if (parent[k][i] < 0) {
          parent[k + 1][i] = -1;
        } else {
          parent[k + 1][i] = parent[k][parent[k][i]];
          if (parent[k + 1][i] >= 0) {
            parmax[k + 1][i] = max(parmax[k][i], parmax[k][parent[k][i]]);
          }
        }
      }
    }
  }
  int lca(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);
    for (int k = 0; k < LOGM; k++) {
      if ((depth[v] - depth[u]) >> k & 1) {
        v = parent[k][v];
      }
    }
    if (u == v)
      return u;
    for (int k = LOGM - 1; k >= 0; k--) {
      if (parent[k][u] != parent[k][v]) {
        u = parent[k][u];
        v = parent[k][v];
      }
    }
    return parent[0][u];
  }
  pair<int, long long> get(int a, int l) {
    long long res = 0;
    int d = depth[a] - depth[l] - 1;
    if (d <= 0)
      return {a, 0};
    for (int k = 0; k < LOGM; k++) {
      if ((d >> k) & 1) {
        res = max(res, parmax[k][a]);
        a = parent[k][a];
      }
    }
    return {a, res};
  }
  int dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
};

int main() {
  int n;
  scanf("%d", &n);
  vector<int> w(n);
  vector<vector<int>> g(n);
  long long tot = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &w[i]);
    tot += w[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<long long> sum(n);
  vector<long long> maxx(n);
  vector<pair<int, long long>> pare(n);
  function<void(int, int)> dfs = [&](int u, int prev) {
    pare[u].first = prev;
    sum[u] = w[u];
    vector<pair<long long, int>> val;
    int cnt = 0;
    for (auto v : g[u])
      if (v != prev) {
        dfs(v, u);
        sum[u] += sum[v];
        val.emplace_back(sum[v], v);
        maxx[u] = max(maxx[u], sum[v]);
        cnt++;
      }
    sort(val.rbegin(), val.rend());
    if (cnt >= 2) {
      for (auto v : g[u])
        if (v != prev) {
          if (val[0].second != v) {
            pare[v].second = val[0].first;
          } else {
            pare[v].second = val[1].first;
          }
        }
    }
  };
  dfs(0, -1);
  vector<vector<pair<long long, int>>> ma(n);
  for (int i = 0; i < n; i++) {
    for (auto v : g[i])
      if (v != pare[i].first) {
        ma[i].emplace_back(sum[v], v);
      }
    sort(ma[i].rbegin(), ma[i].rend());
  }
  LCA lca(0, g, pare);
  /*
  for (int i = 0; i < n; i ++) {
          for (int k = 0; k < 30; k ++) {
                  cerr << lca.parmax[k][i] << ' ';
          }
          cerr << endl;
  }
  */
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    int l = lca.lca(a, b);
    long long ans = tot - sum[l];

    if (a == l && b == l) {
      ans = max(ans, maxx[l]);
      printf("%lld\\n", ans);
      continue;
    }

    if (a == l || b == l) {
      if (a == l)
        swap(a, b);
      ans = max(ans, maxx[a]);
      auto geta = lca.get(a, l);
      int va = geta.first;
      ans = max(ans, geta.second);
      ans = max(ans, pare[va].second);
      printf("%lld\\n", ans);
      continue;
    }
    ans = max(ans, maxx[a]);
    ans = max(ans, maxx[b]);
    auto geta = lca.get(a, l);
    auto getb = lca.get(b, l);
    int va = geta.first;
    int vb = getb.first;
    ans = max(ans, geta.second);
    ans = max(ans, getb.second);
    int top1 = ma[l][0].second;
    int top2 = ma[l][1].second;
    if ((int)g[l].size() >= 4 || ((int)g[l].size() >= 3 && l == 0)) {
      if (top1 == va) {
        if (top2 == vb) {
          ans = max(ans, ma[l][2].first);
        } else {
          ans = max(ans, ma[l][1].first);
        }
      } else if (top1 == vb) {
        if (top2 == va) {
          ans = max(ans, ma[l][2].first);
        } else {
          ans = max(ans, ma[l][1].first);
        }
      } else {
        ans = max(ans, ma[l][0].first);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct LCA {
  const int LOGM = 30;
  vector<int> depth;
  vector<vector<int>> parent;
  LCA(int root, const vector<vector<int>> &g) {
    int n = g.size();
    depth.resize(n);
    parent.resize(LOGM);
    for (int i = 0; i < LOGM; i++)
      parent[i].resize(n);
    function<void(int, int, int)> dfs = [&](int u, int prev, int d) {
      parent[0][u] = prev;
      depth[u] = d;
      for (auto v : g[u])
        if (v != prev)
          dfs(v, u, d + 1);
    };
    dfs(root, -1, 0);
    for (int k = 0; k < LOGM - 1; k++) {
      for (int i = 0; i < n; i++) {
        if (parent[k][i] < 0)
          parent[k + 1][i] = -1;
        else
          parent[k + 1][i] = parent[k][parent[k][i]];
      }
    }
  }
  int lca(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);
    for (int k = 0; k < LOGM; k++) {
      if ((depth[v] - depth[u]) >> k & 1) {
        v = parent[k][v];
      }
    }
    if (u == v)
      return u;
    for (int k = LOGM - 1; k >= 0; k--) {
      if (parent[k][u] != parent[k][v]) {
        u = parent[k][u];
        v = parent[k][v];
      }
    }
    return parent[0][u];
  }
  int dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
};

int main() {
  int n;
  scanf("%d", &n);
  vector<int> w(n);
  vector<vector<int>> g(n);
  long long tot = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &w[i]);
    tot += w[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<long long> sum(n);
  vector<long long> maxx(n);
  vector<map<int, long long>> maxy(n);
  vector<int> par(n, -1);
  function<void(int, int)> dfs = [&](int u, int prev) {
    par[u] = prev;
    sum[u] = w[u];
    /*
    if (u == 0) {
            maxy[u].resize(g[u].size());
    } else {
            maxy[u].resize((int) g[u].size() - 1);
    }
    */
    vector<pair<long long, int>> val;
    int cnt = 0;
    for (auto v : g[u])
      if (v != prev) {
        dfs(v, u);
        sum[u] += sum[v];
        val.emplace_back(sum[v], v);
        maxx[u] = max(maxx[u], sum[v]);
      }
    sort(val.rbegin(), val.rend());
    if (cnt != 1) {
      for (auto v : g[u])
        if (v != prev) {
          if (val[0].second != v) {
            maxy[u][v] = val[0].first;
          } else {
            maxy[u][v] = val[1].first;
          }
        }
    }
  };
  dfs(0, -1);
  /*
  cerr << "OK" << endl;
  for (int i = 0; i < n; i ++) {
          for (auto it : maxy[i]) {
                  cerr << it.second << ' ';
          }
          cerr << endl;
  }
  for (int i = 0; i < n; i ++) {
          cerr << par[i] << ' ';
  }
  cerr << endl;
  */
  int q;
  scanf("%d", &q);
  LCA lca(0, g);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    int l = lca.lca(a, b);
    int cur = a;
    int preva = -1;
    long long ans = 0;
    while (cur != l) {
      if (preva == -1) {
        ans = max(ans, maxx[cur]);
      } else {
        ans = max(ans, maxy[cur][preva]);
      }
      preva = cur;
      cur = par[cur];
    }
    cur = b;
    int prevb = -1;
    while (cur != l) {
      if (prevb == -1) {
        ans = max(ans, maxx[cur]);
      } else {
        ans = max(ans, maxy[cur][prevb]);
      }
      prevb = cur;
      cur = par[cur];
    }
    ans = max(ans, tot - sum[l]);
    for (auto v : g[l])
      if (v != par[l] && v != preva && v != prevb) {
        ans = max(ans, sum[v]);
      }
    // cerr << preva << ' ' << prevb << endl;
    printf("%lld\\n", ans);
  }
  return 0;
}