Language: C++

#include <iostream>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <queue>
#include <cstdio>
typedef long long ll;
#define INF 0x3f3f3f3f

using namespace std;
struct Node {
	int parent;
	int dis;
	int depth;
	vector<int> child;
};
int N;
int graph[100000 + 10][100000 + 10];
int parent[100000 + 10];
int depth[100000 + 10];
int dis[100000 + 10];
bool visited[100000 + 10];
int root;


void dfs(int v, int p, int d) {
	parent[v] = p;
	depth[v] = d;
	//printf("visited %d and %d is parent\n", v, p);
	for (int i = 1; i <= N; i++) {
		if (i != p && graph[v][i] > 0) {
			dis[i] = graph[v][i];
			dfs(i, v, d + 1);
		}
	}
}

void build() {
	int a, b, c;
	scanf("%d", &N);
	for(int i = 0; i < N - 1; i++) {
		scanf("%d%d%d", &a, &b, &c);
		graph[a][b] = graph[b][a] = c;
	}
	
	dfs(1, 0, 0);
}

long long lca(int u, int v) {
	long long ans = 0;
	while(depth[u] > depth[v]) {
		ans += dis[u];
		u = parent[u];
	}
	while(depth[v] > depth[u]) {
		ans += dis[v];
		v = parent[v];
	}
	while(u != v) {
		ans += dis[u] + dis[v];
		u = parent[u];
		v = parent[v];
	}
	return ans;
}

long long hamPath(int start, int len) {
	if(len == N) {
		return 0;
	}
	long long ans = 0;
	visited[start] = true;
	for(int i = 1; i <= N; i++) {
		if(visited[i] == false) {
			ans = max(ans, hamPath(i, len + 1) + graph[start][i]);
		}
	}
	visited[start] = false;
	return ans;
}

void solve() {
	build();
	for(int i = 1; i <= N; i++) {
		for(int j = i + 1; j <= N; j++) {
			graph[i][j] = graph[j][i] = lca(i, j);
		}
	}
	
	long long ans = 0;
	for(int i = 1; i<= N; i++) {
		memset(visited, false, sizeof(visited));
		ans = max(ans, hamPath(i, 1));
	}
	printf("%lld\n", ans);
}

int main(int argc, char *argv[]) {
	solve();
	/*
	build();
	cout << lca(1, 5) << endl;
	for(int i = 1; i <= N; i++) 
		cout << parent[i] << " ";
	cout << endl;
	for(int i = 1; i <= N; i++) 
		cout << dis[i] << " ";
	cout << endl;
	for(int i = 1; i <= N; i++) 
		cout << depth[i] << " ";
	cout << endl;
	
	for(int i = 1; i <= N; i++) {
		for(int j = 1; j <= N; j++) {
			cout << lca(i, j) << " ";
		}
		cout << endl;
	}*/
}

Compiler output:
/tmp/ccMjOyKJ.o: in function `dfs(int, int, int)':
a.cc:(.text+0x21): relocation truncated to fit: R_X86_64_PC32 against symbol `parent' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x3b): relocation truncated to fit: R_X86_64_PC32 against symbol `depth' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0xbc): relocation truncated to fit: R_X86_64_PC32 against symbol `dis' defined in .bss section in /tmp/ccMjOyKJ.o
/tmp/ccMjOyKJ.o: in function `lca(int, int)':
a.cc:(.text+0x209): relocation truncated to fit: R_X86_64_PC32 against symbol `dis' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x226): relocation truncated to fit: R_X86_64_PC32 against symbol `parent' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x240): relocation truncated to fit: R_X86_64_PC32 against symbol `depth' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x257): relocation truncated to fit: R_X86_64_PC32 against symbol `depth' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x274): relocation truncated to fit: R_X86_64_PC32 against symbol `dis' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x291): relocation truncated to fit: R_X86_64_PC32 against symbol `parent' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x2ab): relocation truncated to fit: R_X86_64_PC32 against symbol `depth' defined in .bss section in /tmp/ccMjOyKJ.o
a.cc:(.text+0x2c2): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status