#include #if __has_include() #include using namespace atcoder; #endif using namespace std; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define ll long long #define forin(in, n) \ for (ll i = 0; i < n; i++) \ cin >> in[i] #define forout(out) \ for (ll i = 0; i < (ll)out.size(); i++) \ cout << out[i] << endl #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep_up(i, a, n) for (ll i = a; i < n; ++i) #define rep_down(i, a, n) for (ll i = a; i >= n; --i) #define P pair #define pb push_back #define all(v) v.begin(), v.end() #define fi first #define se second #define vvvvll vector< vector > > > #define vvvll vector< vector< vector > > #define vvll vector< vector > #define vll vector #define pqll priority_queue #define pqllg priority_queue, greater> template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template using V = vector; constexpr ll INF = (1ll << 60); constexpr ll mod = 1000000007; //constexpr ll mod = 998244353; constexpr double pi = 3.14159265358979323846; template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template void pt(T val) { cout << val << "\n"; } template void pt_vll(vector &v) { ll vs = v.size(); rep(i, vs) { cout << v[i]; if (i == vs - 1) cout << "\n"; else cout << " "; } } ll mypow(ll a, ll n) { ll ret = 1; if (n == 0) return 1; if (a == 0) return 0; rep(i, n) { if (ret > (ll)(9e18 + 10) / a) return -1; ret *= a; } return ret; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } struct mint { ll x; constexpr mint(ll x = 0) noexcept : x((x % mod + mod) % mod) {} constexpr mint& operator+=(const mint& a) noexcept { if ((x += a.x) >= mod) x -= mod; return *this; } constexpr mint& operator-=(const mint& a) noexcept { if ((x += mod - a.x) >= mod) x -= mod; return *this; } constexpr mint& operator*=(const mint& a) noexcept { (x *= a.x) %= mod; return *this; } constexpr mint& operator/=(const mint& a) noexcept { return *this *= a.inv(); } constexpr mint operator-() const noexcept { return mint(-x); } constexpr mint operator+(const mint& a) const noexcept { return mint(*this) += a; } constexpr mint operator-(const mint& a) const noexcept { return mint(*this) -= a; } constexpr mint operator*(const mint& a) const noexcept { return mint(*this) *= a; } constexpr mint operator/(const mint& a) const noexcept { return mint(*this) /= a; } constexpr bool operator==(const mint& a) const noexcept { return x == a.x; } constexpr bool operator!=(const mint& a) const noexcept { return x != a.x; } constexpr mint pow(ll n) const { if (n == 0) return 1; mint res = pow(n >> 1); res *= res; if (n & 1) res *= *this; return res; } constexpr mint inv() const { return pow(mod - 2); } friend istream& operator>>(istream& is, mint& a) noexcept { ll v; is >> v; a = mint(v); return is; } friend ostream& operator<<(ostream& os, const mint& a) noexcept { return os << a.x; } }; struct UnionFind { vector par, size; UnionFind(ll N) : par(N) { //最初はすべてが根であるとして初期化 size.resize(N, 1); for (ll i = 0; i < N; i++) par[i] = i; } ll root(ll x) { //データxの木の根を再帰で得る if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(ll x, ll y) { //xとyの木を併合 ll rx = root(x); ll ry = root(y); if (rx == ry) return; //同じ木にあるときはそのまま if (size[rx] > size[ry]) { par[ry] = rx; size[rx] += size[ry]; } else { par[rx] = ry; size[ry] += size[rx]; } return; } bool same(ll x, ll y) { //2つのデータx,yが属する木が同じならtrue ll rx = root(x); ll ry = root(y); return rx == ry; } ll treesize(ll x) { return size[root(x)]; } }; const int MAX = 2010000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } // 二項係数計算 long long COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } vector enum_div(ll n) { //約数全列挙 vector ret; for (ll i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.push_back(i); if (i * i != n) { ret.push_back(n / i); } } } return ret; } void make_prime(vector &ret, ll n) { //素因数分解 ll x = n; for (ll i = 2; i * i <= x; i++) { while (n % i == 0) { n /= i; ret.push_back(i); } } if (n != 1) { ret.push_back(n); } return; } vector prime(1000010, true); vector pri(1000010); vector isprime(int N) { //素数判定 if (N >= 0) prime[0] = false; if (N >= 1) prime[1] = false; for (ll i = 2; i * i <= N; i++) { if (!prime[i]) { continue; } for (ll j = i * i; j <= N; j += i) { if (prime[j]) pri[j] = i; prime[j] = false; } } return prime; } map compression(vector v){ map ret; ll cnt = 0; sort(v.begin(),v.end()); for(ll i=0; i &ret, ll n) //素数判定 : vector isprime(int N) //約数全列挙: vector enum_div(ll n) struct Edge { long long to; long long cost; }; using Graph = vector >; multiset X,Y; ll tmp=INF; vll dist(200001,-1); vll dis(200001,-1); void dfs(Graph &G, ll v){ dist[v]=0; for(auto e:G[v]){ if(dist[e.to]==0) continue; X.insert(e.cost); dfs(G,e.to); } } void dfs2(Graph &G, ll v){ dis[v]=0; ll x,y,z; auto itr = X.end(); itr --; x = *itr; itr = Y.begin(); y = *itr; itr = Y.end(); itr --; z = *itr; chmin(tmp,max(x,z-y)); for(auto e:G[v]){ if(dis[e.to]==0) continue; X.erase(X.find(e.cost)); Y.insert(e.cost); dfs2(G,e.to); X.insert(e.cost); Y.erase(Y.find(e.cost)); } } void solve(){ ll n, m, k, cnt = 0, sum = 0, ans = 0; cin>>n; ll ma=0; if(n<2||n>200000){ pt(-1); return; } Graph G(n); UnionFind uf(n); vll A(n-1); vll B(n-1); vll C(n-1); rep(i,n-1){ ll a,b,c; cin>>a>>b>>c; if(a<1||a>n||b<1||b>n){ pt(-1); return; } if(c<1||c>1000000000){ pt(-1); return; } a--;b--; if(uf.same(a,b)){ pt(-1); return; } uf.unite(a,b); A[i]=a; B[i]=b; C[i]=c; if(chmax(ma,c)){ cnt=i; } } rep(i,n-1){ if(cnt==i) continue; G[A[i]].pb({B[i],C[i]}); G[B[i]].pb({A[i],C[i]}); } Y.insert(C[cnt]); X.insert(0); dfs(G,A[cnt]); dfs2(G,A[cnt]); ans=tmp; X.clear(); Y.clear(); Y.insert(C[cnt]); X.insert(0); tmp=INF; dfs(G,B[cnt]); dfs2(G,B[cnt]); chmax(ans,tmp); pt(ans); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); //cout << fixed << setprecision(16); //ll T; //cin>>T; //rep(ca,T) solve(); }