bool TEST = false; using namespace std; #include #include #define rep(i,n) for(ll (i)=0;(i)<(ll)(n);i++) #define rrep(i,n) for(ll (i)=(ll)(n)-1;(i)>=0;i--) #define range(i,start,end,step) for(ll (i)=start;(i)<(ll)(end);(i)+=(step)) #define rrange(i,start,end,step) for(ll (i)=start;(i)>(ll)(end);(i)+=(step)) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using pdd = pair; template using V = vector; template using VV = V>; template using P = pair; template using M = map; template using S = set; template using UM = unordered_map; template using PQ = priority_queue, greater>; template using rPQ = priority_queue, less>; templatevector make_vec(size_t a){return vector(a);} templateauto make_vec(size_t a, Ts... ts){return vector(ts...))>(a, make_vec(ts...));} template ostream& operator << (ostream& os, const pair v){os << "(" << v.first << ", " << v.second << ")"; return os;} template ostream& operator<<(ostream &os, const vector &v) { for (auto &e : v) os << e << ' '; return os; } template ostream& operator<<(ostream& os, const vector> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template void UNIQUE(vector &x) {sort(all(x));x.erase(unique(all(x)), x.end());} template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template void debug(vector>&v){for(ll i=0;i void debug(vector&v){if(v.size()!=0)cerr< void debug(priority_queue&v){V vals; while(!v.empty()) {cerr << v.top() << " "; vals.push_back(v.top()); v.pop();} cerr<<"\n"; for(auto val: vals) v.push(val);} template void debug(map&v){for(auto [k,v]: v) cerr << k spa v << "\n"; cerr<<"\n";} template void debug(unordered_map&v){for(auto [k,v]: v) cerr << k spa v << "\n";cerr<<"\n";} V listrange(int n) {V res(n); rep(i,n) res[i]=i; return res;} template P divmod(T a, T b) {return make_pair(a/b, a%b);} const ll INF = (1ll<<62); // const ld EPS = 1e-10; // const ld PI = acos(-1.0); using VALUE = char; VALUE unit = 1; VALUE merge(VALUE val, VALUE other) { VALUE tmp; if (val==0 || other==0) tmp = 0; else if (val==2 && other==2) tmp = 0; else tmp = max(val, other); return tmp; } VALUE add_node(VALUE val, int c, int D, int x) { VALUE tmp = val; if (cx) { tmp = 0; } else if (cx) { if (val==1) tmp = 2; } return tmp; } void Main(){ // tree input (unweighted) int n,u,v; int c; cin >> n; // unit = V(); int D = 0; VV> ns(n, V>()); rep(i,n-1) { cin >> u >> v >> c; u--; v--; ns[u].emplace_back(c,v); ns[v].emplace_back(c,u); chmax(D, c); } int root = 0; rep(u,n) if (ns[u].size()>=2) { root = u; break; } V d0(n); // d0[u]: subtree u V par; V pv(n, -1); V d1(n); // d1[u]: subtree par[u] auto sub = [&] (int x) { par = V(n, -1); par[root] = root; V order; stack s; s.push(root); while (!s.empty()) { auto u = s.top(); s.pop(); if (u<0) { u = ~u; auto val = unit; for (auto [c,v] : ns[u]) { if (par[v]!=u) continue; val = merge(val, add_node(d0[v], c, D, x)); } d0[u] = val; } else { order.push_back(u); s.push(~u); for (auto [c,v]: ns[u]) { if (par[v]>=0) continue; if (cx) return 0; s.push(v); par[v] = u; pv[v] = c; } } } d1[root] = unit; int res = 0; for (auto u: order) { V cl = {unit}; V cr = {unit}; VALUE vl = unit; VALUE vr = unit; rep(i, ns[u].size()) { v = ns[u][i].second; auto c = ns[u][i].first; if (par[v]==u) { vl = merge(vl, add_node(d0[v], c, D, x)); cl.push_back(vl); } v = ns[u][ns[u].size()-1-i].second; c = ns[u][ns[u].size()-1-i].first; if (par[v]==u) { vr = merge(vr, add_node(d0[v], c, D, x)); cr.push_back(vr); } } reverse(all(cr)); int i = 0; for (auto [c, v] : ns[u]) { if (par[v]!=u) continue; VALUE tmp = u==root? unit : add_node(d1[u], pv[u], D, x); auto val = merge(merge(tmp, cl[i]), cr[i+1]); d1[v] = val; // if (v==3) cout << "hoge" spa val spa tmp spa c << endl; i++; } } // cout << x spa root << endl; // debug(d0); // debug(d1); int ok = 0; rep(u,n) { V count(3); if (true) { auto c = pv[u]; count[add_node(d1[u], c, D, x)]++; } for (auto [c,v] : ns[u]) { if (par[u]==v) { count[add_node(d1[u], c, D, x)]++; } else { count[add_node(d0[v], c, D, x)]++; } } if (count[0]==0 && count[2]<=2) ok = 1; // cout << u << endl; // debug(count); } return ok; }; ll ng = -1; ll ok = D; while (abs(ng-ok)>1) { ll mm = (ok+ng)/2; if (sub(mm)) ok = mm; else ng = mm; } cout << ok << endl; } int main(void){ std::ifstream in("tmp_in"); if (TEST) { std::cin.rdbuf(in.rdbuf()); std::cout << std::fixed << std::setprecision(15); } else { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); } Main(); }