結果
問題 | No.2427 Tree Distance Two |
ユーザー | eQe |
提出日時 | 2023-08-18 22:15:11 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 521 ms / 2,000 ms |
コード長 | 7,675 bytes |
コンパイル時間 | 6,672 ms |
コンパイル使用メモリ | 337,044 KB |
実行使用メモリ | 80,548 KB |
最終ジャッジ日時 | 2024-11-28 07:46:04 |
合計ジャッジ時間 | 15,096 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 521 ms
46,388 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 501 ms
46,276 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 293 ms
74,544 KB |
testcase_08 | AC | 271 ms
47,120 KB |
testcase_09 | AC | 276 ms
47,448 KB |
testcase_10 | AC | 261 ms
43,892 KB |
testcase_11 | AC | 363 ms
80,548 KB |
testcase_12 | AC | 401 ms
61,500 KB |
testcase_13 | AC | 436 ms
61,820 KB |
testcase_14 | AC | 183 ms
61,248 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 2 ms
5,248 KB |
testcase_17 | AC | 2 ms
5,248 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 2 ms
5,248 KB |
testcase_20 | AC | 8 ms
5,248 KB |
testcase_21 | AC | 10 ms
5,248 KB |
testcase_22 | AC | 4 ms
5,248 KB |
testcase_23 | AC | 3 ms
5,248 KB |
testcase_24 | AC | 9 ms
5,248 KB |
testcase_25 | AC | 88 ms
14,220 KB |
testcase_26 | AC | 274 ms
29,608 KB |
testcase_27 | AC | 172 ms
22,844 KB |
testcase_28 | AC | 454 ms
43,912 KB |
testcase_29 | AC | 390 ms
38,072 KB |
testcase_30 | AC | 280 ms
28,896 KB |
testcase_31 | AC | 151 ms
19,944 KB |
testcase_32 | AC | 267 ms
27,344 KB |
testcase_33 | AC | 222 ms
24,656 KB |
testcase_34 | AC | 54 ms
10,612 KB |
ソースコード
#include<bits/stdc++.h> #include<atcoder/all> #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 fe(v,...) FE(v,__VA_ARGS__) #define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__)) #define of(i,...) for(au[i,i##O,i##E]=rng(1,__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(); I<J A,J B>constexpr bo same=std::is_same_v<A,B>; 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<<x/10)<<ll(x%10);} os&operator<<(os&o,const lll&x){rr o<<str(x<0,'-')<<ulll(x>0?x:-x);} using namespace atcoder; au&operator<<(os&o,const modint&x){rr o<<x.val();} au&operator<<(os&o,const modint998244353&x){rr o<<x.val();} au&operator<<(os&o,const modint1000000007&x){rr o<<x.val();} using ml=modint; au&operator>>(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};} I<J T,J U,J V>bo in(T l,U m,V r){rr l<=m&&m<r;} I<J T>ll 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<<e.t<<sp<<e.w;} }; I<J A,J B=A>struct cp{ A a={};B b={}; cp(){} cp(A a,B b):a(a),b(b){} cp(std::pair<A,B>p):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<c.a:b<c.b;} bo operator>(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<<c.a<<sp<<c.b;} };using cl=cp<ll>; I<J A,J B=A,J C=A>struct 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<t.a:b!=t.b?b<t.b:c<t.c;} friend is&operator>>(is&i,tr&t){rr i>>t.a>>t.b>>t.c;} friend os&operator<<(os&o,const tr&t){rr o<<t.a<<sp<<t.b<<sp<<t.c;} };using tl=tr<ll>; I<J T>decltype(au)rb(T&a){rr*rbegin(a);} I<J T>au pot(T&a){au r=a.top();a.pop();rr r;} I<J T,size_t n>using ar=std::array<T,n>; I<J T,size_t n>os&operator<<(os&o,const ar<T,n>&a){fo(i,n)o<<a[i]<<str(i!=n-1,sp);rr o;} I<J T,J U=T>using map=std::map<T,U>; I<J T,J U>os&operator<<(os&o,const map<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;rr o;} I<J T,J U=T>using umap=std::unordered_map<T,U>; I<J T,J U>os&operator<<(os&o,const umap<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;rr o;} I<size_t n>using bset=std::bitset<n>; I<size_t n>os&operator<<(os&o,const bset<n>&b){fo(i,n)o<<b[i];rr o;} I<J...A>os&operator<<(os&o,const tuple<A...>&t){apply([&](const au&...a){ll i=sizeof...(a);(((o<<a<<str(--i>0,sp))),...);},t);rr o;} I<J V>struct ve; I<J V>constexpr bo isv=0; I<J V>constexpr bo isv<ve<V>> =1; I<J V>constexpr bo isv<std::vector<V>> =1; I<J V>au rawv(V){if constexpr(isv<V>)rr rawv(V(1)[0]);else rr V();} I<J V>struct ve:std::vector<V>{ using std::vector<V>::vector; using T=decltype(rawv(V())); I<J U>ve(const std::vector<U>&v={}){static_assert(isv<V> ==isv<U>);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;} I<size_t n>au&operator+=(const bset<n>&a){fo(i,n)(*this)[i]+=a[i];rr*this;} I<size_t n>au&operator-=(const bset<n>&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<<e<<str(&e!=&v.back(),isv<V>?nl:sp);rr o;} }; I<J T=ll,size_t n,size_t i=0>au vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)rr ve<T>(s[i],x);else{au X=vec<T,n,i+1>(s,x);rr ve<decltype(X)>(s[i],X);}} I<ll n,J...A>vo setsize(const ll(&l)[n],A&...a){((a=vec(l,rawv(a))),...);} I<J T>using vve=ve<ve<T>>;using vl=ve<ll>;using vvl=vve<ll>; using gl=ve<ve<ll>>;using ge=ve<ve<edg>>; vo io(){cin.tie(0)->sync_with_stdio(0);cout<<std::fixed<<std::setprecision(15);std::cerr<<nl;} I<J...A>vo li(A&...a){(cin>>...>>a);} I<J G>au ygi(G&g,ll m=-1,bo f=1){if(m<0)m=len(g)-1;std::conditional_t<same<G,gl>,ve<cl>,ve<tl>>r(m);li(r); fe(r,e)if constexpr(same<G,gl>)g[e.a-=f].eb(e.b-=f);else g[e.a-=f].eb(e.b-=f,e.c);rr r;} I<J G>au 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,gl>)g[e.b].eb(e.a);else g[e.b].eb(e.a,e.c);rr r;} I<char c=sp,J...A>vo pp(const A&...a){ll i=sizeof...(a);((cout<<a<<str(--i>0,c)),...);cout<<nl;} I<J F>struct rec:F{rec(F&&f):F(std::forward<F>(f)){}I<J...A>decltype(au)operator()(A&&...a)const{rr F::operator()(*this,std::forward<A>(a)...);}}; I<J G>au conv(const G&g){ std::conditional_t<same<G,gl>,ge,gl>r; fe(g,e){ r.eb(); fe(e,v){ if constexpr(same<G,gl>)rb(r).eb(v); else rb(r).eb(v.t); } } rr r; } std::array<ve<ll>,3>bfs_order(const vve<ll>&tr,ll s){ ll n=len(tr); ve<ll>ord,par(n),dep(n); std::deque<tuple<ll,ll,ll>>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}; } vve<ll>rooted_tree(const vve<ll>&tr,ll s){ ll n=len(tr); ve<ll>par=get<1>(bfs_order(tr,s)); vve<ll>rooted_tr(n); fo(u,n)fe(tr[u],v)if(par[v]==u)rooted_tr[u].eb(v); return rooted_tr; } vve<edg>rooted_tree(const vve<edg>&tr,ll s){ ll n=len(tr); ve<ll>par=get<1>(bfs_order(conv(tr),s)); vve<edg>rooted_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<tuple<ll,ll,ll>>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<N)pp(len(dep[2])); else pp(0); }else{ ll t=len(g[p[i]])-1; if(p[i]!=0)t++; if(d[i]+2<N){ t+=dep[d[i]+2].lower_bound(out[i])-dep[d[i]+2].lower_bound(in[i]); } pp(t); } } }