Language: C++

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

int N;
vector<int> Tree[100000];

int DISTANCE[100000][100000] = {};

bool in(int item, vector<int> list)
{
    for (int i = 0; i < list.size(); i++) {
        if (item == list[i]) return true;
    }

    return false;
}

int distance (int from, int to, int prev)
{
    if (from == to) return 0;

    if (DISTANCE[from][to] > 0) return DISTANCE[from][to];

    vector<int> neighbors = Tree[from];
    if (in(to, neighbors)) return DISTANCE[from][to] = DISTANCE[to][from] = 1;

    int minimum = INT_MAX;
    for (int i = 0; i < neighbors.size(); i++) {
        if (prev == neighbors[i]) continue;
        int next = distance(neighbors[i], to, from) + 1;
        minimum = next < minimum ? next : minimum;
    }

    return DISTANCE[from][to] = DISTANCE[to][from] = minimum;
}

int aoki (int self, int opp)
{
    int size = Tree[self].size();
    if (size == 1) return Tree[self][0];

    int minimum = INT_MAX;
    int node = -1;
    for (int i = 0; i < size; i++) {
        int d = distance(Tree[self][i], opp, -1);
        if (d < minimum) {
            minimum = d;
            node = Tree[self][i];
        }
    }

    return node;
}

int DFS[100000][100000];
int dfs (int u, int v)
{
    if (u == v) return 0;

    if (DFS[u][v] >= 0) return DFS[u][v];

    int size = Tree[u].size();
    int next_u = 0;
    if (size == 1) {
        next_u = Tree[u][0];
        if (next_u == v) return DFS[u][v] = 0;
    }

    int max_turns = 0;
    for (int i = 0; i < size; i++) {
        next_u = Tree[u][i];
        if (next_u == v) continue;

        int next_v = aoki(v, next_u);
        int turns = DFS[u][v] = dfs(next_u, next_v) + 1;
        max_turns = turns > max_turns ? turns : max_turns;
    }

    return max_turns;
}

int main ()
{
    cin >> N;
    int u, v; cin >> u >> v;

    for (int i = 0; i < N; i++) {
        Tree[i] = {};
    }

    int a, b;
    for (int i = 0; i < N; i++) {
        cin >> a >> b;
        Tree[a].push_back(b);
        Tree[b].push_back(a);
    }

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            DFS[i][j] = -1;
        }
    }
    cout << dfs(u, v) << endl;

    return 0;
}

Compiler output:
/tmp/ccuXInG1.o: in function `dfs(int, int)':
a.cc:(.text+0x4b9): relocation truncated to fit: R_X86_64_PC32 against symbol `DFS' defined in .bss section in /tmp/ccuXInG1.o
a.cc:(.text+0x4e4): relocation truncated to fit: R_X86_64_PC32 against symbol `DFS' defined in .bss section in /tmp/ccuXInG1.o
a.cc:(.text+0x580): relocation truncated to fit: R_X86_64_PC32 against symbol `DFS' defined in .bss section in /tmp/ccuXInG1.o
a.cc:(.text+0x5ab): relocation truncated to fit: R_X86_64_PC32 against symbol `DFS' defined in .bss section in /tmp/ccuXInG1.o
a.cc:(.text+0x64c): relocation truncated to fit: R_X86_64_PC32 against symbol `DFS' defined in .bss section in /tmp/ccuXInG1.o
a.cc:(.text+0x673): relocation truncated to fit: R_X86_64_PC32 against symbol `DFS' defined in .bss section in /tmp/ccuXInG1.o
/tmp/ccuXInG1.o: in function `main':
a.cc:(.text+0x818): relocation truncated to fit: R_X86_64_PC32 against symbol `DFS' defined in .bss section in /tmp/ccuXInG1.o
collect2: error: ld returned 1 exit status