#line 1 "lib/template.hpp" #ifdef TEMPLATE #else #define TEMPLATE # pragma GCC optimize("O3") using namespace std; #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using uint=unsigned; using ll=long long; using ull=unsigned long long; using ld=long double; using pii=pair; using pll=pair; using i128=__int128; templateusing vc=vector; templateusing vvc=vc>; templateusing vvvc=vvc>; templateusing smpq=priority_queue,greater>; templateusing bipq=priority_queue; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++) #define DREP(i,n,m) for(ll i=(n);i>=(m);i--) #define drep(i,n) for(ll i=((n)-1);i>=0;i--) #define rall(x) x.rbegin(),x.rend() #define mp make_pair #define pb push_back #define fi first #define se second #define is insert #define bg begin() #define ed end() #define all(x) x.begin(),x.end() void scan(int&a) { cin >> a; } void scan(ll&a) { cin >> a; } void scan(string&a) { cin >> a; } void scan(char&a) { cin >> a; } void scan(uint&a) { cin >> a; } void scan(ull&a) { cin >> a; } void scan(bool&a) { cin >> a; } void scan(ld&a){ cin>> a;} template void scan(vector&a) { for(auto&x:a) scan(x); } void read() {} template void read(Head&head, Tail&... tail) { scan(head); read(tail...); } #define INT(...) int __VA_ARGS__; read(__VA_ARGS__); #define LL(...) ll __VA_ARGS__; read(__VA_ARGS__); #define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__); #define STR(...) string __VA_ARGS__; read(__VA_ARGS__); #define VC(type, name, ...) vector name(__VA_ARGS__); read(name); #define VVC(type, name, size, ...) vector> name(size, vector(__VA_ARGS__)); read(name); templatevoid print(T a) { cout << a; } template void print(vectora) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout< void PRT(T a) { print(a); cout < void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; } template bool chmin(T &x, F y){ if(x>y){ x=y; return true; } return false; } template bool chmax(T &x, F y){ if(x T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); } template T ceil(T x, T y) { return floor(x + y - 1, y); } template T bmod(T x, T y) { return x - y * floor(x, y); } template pair divmod(T x, T y) { T q = floor(x, y); return {q, x - q * y}; } void YesNo(bool b){ cout<<(b?"Yes":"No")<stovi(const string&s,const string&S){ vcv(s.size()); rep(i,s.size()){ auto t=S.find(s[i]); assert(t!=string::npos); v[i]=t; } return v; } template T isqrt(T x){ T F=sqrtl(x); while((F+1)*(F+1)<=x)F++; while(F*F>x)F--; return F; } template vvctrans(const vvc&a){ assert(a.size()&&a[0].size()); vvcb(a[0].size(),vc(a.size())); rep(i,a.size())rep(j,a[0].size()){ b[j][i]=a[i][j]; } return b; } template vctrans(const vc&a){ assert(a.size()&&a[0].size()); vcb(a[0].size(),string(a.size(),0)); rep(i,a.size())rep(j,a[0].size()){ b[j][i]=a[i][j]; } return b; } template int popcount(T n){ return __builtin_popcountll(n); } template L sum(vc&a){ return accumulate(all(a),L(0)); } template vcsubset(T S){ vcans; for(T x=S;x>0;x=(x-1)&S)ans.pb(x); ans.pb(0); return ans; } template T max(vc&a){ return *max_element(all(a)); } template T min(vc&a){ return *min_element(all(a)); } #ifndef COMPRESSER_STRUCT #define COMPRESSER_STRUCT template struct Compresser{ vcx; Compresser(int n=0){x.reserve(n);} Compresser(const vc&xs){ x=xs; } void push(T p){built=false;x.pb(p);} bool built=false; void build(){ if(!chmax(built,1))return; sort(all(x)); x.erase(unique(all(x)),x.end()); } int find(T v){ build(); auto itr=lower_bound(all(x),v)-x.begin(); if(itr==x.size()||x[itr]!=v)return -1; return itr; } int find_next(T v){ build(); return lower_bound(all(x),v)-x.begin(); } int size(){ build(); return x.size(); } T operator[](int i)const{ assert(0<=i&&i vc presum(vc &a){ vc ret(a.size()+1); rep(i,a.size())ret[i+1]=ret[i]+a[i]; return ret; } template vc &operator+=(vc &a,F b){ for (auto&v:a)v += b; return a; } template vc &operator-=(vc&a,F b){ for (auto&v:a)v-=b; return a; } template vc &operator*=(vc&a,F b){ for (auto&v:a)v*=b; return a; } template constexpr T POW(T a,T b){ T res=1; while(b){ if(b&1)res*=a; a*=a; b/=2; } return res; } constexpr ll ten(ll a){ return POW(10,a); } templateconstexpr T inf=numeric_limits::max()/2-1; template int tbit(T x){ using U=make_unsigned_t; U y=(U)x; return y?(int)bit_width(y)-1:-1; } template int lbit(T x){ using U=make_unsigned_t; U y=(U)x; return y?(int)countr_zero(y):-1; } template int tbit(T x,int p){ using U=make_unsigned_t; constexpr int W=numeric_limits::digits; U y=(U)x; if(p<0)return -1; if(p>=W-1)return tbit(y); return tbit(y&((U(1)<<(p+1))-1)); } template int lbit(T x,int p){ using U=make_unsigned_t; constexpr int W=numeric_limits::digits; U y=(U)x; if(p<0)return lbit(y); if(p>=W)return -1; return lbit(y&(~U(0)<>(istream&is,i128&x){ string s;is>>s; x=0; int i=0,neg=0; if(s[0]=='-')neg=1,i=1; for(;i<(int)s.size();i++)x=x*10+s[i]-'0'; if(neg)x=-x; return is; } ostream& operator<<(ostream&os,i128 x){ if(x==0)return os<<0; if(x<0)os<<"-"; using u128=__uint128_t; u128 y=x<0?-(u128)x:(u128)x; string s; while(y)s.pb('0'+y%10),y/=10; reverse(all(s)); return os< ostream& operator<<(ostream&os,const pair&p){ return os<<"("< ostream& operator<<(ostream&os,const array&a){ os<<"["; rep(i,N){ if(i)os<<", "; os< ostream& operator<<(ostream&os,const vc&a){ os<<"["; rep(i,a.size()){ if(i)os<<", "; os< ostream& operator<<(ostream&os,const deque&a){ os<<"["; rep(i,a.size()){ if(i)os<<", "; os< ostream& operator<<(ostream&os,const set&s){ os<<"{"; bool f=0; for(auto&x:s){ if(f)os<<", "; f=1; os< ostream& operator<<(ostream&os,const multiset&s){ os<<"{"; bool f=0; for(auto&x:s){ if(f)os<<", "; f=1; os< ostream& operator<<(ostream&os,const unordered_set&s){ os<<"{"; bool f=0; for(auto&x:s){ if(f)os<<", "; f=1; os< ostream& operator<<(ostream&os,const map&m){ os<<"{"; bool f=0; for(auto&x:m){ if(f)os<<", "; f=1; os< ostream& operator<<(ostream&os,const unordered_map&m){ os<<"{"; bool f=0; for(auto&x:m){ if(f)os<<", "; f=1; os< ostream& operator<<(ostream&os,queueq){ vca; while(q.size())a.pb(q.front()),q.pop(); return os< ostream& operator<<(ostream&os,stacks){ vca; while(s.size())a.pb(s.top()),s.pop(); return os< ostream& operator<<(ostream&os,priority_queueq){ vca; while(q.size())a.pb(q.top()),q.pop(); return os< void debug_out(const T&x,const Ts&...xs){ cout<sync_with_stdio(0); #ifdef LOCAL cout< struct Unweighted{ Unweighted()=default; Unweighted(int){} operator int()const{return 1;} }; template struct Edge{ int from,to,id; [[no_unique_address]] T cost; #ifdef LOCAL friend ostream&operator<<(ostream&os,const Edge&e){ return os<<"{from:"<struct IsEdge:false_type{}; templatestruct IsEdge().from),decltype(declval().to),decltype(declval().id),decltype(declval().cost)>>:true_type{}; struct EmptyStorage{}; template struct StaticGraph{ constexpr static bool directed(){return is_directed;} using Edge=conditional_t::value,T,::Edge>; using cost_t=decltype(declval().cost); private: int n,m,added=0; mutable bool csr_built=false; [[no_unique_address]] mutable conditional_t inv_built{}; vc_all_edges; mutable vccsr_start; mutable vccsr_edge; [[no_unique_address]] mutable conditional_t,EmptyStorage>inv_start; [[no_unique_address]] mutable conditional_t,EmptyStorage>inv_edge; public: StaticGraph(int n):n(n),m(-1){assert(n>=0);csr_start.resize(n+1);} StaticGraph(int n,int m):n(n),m(m){assert(n>=0&&m>=0);csr_start.resize(n+1);_all_edges.reserve(m);} void resize(int size){ assert(n<=size); assert(!csr_built); n=size; csr_start.resize(n+1); } void add_edge(const Edge&e){ assert(0<=e.from&&e.from void input(int edge_count){ assert(edge_count>=0); rep(i,edge_count){ INT(a,b); a-=substract; b-=substract; add_edge(a,b); } } void build()const{ if(csr_built)return; csr_built=true; csr_start.assign(n+1,0); for(auto&e:_all_edges){ csr_start[e.from]++; if constexpr(!is_directed)csr_start[e.to]++; } rep(i,n)csr_start[i+1]+=csr_start[i]; csr_edge.resize(csr_start[n]); for(auto it=_all_edges.rbegin();it!=_all_edges.rend();++it){ auto&e=*it; csr_edge[--csr_start[e.from]]=e; if constexpr(!is_directed)csr_edge[--csr_start[e.to]]={e.to,e.from,e.id,e.cost}; } } void build_inv()const{ if constexpr(!is_directed){ build(); return; }else{ if(inv_built)return; inv_built=true; inv_start.assign(n+1,0); for(auto&e:_all_edges){ inv_start[e.to]++; } rep(i,n)inv_start[i+1]+=inv_start[i]; inv_edge.resize(inv_start[n]); for(auto it=_all_edges.rbegin();it!=_all_edges.rend();++it){ auto&e=*it; inv_edge[--inv_start[e.to]]={e.to,e.from,e.id,e.cost}; } } } const vc&all_edges()const{return _all_edges;} int edge_size()const{return (int)_all_edges.size();} Edge get_edge(int id)const{ assert(0<=id&&id struct Span{ E*l; E* r; E*begin()const{return l;} E*end()const{return r;} int size()const{return r-l;} E&back()const{return r[-1];} E&operator[](int i){return l[i];} const E&operator[](int i)const{return l[i];} }; auto operator[](int u){ assert(0<=u&&u{csr_edge.data()+csr_start[u],csr_edge.data()+csr_start[u+1]}; } auto operator[](int u)const{ assert(0<=u&&u{csr_edge.data()+csr_start[u],csr_edge.data()+csr_start[u+1]}; } auto inv(int u){ assert(0<=u&&u{inv_edge.data()+inv_start[u],inv_edge.data()+inv_start[u+1]}; } } auto inv(int u)const{ assert(0<=u&&u{inv_edge.data()+inv_start[u],inv_edge.data()+inv_start[u+1]}; } } int size()const{return n;} templatevvcadj()const{ vvcres(n,vc(n)); for(auto&e:all_edges()){ res[e.from][e.to]=e.cost; if(directed()==false)res[e.to][e.from]=e.cost; } return res; } void clear(){ added=0; csr_built=false; if constexpr(is_directed)inv_built=false; _all_edges.clear();_all_edges.shrink_to_fit(); csr_start.assign(n+1,0);csr_start.shrink_to_fit(); csr_edge.clear();csr_edge.shrink_to_fit(); if constexpr(is_directed){ inv_start.clear();inv_start.shrink_to_fit(); inv_edge.clear();inv_edge.shrink_to_fit(); } } template void sort(int i,F f){ assert(0<=i&&i void sort_inv(int i,F f){ assert(0<=i&&i StaticGraphextract(F f)const{ StaticGraphres(n); for(auto&e:_all_edges)if(f(e))res.add_edge(e); return res; } template StaticGraph<1,T>reorder(F f)const{ StaticGraph<1,T>res(n); for(auto&e:_all_edges){ if(f(e))res.add_edge(e); else res.add_edge({e.to,e.from,e.id,e.cost}); } return res; } }; #line 3 "lib/tree/base.hpp" template struct Tree{ using Graph=StaticGraph<0,T>; using Edge=typename Graph::Edge; using cost_t=typename Graph::cost_t; mutable Graph g; mutable int built_hld=-1; int n; mutable vcin,out,head,size_,par,depth,ord; Tree(int n):n(n),g(n,n-1){} void add_edge(const Edge&e){ g.add_edge(e); } void add_edge(int a,int b,cost_t cost=1,int id=-1){ g.add_edge(a,b,cost,id); } bool is_to_par(auto&e)const{ return par[e.from]==e.to; } int size()const{ return n; } auto operator[](int u)const{ assert(0<=u&&u void input(){ rep(i,n-1){ INT(a,b); a-=substract; b-=substract; g.add_edge(a,b); } } template void build(int root=0)const{ assert(0<=root&&rootvoid{ depth[u]=d++; auto s=g[u]; int heavy=-1,par_id=-1; rep(i,s.size()){ auto&e=s[i]; if(e.to==v){ par_id=i; continue; } par[e.to]=u; dfs(dfs,e.to,u,d); size_[u]+=size_[e.to]; if(heavy==-1||size_[s[heavy].to]void{ in[u]=timer; ord[timer++]=u; auto s=g[u]; bool first=true; rep(i,s.size()){ auto&e=s[i]; if(e.to==par[u])continue; head[e.to]=first?head[u]:e.to; first=false; dfs(dfs,e.to); } if constexpr(disjoint)out[u]=timer++; else out[u]=timer; };dfs(dfs,root);} rep(u,n){ auto s=g[u]; int ce=s.size()-(par[u]!=-1); if(ce>1)sort(s.begin()+1,s.begin()+ce,[&](const Edge&a,const Edge&b){return in[a.to]{s.l,s.l}; return typename Graph::template Span{s.l,s.l+1}; } auto light_edges(int u)const{ assert(0<=u&&u{s.l,s.l}; return typename Graph::template Span{s.l+1,s.l+ce}; } int lca(int a,int b)const{ assert(0<=a&&a0){ int x=I[a]-I[H[a]]; if(x>=k){ return O[I[a]-k]; } k-=x+1; a=P[H[a]]; } return a; } int jump(int s,int t,int k)const{ assert(0<=s&&s=k){ return jumpup(s,k); }else{ return jumpup(t,D-k); } } int to(int x,int y)const{ assert(0<=x&&x>Query(int s,int t,bool edge=false)const{ assert(0<=s&&s>rs,rt; while(h[s]!=h[t]){ if(d[h[s]]>d[h[t]]){ rs.push_back({I[s],I[h[s]]}); s=P[h[s]]; }else{ rt.push_back({I[h[t]],I[t]}); t=P[h[t]]; } } if(s!=t||!edge){ if(d[s]>d[t]){ rs.push_back({I[s],I[t]+edge}); }else{ rt.push_back({I[s]+edge,I[t]}); } } rs.reserve(rs.size()+rt.size()); for(auto it=rt.rbegin();it!=rt.rend();++it)rs.push_back(*it); return rs; } pairget_diameter()const{ assert(n>0); auto find_farthest=[&](int from)->int{ vcd(n),p(n,-2),st(1,from); p[from]=-1; int far=from; while(!st.empty()){ int u=st.back(); st.pop_back(); if(d[far](); vcdp0(n),dp1(n); auto dfs=[&](auto&dfs,int u,int v)->void{ int max_delta=-1e9; ll S=0; ll S3=0; for(auto&e:base[u]){ if(e.to==v)continue; dfs(dfs,e.to,u); chmax(max_delta,dp0[e.to]-dp1[e.to]); S+=dp1[e.to]; S3+=max(dp0[e.to],dp1[e.to]); } dp0[u]=S3; dp1[u]=max(0ll,max_delta+S+1); }; dfs(dfs,0,-1); dbg(dp0); dbg(dp1); PRT(max(dp0[0],dp1[0])); } signed main(){ int t=1; // cin >> t; while(t--)solve(); }