Problem p02834 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <vector>

int parent[18][100000];
int depth[100000];
std::vector<int> edge[100000];

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (auto to : edge[v]) {
    if (to != p)
      dfs(to, v, d + 1);
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    std::swap(u, v);
  for (int k = 0; k < 18; ++k) {
    if ((depth[v] - depth[u]) >> k & 1)
      v = parent[k][v];
  }
  if (u == v)
    return u;
  for (int k = 17; 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 n, u, v;
  scanf("%d%d%d", &n, &u, &v);
  --u, --v;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a, --b;
    edge[a].emplace_back(b);
    edge[b].emplace_back(a);
  }

  dfs(v, -1, 0);
  for (int k = 0; k < 18; ++k) {
    for (int j = 0; j < n; ++j) {
      if (parent[k][j] < 0)
        parent[k + 1][j] = -1;
      else
        parent[k + 1][j] = parent[k][parent[k][j]];
    }
  }

  std::vector<int> leaf;
  std::vector<int> lcav;
  for (int i = 0; i < n; ++i) {
    if (edge[i].size() == 1) {
      int ansc = lca(i, u);
      if (ansc == v)
        continue;
      int tak_to_lca = depth[u] - depth[ansc];
      int aok_to_lca = depth[ansc];
      if (aok_to_lca <= tak_to_lca)
        continue;
      leaf.emplace_back(i);
      lcav.emplace_back(ansc);
    }
  }

  int ans = 0;
  for (int i = 0; i < leaf.size(); ++i) {
    int r = depth[u] - depth[lcav[i]];
    r += depth[leaf[i]] - depth[lcav[i]];
    r += (depth[leaf[i]] - r) - 1;
    if (r > ans)
      ans = r;
  }

  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

int parent[20][100000];
int depth[100000];
std::vector<int> edge[100000];

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (auto to : edge[v]) {
    if (to != p)
      dfs(to, v, d + 1);
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    std::swap(u, v);
  for (int k = 0; k < 18; ++k) {
    if ((depth[v] - depth[u]) >> k & 1)
      v = parent[k][v];
  }
  if (u == v)
    return u;
  for (int k = 17; 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 n, u, v;
  scanf("%d%d%d", &n, &u, &v);
  --u, --v;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a, --b;
    edge[a].emplace_back(b);
    edge[b].emplace_back(a);
  }

  dfs(v, -1, 0);
  for (int k = 0; k < 18; ++k) {
    for (int j = 0; j < n; ++j) {
      if (parent[k][j] < 0)
        parent[k + 1][j] = -1;
      else
        parent[k + 1][j] = parent[k][parent[k][j]];
    }
  }

  std::vector<int> leaf;
  std::vector<int> lcav;
  for (int i = 0; i < n; ++i) {
    if (edge[i].size() == 1) {
      int ansc = lca(i, u);
      if (ansc == v)
        continue;
      int tak_to_lca = depth[u] - depth[ansc];
      int aok_to_lca = depth[ansc];
      if (aok_to_lca <= tak_to_lca)
        continue;
      leaf.emplace_back(i);
      lcav.emplace_back(ansc);
    }
  }

  int ans = 0;
  for (int i = 0; i < leaf.size(); ++i) {
    int r = depth[u] - depth[lcav[i]];
    r += depth[leaf[i]] - depth[lcav[i]];
    r += (depth[leaf[i]] - r) - 1;
    if (r > ans)
      ans = r;
  }

  printf("%d\n", ans);
  return 0;
}