#include namespace { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic pop using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) { a = max(a, b); } template void chmin(T& a, const T& b) { a = min(a, b); } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; struct HLD { std::vector> &to; int root, n; std::vector sz, parent, depth, idx, ridx, head, inv; HLD(std::vector>& to, int root=0): to(to), root(root), n(to.size()), sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n) { init_tree_data(root); int x = 0; assign_idx(root, root, x); } void init_tree_data(int u, int p=-1, int d=0) { parent[u] = p; depth[u] = d; int s = 1; for(int v: to[u]) { if (v == p) continue; init_tree_data(v, u, d+1); s += sz[v]; } sz[u] = s; } void assign_idx(int u, int h, int &nxt, int p=-1) { head[u] = h; inv[nxt] = u; idx[u] = nxt++; if (sz[u] == 1) { ridx[u] = nxt; return; } int mxsize = 0; int mi; for(int v: to[u]) { if (v == p) continue; if (sz[v] > mxsize) { mxsize = sz[v]; mi = v; } } assign_idx(mi, h, nxt, u); for(int v: to[u]) { if (v == p || v == mi) continue; assign_idx(v, v, nxt, u); } ridx[u] = nxt; } int lca(int u, int v) { while(head[u] != head[v]) { if (depth[head[u]] > depth[head[v]]) u = parent[head[u]]; else v = parent[head[v]]; } return (depth[u] < depth[v] ? u : v); } // returns (paths upto lca from x (excluding lca), those from y, lca) std::tuple>, std::vector>, int> paths(int x, int y) { std::tuple>, std::vector>, int> ret; std::vector>& x_paths = get<0>(ret); std::vector>& y_paths = get<1>(ret); int& lca = get<2>(ret); while(head[x] != head[y]) { int xhead = head[x], yhead = head[y]; if (depth[xhead] > depth[yhead]) { x_paths.emplace_back(x, xhead); x = parent[xhead]; } else { y_paths.emplace_back(y, yhead); y = parent[yhead]; } } if (depth[x] > depth[y]) { int ychild = inv[idx[y] + 1]; x_paths.emplace_back(x, ychild); x = y; } else if (depth[x] < depth[y]) { int xchild = inv[idx[x] + 1]; y_paths.emplace_back(y, xchild); y = x; } lca = x; return ret; } int dist(int u, int v) { int w = lca(u, v); return depth[u] + depth[v] - 2 * depth[w]; } }; template struct DisjointSparseTable { const int n; const vector msb; const vector> d; DisjointSparseTable(vector a) : n(a.size()), msb(build_msb_table(n)), d(build_table(move(a))) {} vector build_msb_table(int n) { if (n <= 1) return {}; unsigned char k_max = 32 - __builtin_clz(n - 1); vector res(1 << k_max); unsigned char* p = res.data(); for (unsigned char k = 0; k < k_max; k++) { memset(p + (1U << k), k, 1U << k); } return res; } vector> build_table(vector a) { const int n = a.size(); vector> res(1); if (n >= 2) { const int i_max = n - 1, k_max = 32 - __builtin_clz(i_max); res.resize(k_max); for (int k = 1; k < k_max; k++) { vector t(i_max >> k & 1 ? n : i_max & ~0U << k); for (int c = 1 << k; c < n; c += 1 << (k + 1)) { int l = c - (1 << k); int r = min(n, c + (1 << k)); t[c - 1] = a[c - 1]; for (int i = c - 2; i >= l; i--) t[i] = op(a[i], t[i + 1]); t[c] = a[c]; for (int i = c + 1; i < r; i++) t[i] = op(t[i - 1], a[i]); } res[k] = move(t); } } res[0] = move(a); return res; } S query(int l, int r) { return query_closed(l, r - 1); } S query_closed(int l, int r) { assert(0 <= l && l <= r && r < n); if (l == r) return d[0][l]; auto k = msb[l ^ r]; return op(d[k][l], d[k][r]); } }; int op_min(int x, int y) { return min(x, y); } int op_max(int x, int y) { return max(x, y); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; struct E { int a, b, c; }; VVI to(n); vector es(n - 1); for(auto& [a, b, c] : es) { cin >> a >> b >> c; a--, b--; to[a].emplace_back(b); to[b].emplace_back(a); } HLD hld(to); VI init_vec(n); for(auto& [a, b, c] : es) { if (hld.depth[a] < hld.depth[b]) swap(a, b); init_vec[hld.idx[a]] = c; } constexpr int INF = 1001001001; DisjointSparseTable RmQ(init_vec); DisjointSparseTable RMQ(init_vec); vector ord(n - 1); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](int i, int j) { return es[i].c > es[j].c; }); ord.emplace_back(n - 1); es.push_back({-1, -1, -1}); int u = -1, v = -1; int ans = INF; rep(i, n - 1) { auto [a, b, c] = es[ord[i]]; if (i == 0) { u = a; v = b; } else { int d = hld.dist(u, v); if (hld.dist(a, u) + hld.dist(a, v) == d && hld.dist(b, u) + hld.dist(b, v) == d) { ; } else { int nu = -1, nv = -1; if (hld.dist(a, u) > hld.dist(b, u)) nu = a; else nu = b; if (hld.dist(a, u) > hld.dist(a, v)) nv = u; else nv = v; int nd = hld.dist(nu, nv); bool ok = true; for (int t : {a, b, u, v}) { if (hld.dist(t, nu) + hld.dist(t, nv) != nd) { ok = false; break; } } if (!ok) break; u = nu; v = nv; } } int mn = INF, mx = -INF; auto [p1, p2, _] = hld.paths(u, v); rep(_, 2) { for (auto [x, y] : p1) { int ix = hld.idx[x], iy = hld.idx[y]; chmin(mn, RmQ.query_closed(iy, ix)); chmax(mx, RMQ.query_closed(iy, ix)); } swap(p1, p2); } chmin(ans, max(mx - mn, es[ord[i + 1]].c)); } cout << ans << '\n'; }