#include #include #define eb emplace_back #define all(a) (a).begin(),(a).end() #define RD(T,...) T __VA_ARGS__;li(__VA_ARGS__) #define LL(...) RD(ll,__VA_ARGS__) #define JO(a,b) a##b #define jo(a,b) JO(a,b) #define usymbol(a) jo(a,__LINE__) #define FE(v,e,...) for(au&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):v) #define FO(n) for(ll usymbol(i)=n;usymbol(i)-->0;) #define FOR(i,...) for(au[i,i##O,i##E]=rng(0,__VA_ARGS__);i=i##O;i-=i##E) #define I template #define J class #define au auto #define rr return using std::cin,std::cout,std::begin,std::end,std::rbegin; using std::swap,std::move,std::abs,std::prev,std::next; using std::tuple,std::minmax,std::get; using vo=void;using bo=bool; vo solve(); Iconstexpr bo same=std::is_same_v; using is=std::istream;using os=std::ostream; using dd=long double;using ll=long long;using ull=unsigned long long; using lll=__int128_t;using ulll=__uint128_t;using str=std::string; os&operator<<(os&o,const ulll&x){rr(x<10?o:o<0?x:-x);} using namespace atcoder; au&operator<<(os&o,const modint&x){rr o<>(is&i,ml&x){ll t;i>>t;x=t;rr i;} constexpr ll oo=3e18; constexpr ll dx[]{-1,0,1,0,-1,1,1,-1},dy[]{0,-1,0,1,-1,-1,1,1}; constexpr char sp=32; constexpr char nl=10; au rng(bo s,ll a,ll b=oo,ll c=1){if(b==oo)b=a,(s?b:a)=0;rr tuple{a-s,b,c};} Ibo in(T l,U m,V r){rr l<=m&&mll len(const T&a){rr a.size();} struct edg{ ll t,w; edg(){} edg(ll t,ll w=1):t(t),w(w){} friend os&operator<<(os&o,const edg&e){rr o<struct cp{ A a={};B b={}; cp(){} cp(A a,B b):a(a),b(b){} cp(std::pairp):a(p.first),b(p.second){} bo operator==(const cp&c)const{rr a==c.a&&b==c.b;} bo operator<(const cp&c)const{rr a!=c.a?a(const cp&c)const{rr a!=c.a?a>c.a:b>c.b;} friend is&operator>>(is&i,cp&c){rr i>>c.a>>c.b;} friend os&operator<<(os&o,const cp&c){rr o<; Istruct tr{ A a={};B b={};C c={}; tr(){} tr(A a,B b,C c):a(a),b(b),c(c){} bo operator==(const tr&t)const{rr a==t.a&&b==t.b&&c==t.c;} au operator<(const tr&t)const{rr a!=t.a?a>(is&i,tr&t){rr i>>t.a>>t.b>>t.c;} friend os&operator<<(os&o,const tr&t){rr o<; Idecltype(au)rb(T&a){rr*rbegin(a);} Iau pot(T&a){au r=a.top();a.pop();rr r;} Iusing ar=std::array; Ios&operator<<(os&o,const ar&a){fo(i,n)o<using map=std::map; Ios&operator<<(os&o,const map&m){fe(m,e)o<using umap=std::unordered_map; Ios&operator<<(os&o,const umap&m){fe(m,e)o<using bset=std::bitset; Ios&operator<<(os&o,const bset&b){fo(i,n)o<os&operator<<(os&o,const tuple&t){apply([&](const au&...a){ll i=sizeof...(a);(((o<0,sp))),...);},t);rr o;} Istruct ve; Iconstexpr bo isv=0; Iconstexpr bo isv> =1; Iconstexpr bo isv> =1; Iau rawv(V){if constexpr(isv)rr rawv(V(1)[0]);else rr V();} Istruct ve:std::vector{ using std::vector::vector; using T=decltype(rawv(V())); Ive(const std::vector&v={}){static_assert(isv ==isv);fe(v,e)this->eb(e);} ve&operator+=(const ve&u){au&v=*this;fo(i,len(v))v[i]+=u[i];rr v;}ve operator+(const ve&u)const{rr ve(*this)+=u;} ve&operator-=(const ve&u){au&v=*this;fo(i,len(v))v[i]-=u[i];rr v;}ve operator-(const ve&u)const{rr ve(*this)-=u;} ve&operator^=(const ve&u){fe(u,e)this->eb(e);rr*this;}ve operator^(const ve&u)const{rr ve(*this)^=u;} ve&operator+=(const T&x){au&v=*this;fe(v,e)e+=x;rr v;}ve operator+(const T&x)const{rr ve(*this)+=x;} ve&operator-=(const T&x){au&v=*this;fe(v,e)e-=x;rr v;}ve operator-(const T&x)const{rr ve(*this)-=x;} ve&operator*=(const T&x){au&v=*this;fe(v,e)e*=x;rr v;}ve operator*(const T&x)const{rr ve(*this)*=x;} ve&operator++(){rr*this+=1;} ve&operator--(){rr*this-=1;} ve operator-()const{rr ve(*this)*=-1;} Iau&operator+=(const bset&a){fo(i,n)(*this)[i]+=a[i];rr*this;} Iau&operator-=(const bset&a){fo(i,n)(*this)[i]-=a[i];rr*this;} au lower_bound(const V&x)const{rr std::lower_bound(all(*this),x);} au upper_bound(const V&x)const{rr std::upper_bound(all(*this),x);} friend is&operator>>(is&i,ve&v){fe(v,e)i>>e;rr i;} friend os&operator<<(os&o,const ve&v){fe(v,e)o<?nl:sp);rr o;} }; Iau vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)rr ve(s[i],x);else{au X=vec(s,x);rr ve(s[i],X);}} Ivo setsize(const ll(&l)[n],A&...a){((a=vec(l,rawv(a))),...);} Iusing vve=ve>;using vl=ve;using vvl=vve; using gl=ve>;using ge=ve>; vo io(){cin.tie(0)->sync_with_stdio(0);cout<vo li(A&...a){(cin>>...>>a);} Iau ygi(G&g,ll m=-1,bo f=1){if(m<0)m=len(g)-1;std::conditional_t,ve,ve>r(m);li(r); fe(r,e)if constexpr(same)g[e.a-=f].eb(e.b-=f);else g[e.a-=f].eb(e.b-=f,e.c);rr r;} Iau mgi(G&g,ll m=-1,bo f=1){if(m<0)m=len(g)-1;au r=ygi(g,m,f); fe(r,e)if constexpr(same)g[e.b].eb(e.a);else g[e.b].eb(e.a,e.c);rr r;} Ivo pp(const A&...a){ll i=sizeof...(a);((cout<0,c)),...);cout<struct rec:F{rec(F&&f):F(std::forward(f)){}Idecltype(au)operator()(A&&...a)const{rr F::operator()(*this,std::forward(a)...);}}; Iau conv(const G&g){ std::conditional_t,ge,gl>r; fe(g,e){ r.eb(); fe(e,v){ if constexpr(same)rb(r).eb(v); else rb(r).eb(v.t); } } rr r; } std::array,3>bfs_order(const vve&tr,ll s){ ll n=len(tr); veord,par(n),dep(n); std::deque>q{{s,-1,0}}; while(len(q)){ auto[u,p,d]=q.front();q.pop_front(); ord.eb(u),par[u]=p,dep[u]=d; fe(tr[u],v)if(v!=p)q.eb(v,u,d+1); } return{ord,par,dep}; } vverooted_tree(const vve&tr,ll s){ ll n=len(tr); vepar=get<1>(bfs_order(tr,s)); vverooted_tr(n); fo(u,n)fe(tr[u],v)if(par[v]==u)rooted_tr[u].eb(v); return rooted_tr; } vverooted_tree(const vve&tr,ll s){ ll n=len(tr); vepar=get<1>(bfs_order(conv(tr),s)); vverooted_tr(n); fo(u,n)fe(tr[u],v,w)if(par[v]==u)rooted_tr[u].eb(v,w); return rooted_tr; } int main(){io();ll T=1;fo(T)solve();} vo solve(){ LL(N); gl g(N);mgi(g); g=rooted_tree(g,0); au[o,p,d]=bfs_order(g,0); vvl dep(N); vl in(N),out(N); ll x=0; ve>st{{~0,-1,0},{0,-1,0}}; while(len(st)){ auto[u,p,d]=st.back();st.pop_back(); if(u>=0){ dep[d].eb(x); in[u]=x++; fe(g[u],v)if(v!=p)st.eb(~v,u,d+1),st.eb(v,u,d+1); }else{ out[~u]=x++; } } fo(i,N){ if(i==0){ if(2