#include #include #include using namespace std; struct Edge { int to, weight; }; int N; // 頂点の数 vector> graph; pair dfs(int v, int parent) { pair farthest = {0, v}; // {最長距離, 頂点番号} for (const Edge& edge : graph[v]) { if (edge.to == parent) continue; // 親ノードには戻らない pair result = dfs(edge.to, v); result.first += edge.weight; // 頂点 v から子ノードへの距離を加算 if (result.first > farthest.first) { farthest = result; // 最長距離を更新 } } return farthest; } int main() { cin >> N; graph.resize(N); // 辺の入力 for (int i = 0; i < N - 1; ++i) { int u, v, w; cin >> u >> v >> w; u--; v--; // 0-indexedに調整 graph[u].push_back({v, w}); graph[v].push_back({u, w}); } // 任意の頂点(0)から最も遠い頂点を見つける pair farthest_from_start = dfs(0, -1); // 最も遠い頂点からさらに最も遠い頂点を見つける(直径の長さを求める) pair farthest_from_farthest = dfs(farthest_from_start.second, -1); cout << farthest_from_farthest.first << endl; // 直径(最大距離) return 0; }