#include #include #include #include #include using namespace std; using ll = long long; using P = pair; int N; vector> G; pair bfs(int start) { vector dist(N + 1, LLONG_MIN); queue q; dist[start] = 0; q.push(start); int far = start; while (!q.empty()) { int cur = q.front(); q.pop(); for (auto [to, cost] : G[cur]) { if (dist[to] == LLONG_MIN) { dist[to] = dist[cur] + cost; q.push(to); if (dist[to] > dist[far]) { far = to; } } } } return {far, dist[far]}; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; G.resize(N + 1); // 1-indexed for (int i = 0; i < N - 1; ++i) { int u, v; ll w; cin >> u >> v >> w; G[u].emplace_back(v, w); G[v].emplace_back(u, w); } // 1回目のBFSで遠い点を見つける int farthest = bfs(1).first; // 2回目のBFSで直径を求める ll diameter = bfs(farthest).second; cout << diameter << '\n'; return 0; }