#include #include #include #include #include #include #include #include using namespace std; #define int long long std::pair,int> _max(std::pair,int> a,std::pair,int> b){ return a.first > b.first ? a : b; } std::pair,int> _e(){ return std::make_pair(make_pair(-1,-1),-1); } class ContinuousPriorityQueue{ private: std::vector>> _vec; atcoder::segtree,int>,_max,_e> _seg; // val,index public: ContinuousPriorityQueue(int maxsize=200000){ _seg=atcoder::segtree,int>,_max,_e>(maxsize); _vec.reserve(maxsize); } pair top(int l,int r){ assert(0<=l && l<_vec.size() && 0<=r && r<=_vec.size()); return _seg.prod(l,r).first; } pair pop(int l,int r){ assert(0<=l && l<_vec.size() && 0,int> max_p=_seg.prod(l,r); if(max_p==_e()){ return max_p.first; } _vec[max_p.second].pop(); if(_vec[max_p.second].empty()){ _seg.set(max_p.second,_e()); }else{ _seg.set(max_p.second,std::make_pair(_vec[max_p.second].top(),max_p.second)); } return max_p.first; } void add(int index,pair val){ assert(0<=index && index<_vec.size()); _vec[index].push(val); _seg.set(index,std::make_pair(_vec[index].top(),index)); return; } void add_back(pair val){ assert(!_vec.empty()); _vec.back().push(val); _seg.set(_vec.size()-1,std::make_pair(_vec.back().top(),_vec.size()-1)); return; } void push_back(){ _vec.push_back(std::priority_queue>()); return; } int size(){ return _vec.size(); } bool empty(int index){ return _vec[index].empty(); } bool range_empty(int l,int r){ return _seg.prod(l,r)==_e(); } void _print(){ for(int i=0;i<_vec.size();i++){ priority_queue> pq=_vec[i]; while(!pq.empty()){ cerr<<"("<> G; ContinuousPriorityQueue CPQ; vector depth; vector parent; vector> children; vector searched; unordered_set deled; void dfs(int point,int dep){ depth[point]=dep; for(int to:G[point]){ if(depth[to]==-1){ dfs(to,dep+1); children[point].push_back(to); }else{ parent[point]=to; } } return; } vector table; vector> new_children; vector new_parent; const int num=100000; int dfs2(int point){ vector> pq; int pre=CPQ.size(); for(int to:children[point]){ int tmp=dfs2(to); pq.push_back(make_pair(tmp,to)); } int res=num; //cerr<<"point:"< i:pq){ CPQ.add_back(i); } if(!CPQ.range_empty(pre,CPQ.size())){ //cerr<<"pre:"< t1=CPQ.pop(pre,CPQ.size()); res+=t1.first; //assert(0<=t1.second && t1.second t2=CPQ.pop(pre,CPQ.size()); res+=t2.first; //assert(0<=t2.second && t2.second CPQ[pre,size) is empty } //cerr<<"let serach"<0){ res--; res+=num; }else{ res-=num; res++; } //deled.insert(point); } } //cerr<<"res:"<>N; G.resize(N); depth.resize(N,-1); parent.resize(N,-1); children.resize(N); searched.resize(N,false); table.resize(N,-1); new_parent.resize(N); new_children.resize(N); for(int i=0;i>A>>B; G[A-1].push_back(B-1); G[B-1].push_back(A-1); } dfs(0,0); int ans=dfs2(0); // cerr<<"dfs2 end"<=1 && dfs3(0)){ // //cerr<<"ans++"<