#include<bits/stdc++.h> namespace skyy{ using namespace std; typedef long long ll; template<typename T>mi(T x,T y){ return x<y?x:y; }template<typename T>ma(T x,T y){ return x<y?y:x; }inline void fastiostream(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); }inline void swapll(ll&x,ll&y){ x^=y^=x^=y; }inline ll lg2(ll x){ if(x<=0)return 0; ll l=0,r=62,mi; while(l<r){ mi=(l+r+1)>>1; if(1<<mi>x)r=mi-1; else l=mi; }return l; } template<const ll maxnode>struct unrooted_tree{ private: vector<ll>tree[maxnode+1]; ll dep[maxnode+1]; inline void ddep(ll f,ll x){ for(auto i:tree[x]){ if(i!=f){ dep[i]=dep[x]+1; ddep(x,i); } } } public: inline void addedge(ll x,ll y){ if(x<0||y<0||x>maxnode||y>maxnode)return; tree[x].push_back(y); tree[y].push_back(x); }inline void initdeep(ll root){ if(root<0||root>maxnode)return; dep[root]=0; ddep(-1,root); }inline ll deep(ll x){ if(x<0||x>maxnode)return 0; return dep[x]; } }; template<const ll maxnode,const ll root>struct rooted_tree{ private: vector<ll>tree[maxnode+1]; ll lo=lg2(maxnode+1),dep[maxnode+1],h,dfn[maxnode+1],df,fa[maxnode+1],st[(const ll)lg2(maxnode+1)+1][maxnode+1]; inline void udep(ll f,ll x){ fa[x]=f; for(auto i:tree[x]){ if(i!=f&&!dep[i]){ dep[i]=dep[x]+1; h=ma(h,dep[i]); udep(x,i); } } }inline bool itr(ll x){ if(dep[x]||x==root)return 1; return 0; }inline void idn(ll f,ll x){ dfn[x]=++df; st[0][df]=f; for(auto i:tree[x]){ idn(x,i); } }inline void idfn(){ df=0; idn(-1,root); } public: rooted_tree(){ dep[root]=h=0; fa[root]=-1; }inline void addedge(ll x,ll y){ if(x<0||y<0||x>maxnode||y>maxnode)return; bool inx=itr(x),iny=itr(y); if(inx&&iny)return; tree[x].push_back(y); tree[y].push_back(x); if(iny)swapll(x,y); dep[y]=dep[x]+1; h=ma(h,dep[y]); udep(x,y); }inline ll deep(ll x){ if(x<0||x>maxnode)return 0; return dep[x]; }inline ll height(){ return h+1; }inline ll father(ll x){ if(x<0||x>maxnode)return 0; return fa[x]; }inline void init_LCA(){ idn(-1,root); ll lp=lg2(df); for(ll i=1;i<=lp;i++){ for(ll j=1;j<=df-(1<<i)+1;j++){ if(dfn[st[i-1][j]]<dfn[st[i-1][j+(1<<(i-1))]])st[i][j]=st[i-1][j]; else st[i][j]=st[i-1][j+(1<<(i-1))]; } } } }; } using namespace skyy;