#include #include #include #include # define int long long using namespace std; int n; vector>> graph; pair bfs(int start) { vector dist(n + 1, -1); queue q; dist[start] = 0; q.push(start); while (!q.empty()) { int node = q.front(); q.pop(); for (auto [neighbor, weight] : graph[node]) { if (dist[neighbor] == -1) { dist[neighbor] = dist[node] + weight; q.push(neighbor); } } } int max_dist = 0, farthest_node = start; for (int i = 1; i <= n; ++i) { if (dist[i] > max_dist) { max_dist = dist[i]; farthest_node = i; } } return {farthest_node, max_dist}; } signed main() { cin >> n; graph.resize(n + 1); for (int i = 0; i < n - 1; ++i) { int u, v, w; cin >> u >> v >> w; graph[u].emplace_back(v, w); graph[v].emplace_back(u, w); } auto [u, dist1] = bfs(1); auto [v, diameter] = bfs(u); cout << diameter << endl; return 0; }