Problem p02834 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

struct node {
  int aoki;
  vector<int> neighbor;

  bool isLeaf() const { return neighbor.size() == 1; }
};

using graph = vector<node>;

void aoki(int s, graph &g) {
  function<void(int, int)> rec = [&](int v, int bv) {
    for (int u : g[v].neighbor) {
      if (u != bv) {
        g[u].aoki = g[v].aoki + 1;
        rec(u, v);
      }
    }
  };

  g[s].aoki = 0;
  rec(s, -1);
}

int f(int n) {
  static map<int, int> mp;
  if (mp[n]) {
    return mp[n];
  }

  if (n == 1) {
    return 0;
  } else if (n == 2) {
    return 1;
  } else {
    return mp[n] = 2 + f(n - 2);
  }
}

int takahashi(int s, graph &g) {
  function<int(int, int, int)> rec = [&](int v, int bv, int step) {
    int ret = 0;

    if (g[v].isLeaf()) {
      ret = step + f(g[v].aoki - step);
    }

    for (int u : g[v].neighbor) {
      if (u != bv && g[u].aoki - step > 0) {
        ret = max(ret, rec(u, v, step + 1));
      }
    }

    return ret;
  };

  return rec(s, -1, 0);
}

int main() {
  int n, u, v;
  cin >> n >> u >> v;
  --u;
  --v;

  graph g(n);

  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].neighbor.push_back(b);
    g[b].neighbor.push_back(a);
  }

  aoki(v, g);

  cout << takahashi(u, g) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

struct node {
  int aoki;
  vector<int> neighbor;

  bool isLeaf() const { return neighbor.size() == 1; }
};

using graph = vector<node>;

void aoki(int s, graph &g) {
  function<void(int, int)> rec = [&](int v, int bv) {
    for (int u : g[v].neighbor) {
      if (u != bv) {
        g[u].aoki = g[v].aoki + 1;
        rec(u, v);
      }
    }
  };

  g[s].aoki = 0;
  rec(s, -1);
}

int f(int n) { return n - 1; }

int takahashi(int s, graph &g) {
  function<int(int, int, int)> rec = [&](int v, int bv, int step) {
    int ret = 0;

    if (g[v].isLeaf()) {
      ret = step + f(g[v].aoki - step);
    }

    for (int u : g[v].neighbor) {
      if (u != bv && g[u].aoki - step > 0) {
        ret = max(ret, rec(u, v, step + 1));
      }
    }

    return ret;
  };

  return rec(s, -1, 0);
}

int main() {
  int n, u, v;
  cin >> n >> u >> v;
  --u;
  --v;

  graph g(n);

  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].neighbor.push_back(b);
    g[b].neighbor.push_back(a);
  }

  aoki(v, g);

  cout << takahashi(u, g) << endl;

  return 0;
}