/* #include void self::main(){ LL(N); VV(N-1,a,b);dec(a,b); struct node{ struct S{ bool is_empty; ll diameter; ll max_dep; }; static S trans_edge(S x,const edge_rev_index&e){ x.max_dep++; return x; }; static S op(S a,S b){ if(a.is_empty)return b; if(b.is_empty)return a; return{ false, max(a.diameter,b.diameter,a.max_dep+b.max_dep), max(a.max_dep,b.max_dep) }; }; static S trans_vertex(S x,ll u){ return{ false, max(x.diameter,x.max_dep), x.max_dep }; }; static S e(){return{true,0,0};} }; RerootingDPTreeNodeAliasrt(N); rt.add_edges(a,b); rt.build(); auto dp=rt.dp(); ll ans=oo; fo(u,N){ fo(i,rt[u].size()){ ll v=rt[u][i].to; ll j=rt[u][i].rev_idx; amin(ans,max( dp[u][i].diameter, dp[v][j].diameter, ceil(dp[u][i].diameter,2)+ceil(dp[v][j].diameter,2)+1 // 接続による直径を最小化するには,中心同士を接続する. )); } } pp(ans); } */ #include using namespace std; namespace self{void main();void sub();void pp(auto&&...a);} int main(){cin.tie(nullptr)->sync_with_stdio(0);cout<__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resize(n);vin(__VA_ARGS__) #define VV(n,...) RDVV(ll,n,__VA_ARGS__) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range(0,__VA_ARGS__);i(1,__VA_ARGS__);i>=i##stop;i+=i##step) #define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a) namespace self{ using ll=long long; constexpr auto refs(auto&...a){return array{ref(a)...};} templateconstexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} templateconstexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};} void lin(auto&...a){(cin>>...>>a);} void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);} constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;} templateconstexpr ll maxsize10(){return size10(numeric_limits::max());} auto ceil(auto x,auto y){if(y<0)x=-x,y=-y;return x<=0?x/y:(x-1)/y+1;} bool amin(auto&a,const auto&b){return b>{a...});} templatestruct infinity{ templatestatic constexpr T ones(size_t n){return n?ones(n-1)*10+1:0;} templateconstexpr operator T()const{return ones(maxsize10())*(1-neg*2);} }; constexpr infinity oo; void dec(auto&...a){((--a),...);} templateusing pack_back_t=tuple_element_t>; } namespace self{ templatestruct vec; templatestruct hvec_helper{using type=vec::type>;}; templatestruct hvec_helper<0,T>{using type=T;}; templateusing hvec=hvec_helper::type; templatestruct vec:vector{ using vector::vector; vec&operator--(){fe(*this,e)--e;return*this;} ll size()const{return vector::size();} auto&emplace_back(auto&&...a){vector::emplace_back(std::forward(a)...);return*this;} }; templaterequires(sizeof...(A)>=2)vec(const A&...a)->vec>>; } namespace self{ templatestruct edge_rev_index{ int from,to; WT wt; int id; int rev_idx; edge_rev_index()=default; edge_rev_index(int from,int to,WT wt,int id,int rev_idx):from(from),to(to),wt(wt),id(id),rev_idx(rev_idx){} }; } namespace self{ template&),S op(S,S),S trans_vertex(S,ll),S e()>class RerootingDPTree{ private: ll n_; vec>>edges_; vec>dp_; public: RerootingDPTree(ll n):n_(n),edges_(n),dp_(n){} const auto&operator[](ll i)const{return edges_[i];} void add_edges(const vec&a,const vec&b){ fo(i,a.size()){ edges_[a[i]].eb(a[i],b[i],1,i,edges_[b[i]].size()); edges_[b[i]].eb(b[i],a[i],1,i,edges_[a[i]].size()-1); } } ll deg(ll u)const{return edges_[u].size();} S init_dfs(ll u,ll p){ S cum=e(); dp_[u].resize(deg(u)); fo(i,deg(u))if(ll v=edges_[u][i].to;v!=p){ dp_[u][i]=trans_edge(init_dfs(v,u),edges_[u][i]); cum=op(cum,dp_[u][i]); } return trans_vertex(cum,u); }; void reroot_dfs(ll u,ll p){ vec left(deg(u),e()),right(deg(u),e()); fo(i,left.size()-1)left[i+1]=op(left[i],dp_[u][i]); of(i,right.size(),1)right[i-1]=op(dp_[u][i],right[i]); fo(i,deg(u))if(ll v=edges_[u][i].to;v!=p){ ll j=edges_[u][i].rev_idx; dp_[v][j]=trans_edge(trans_vertex(op(left[i],right[i]),u),edges_[v][j]); reroot_dfs(v,u); } }; void build(){ init_dfs(0,-1); reroot_dfs(0,-1); } auto dp()const{return dp_;} }; templateusing RerootingDPTreeNodeAlias=RerootingDPTree; } void self::pp(auto&&...a){const char*c="";((cout<&e){ x.max_dep++; return x; }; static S op(S a,S b){ if(a.is_empty)return b; if(b.is_empty)return a; return{ false, max(a.diameter,b.diameter,a.max_dep+b.max_dep), max(a.max_dep,b.max_dep) }; }; static S trans_vertex(S x,ll u){ return{ false, max(x.diameter,x.max_dep), x.max_dep }; }; static S e(){return{true,0,0};} }; RerootingDPTreeNodeAliasrt(N); rt.add_edges(a,b); rt.build(); auto dp=rt.dp(); ll ans=oo; fo(u,N){ fo(i,rt[u].size()){ ll v=rt[u][i].to; ll j=rt[u][i].rev_idx; amin(ans,max( dp[u][i].diameter, dp[v][j].diameter, ceil(dp[u][i].diameter,2)+ceil(dp[v][j].diameter,2)+1 )); } } pp(ans); }