結果
問題 | No.2427 Tree Distance Two |
ユーザー | 👑 p-adic |
提出日時 | 2023-08-18 21:42:35 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 413 ms / 2,000 ms |
コード長 | 26,978 bytes |
コンパイル時間 | 3,528 ms |
コンパイル使用メモリ | 229,824 KB |
実行使用メモリ | 49,280 KB |
最終ジャッジ日時 | 2024-11-28 06:17:12 |
合計ジャッジ時間 | 11,616 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 10 ms
13,184 KB |
testcase_01 | AC | 10 ms
13,056 KB |
testcase_02 | AC | 10 ms
13,056 KB |
testcase_03 | AC | 413 ms
44,672 KB |
testcase_04 | AC | 10 ms
13,056 KB |
testcase_05 | AC | 388 ms
44,800 KB |
testcase_06 | AC | 10 ms
12,928 KB |
testcase_07 | AC | 243 ms
43,136 KB |
testcase_08 | AC | 304 ms
42,368 KB |
testcase_09 | AC | 296 ms
41,728 KB |
testcase_10 | AC | 303 ms
41,728 KB |
testcase_11 | AC | 278 ms
47,104 KB |
testcase_12 | AC | 322 ms
45,184 KB |
testcase_13 | AC | 370 ms
43,752 KB |
testcase_14 | AC | 178 ms
49,280 KB |
testcase_15 | AC | 11 ms
13,056 KB |
testcase_16 | AC | 10 ms
13,184 KB |
testcase_17 | AC | 11 ms
13,056 KB |
testcase_18 | AC | 11 ms
13,056 KB |
testcase_19 | AC | 10 ms
13,184 KB |
testcase_20 | AC | 15 ms
13,824 KB |
testcase_21 | AC | 16 ms
14,000 KB |
testcase_22 | AC | 12 ms
13,440 KB |
testcase_23 | AC | 11 ms
13,184 KB |
testcase_24 | AC | 16 ms
13,824 KB |
testcase_25 | AC | 82 ms
21,248 KB |
testcase_26 | AC | 247 ms
32,640 KB |
testcase_27 | AC | 158 ms
27,008 KB |
testcase_28 | AC | 407 ms
42,368 KB |
testcase_29 | AC | 314 ms
38,784 KB |
testcase_30 | AC | 217 ms
32,000 KB |
testcase_31 | AC | 124 ms
25,216 KB |
testcase_32 | AC | 213 ms
31,872 KB |
testcase_33 | AC | 188 ms
29,696 KB |
testcase_34 | AC | 51 ms
18,176 KB |
ソースコード
#ifdef DEBUG #define _GLIBCXX_DEBUG #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ); signal( SIGABRT , &AlertAbort ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE ) #define CERR( MESSAGE ) cerr << MESSAGE << endl; #define COUT( ANSWER ) cout << ANSWER << endl #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " << ( MIN ) << ( ( MIN ) <= A ? "<=" : ">" ) << A << ( A <= ( MAX ) ? "<=" : ">" ) << ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define LIBRARY_SEARCH bool searched_library = false; LibrarySearch( searched_library ); if( searched_library ){ QUIT; }; #define START_WATCH( PROCESS_NAME ) StartWatch( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) StopWatch( HOW_MANY_TIMES ) #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define CERR( MESSAGE ) #define COUT( ANSWER ) cout << ANSWER << "\n" #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define LIBRARY_SEARCH #define START_WATCH( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) #endif // #define RANDOM_TEST #include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define TYPE_OF( VAR ) decay_t<decltype( VAR )> #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN( LL , A ) LL A; cin >> A #define CIN_ASSERT( A , MIN , MAX ) TYPE_OF( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define GETLINE( A ) string A; getline( cin , A ) #define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) #define FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR >= FINAL ; VAR -- ) #define AUTO_ITR( ARRAY ) auto itr_ ## ARRAY = ARRAY .begin() , end_ ## ARRAY = ARRAY .end() #define FOR_ITR( ARRAY ) for( AUTO_ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT_ ## HOW_MANY_TIMES , 0 , HOW_MANY_TIMES ) #define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS ) #define QUIT return 0 #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void StartWatch( const string& process_name = "nothing" ); void StopWatch( const int& how_many_times = 1 ); #endif #if defined( DEBUG ) && defined( RANDOM_TEST ) inline CEXPR( int , bound_random_test_num , 1000 ); #define START_MAIN FOR( random_test_num , 0 , bound_random_test_num ){ CERR( "(" << random_test_num << ")" ); ll GetRand( const ll& Rand_min , const ll& Rand_max ); #define SET_ASSERT( A , MIN , MAX ) CERR( #A << " = " << ( A = GetRand( MIN , MAX ) ) ) #define RETURN( ANSWER ) if( ( ANSWER ) == guchoku ){ CERR( ( ANSWER ) << " == " << guchoku ); continue; } else { CERR( ( ANSWER ) << " != " << guchoku ); QUIT; } #define FINISH_MAIN CERR( "" ); } #else #define START_MAIN #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define RETURN( ANSWER ) COUT( ( ANSWER ) ); QUIT #define FINISH_MAIN #endif template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : -a; } template <typename T> inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : p - 1 - ( ( - ( a + 1 ) ) % p ); } #define POWER( ANSWER , ARGUMENT , EXPONENT ) \ static_assert( ! is_same<TYPE_OF( ARGUMENT ),int>::value && ! is_same<TYPE_OF( ARGUMENT ),uint>::value ); \ TYPE_OF( ARGUMENT ) ANSWER{ 1 }; \ { \ TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define POWER_MOD( ANSWER , ARGUMENT , EXPONENT , MODULO ) \ ll ANSWER{ 1 }; \ { \ ll ARGUMENT_FOR_SQUARE_FOR_POWER = ( ( MODULO ) + ( ( ARGUMENT ) % ( MODULO ) ) ) % ( MODULO ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER = ( ANSWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % ( MODULO ); \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT_FOR_SQUARE_FOR_POWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % ( MODULO ); \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define FACTORIAL_MOD( ANSWER , ANSWER_INV , INVERSE , MAX_INDEX , CONSTEXPR_LENGTH , MODULO ) \ static ll ANSWER[CONSTEXPR_LENGTH]; \ static ll ANSWER_INV[CONSTEXPR_LENGTH]; \ static ll INVERSE[CONSTEXPR_LENGTH]; \ { \ ll VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ ANSWER[0] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL; \ FOREQ( i , 1 , MAX_INDEX ){ \ ANSWER[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= i ) %= ( MODULO ); \ } \ ANSWER_INV[0] = ANSWER_INV[1] = INVERSE[1] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ FOREQ( i , 2 , MAX_INDEX ){ \ ANSWER_INV[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= INVERSE[i] = ( MODULO ) - ( ( ( ( MODULO ) / i ) * INVERSE[ ( MODULO ) % i ] ) % ( MODULO ) ) ) %= ( MODULO ); \ } \ } \ // 二分探索テンプレート // EXPRESSIONがANSWERの広義単調関数の時、EXPRESSION >= TARGETの整数解を格納。 #define BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , DESIRED_INEQUALITY , TARGET , INEQUALITY_FOR_CHECK , UPDATE_U , UPDATE_L , UPDATE_ANSWER ) \ static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \ ll ANSWER = MINIMUM; \ if( MINIMUM <= MAXIMUM ){ \ ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM; \ ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM; \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \ while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ CERR( "二分探索中: " << VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH INEQUALITY_FOR_CHECK 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_U = UPDATE_U; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_L = UPDATE_L; \ } \ ANSWER = UPDATE_ANSWER; \ } \ CERR( "二分探索終了: " << VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << ( EXPRESSION > TARGET ? ">" : EXPRESSION < TARGET ? "<" : "=" ) << TARGET ); \ CERR( ( EXPRESSION DESIRED_INEQUALITY TARGET ? "二分探索成功" : "二分探索失敗" ) ); \ assert( EXPRESSION DESIRED_INEQUALITY TARGET ); \ } else { \ CERR( "二分探索失敗: " << MINIMUM << ">" << MAXIMUM ); \ assert( MINIMUM <= MAXIMUM ); \ } \ // 単調増加の時にEXPRESSION >= TARGETの最小解を格納。 #define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , >= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調増加の時にEXPRESSION <= TARGETの最大解を格納。 #define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , > , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION >= TARGETの最大解を格納。 #define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , < , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION <= TARGETの最小解を格納。 #define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , <= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // t以下の値が存在すればその最大値のiterator、存在しなければend()を返す。 template <typename T> inline typename set<T>::iterator MaximumLeq( set<T>& S , const T& t ) { const auto end = S.end(); if( S.empty() ){ return end; } auto itr = S.upper_bound( t ); return itr == end ? S.find( *( S.rbegin() ) ) : itr == S.begin() ? end : --itr; } // t未満の値が存在すればその最大値のiterator、存在しなければend()を返す。 template <typename T> inline typename set<T>::iterator MaximumLt( set<T>& S , const T& t ) { const auto end = S.end(); if( S.empty() ){ return end; } auto itr = S.lower_bound( t ); return itr == end ? S.find( *( S.rbegin() ) ) : itr == S.begin() ? end : --itr; } // t以上の値が存在すればその最小値のiterator、存在しなければend()を返す。 template <typename T> inline typename set<T>::iterator MinimumGeq( set<T>& S , const T& t ) { return S.lower_bound( t ); } // tより大きい値が存在すればその最小値のiterator、存在しなければend()を返す。 template <typename T> inline typename set<T>::iterator MinimumGt( set<T>& S , const T& t ) { return S.upper_bound( t ); } // 圧縮用 #define TE template #define TY typename #define US using #define ST static #define IN inline #define CL class #define PU public #define OP operator #define CE constexpr #define CO const #define NE noexcept #define RE return #define WH while #define VO void #define VE vector #define LI list #define BE begin #define EN end #define SZ size #define MO move #define TH this #define CRI CO int& #define CRUI CO uint& #define CRL CO ll& #define DC_OF_FIRST_SEARCH(BREADTH)TE <int V_max> CL BREADTH ## FirstSearch_Body{PU:int m_V;int m_init;LI<int> m_next;bool m_found[V_max];int m_prev[V_max];IN BREADTH ## FirstSearch_Body(CRI V);IN BREADTH ## FirstSearch_Body(CRI V,CRI init);IN VO Reset(CRI init);IN VO Shift(CRI init);IN CRI SZ()CO;IN CRI init()CO;IN bool& found(CRI i);IN CRI prev(CRI i)CO;int Next();virtual LI<int> e(CRI t)= 0;};TE <int V_max,LI<int> E(CRI)> CL BREADTH ## FirstSearch:PU BREADTH ## FirstSearch_Body<V_max>{PU:TE<TY... Args> IN BREADTH ## FirstSearch(CO Args&... args);IN LI<int> e(CRI t);};TE <int V_max,LI<int> E(CRI)> VO BREADTH ## FirstConnectedComponent(CRI V,int(&vertex)[V_max],int& count); #define DF_OF_FIRST_SEARCH(BREADTH,PUSH)TE <int V_max> IN BREADTH ## FirstSearch_Body<V_max>::BREADTH ## FirstSearch_Body(CRI V):m_V(V),m_init(),m_next(),m_found(),m_prev(){assert(m_V <= V_max);for(int i = 0;i < m_V;i++){m_prev[i] = -1;}}TE <int V_max> IN BREADTH ## FirstSearch_Body<V_max>::BREADTH ## FirstSearch_Body(CRI V,CRI init):BREADTH ## FirstSearch_Body(V){m_init = init;m_next.push_back(m_init);m_found[m_init] = true;}TE <int V_max,LI<int> E(CRI)> TE <TY... Args> IN BREADTH ## FirstSearch<V_max,E>::BREADTH ## FirstSearch(CO Args&... args):BREADTH ## FirstSearch_Body<V_max>(args...){}TE <int V_max> IN VO BREADTH ## FirstSearch_Body<V_max>::Reset(CRI init){m_init = init;assert(m_init < m_V);m_next.clear();m_next.push_back(m_init);for(int i = 0;i < m_V;i++){m_found[i] = i == m_init;m_prev[i] = -1;}}TE <int V_max> IN VO BREADTH ## FirstSearch_Body<V_max>::Shift(CRI init){m_init = init;assert(m_init < m_V);m_next.clear();if(! m_found[m_init]){m_next.push_back(m_init);m_found[m_init] = true;}}TE <int V_max> IN CRI BREADTH ## FirstSearch_Body<V_max>::SZ()CO{RE m_V;}TE <int V_max> IN CRI BREADTH ## FirstSearch_Body<V_max>::init()CO{RE m_init;}TE <int V_max> IN bool& BREADTH ## FirstSearch_Body<V_max>::found(CRI i){assert(i < m_V);RE m_found[i];}TE <int V_max> IN CRI BREADTH ## FirstSearch_Body<V_max>::prev(CRI i)CO{assert(i < m_V);RE m_prev[i];}TE <int V_max> int BREADTH ## FirstSearch_Body<V_max>::Next(){if(m_next.empty()){RE -1;}CO int i_curr = m_next.front();m_next.pop_front();LI<int> edge = e(i_curr);WH(! edge.empty()){CRI i = edge.front();bool& found_i = found(i);if(! found_i){m_next.PUSH(i);m_prev[i] = i_curr;found_i = true;}edge.pop_front();}RE i_curr;}TE <int V_max,LI<int> E(CRI)> IN LI <int> BREADTH ## FirstSearch<V_max,E>::e(CRI t){RE E(t);}TE <int V_max,LI<int> E(CRI)> VO BREADTH ## FirstConnectedComponentSearch(CRI V,int(&vertex)[V_max],int& count){BREADTH ## FirstSearch<V_max,E> bfs{V};count = 0;for(int i = 0;i < V;i++){vertex[i] = -1;}for(int i = 0;i < V;i++){if(vertex[i] == -1){bfs.Shift(i);int j = bfs.Next();WH(j != -1?vertex[j] == 0:false){vertex[j] = count;j = bfs.Next();}count++;}}RE;} DC_OF_FIRST_SEARCH(Depth);DF_OF_FIRST_SEARCH(Depth,push_front); TE <int V_max,LI<int> E(CRI),int digit = 0>CL DepthFirstSearchOnTree:PU DepthFirstSearch<V_max,E>{PU:int m_reversed[V_max];VE<VE<int> > m_children;VE<int> m_children_num;bool m_set_children;VE<int> m_depth;bool m_set_depth;VE<int> m_height;bool m_set_height;VE<int> m_weight;bool m_set_weight;VE<int> m_doubling[digit];bool m_set_doubling;IN DepthFirstSearchOnTree(CRI V,CRI root);IN VO Reset(CRI init)= delete;IN VO Shift(CRI init)= delete;IN CRI Root()CO;IN CRI Parent(CRI i)CO;IN CO VE<int>& Children(CRI i);IN CRI Depth(CRI i)CO;IN CRI Height(CRI i);IN CRI Weight(CRI i);IN CRI NodeNumber(CRI i,CO bool& reversed = false)CO;IN CRI ChildrenNumber(CRI i);int Ancestor(int i,int n);int LCA(int i,int j);int LCA(int i,int j,int& i_prev,int& j_prev);TE <TY T,T m_T(const T&,const T&)>T RootingDP(const T(&a)[V_max]);TE <TY T,T m_T(CO T&,CO T&),CO T& e_T(),T f(CO T&,CRI)> VO RerootingDP(T(&d)[V_max]);VO SetChildren();VO SetDepth();VO SetHeight();VO SetWeight();VO SetDoubling();}; TE <int V_max,LI<int> E(CRI),int digit> IN DepthFirstSearchOnTree<V_max,E,digit>::DepthFirstSearchOnTree(CRI V,CRI root):DepthFirstSearch<V_max,E>(V,root),m_reversed(),m_children(),m_set_children(),m_depth(),m_set_depth(),m_height(),m_set_height(),m_weight(),m_set_weight(),m_doubling(),m_set_doubling(){int n = DepthFirstSearch<V_max,E>::SZ();WH(--n >= 0){m_reversed[n] = DepthFirstSearch<V_max,E>::Next();}}TE <int V_max,LI<int> E(CRI),int digit> IN CRI DepthFirstSearchOnTree<V_max,E,digit>::Root()CO{RE DepthFirstSearch<V_max,E>::init();}TE <int V_max,LI<int> E(CRI),int digit> IN CRI DepthFirstSearchOnTree<V_max,E,digit>::Parent(CRI i)CO{RE DepthFirstSearch<V_max,E>::prev(i);}TE <int V_max,LI<int> E(CRI),int digit> IN CO VE<int>& DepthFirstSearchOnTree<V_max,E,digit>::Children(CRI i){if(! m_set_children){SetChildren();}RE m_children[i];}TE <int V_max,LI<int> E(CRI),int digit> IN CRI DepthFirstSearchOnTree<V_max,E,digit>::Depth(CRI i)CO{if(! m_set_depth){SetDepth();}RE m_depth[i];}TE <int V_max,LI<int> E(CRI),int digit> IN CRI DepthFirstSearchOnTree<V_max,E,digit>::Height(CRI i){if(! m_set_height){SetHeight();}RE m_height[i];}TE <int V_max,LI<int> E(CRI),int digit> IN CRI DepthFirstSearchOnTree<V_max,E,digit>::Weight(CRI i){if(! m_set_weight){SetWeight();}RE m_weight[i];}TE <int V_max,LI<int> E(CRI),int digit> IN CRI DepthFirstSearchOnTree<V_max,E,digit>::NodeNumber(CRI i,CO bool& reversed)CO{RE m_reversed[reversed?i:DepthFirstSearch<V_max,E>::SZ()- 1 - i];}TE <int V_max,LI<int> E(CRI),int digit> IN CRI DepthFirstSearchOnTree<V_max,E,digit>::ChildrenNumber(CRI i){if(! m_set_children){SetChildren();}RE m_children_num[i];}TE <int V_max,LI<int> E(CRI),int digit>int DepthFirstSearchOnTree<V_max,E,digit>::Ancestor(int i,int n){if(! m_set_doubling){SetDoubling();}assert((n >> digit)== 0);int d = 0;WH(n != 0){if((n & 1)== 1){assert((i = m_doubling[d][i])!= -1);}d++;n >>= 1;}RE i;}TE <int V_max,LI<int> E(CRI),int digit>int DepthFirstSearchOnTree<V_max,E,digit>::LCA(int i,int j){int diff = Depth(i)- Depth(j);if(diff < 0){swap(i,j);diff *= -1;}i = Ancestor(i,diff);if(i == j){RE i;}int d = digit;WH(--d >= 0){CO int(&doubling_d)[V_max] = m_doubling[d];CRI doubling_d_i = doubling_d[i];CRI doubling_d_j = doubling_d[j];if(doubling_d_i != doubling_d_j){i = doubling_d_i;j = doubling_d_j;assert(i != -1);assert(j != -1);}}RE Parent(i);}TE <int V_max,LI<int> E(CRI),int digit>int DepthFirstSearchOnTree<V_max,E,digit>::LCA(int i,int j,int& i_prev,int& j_prev){if(i == j){i_prev = j_prev = -1;RE i;}int diff = Depth(i)- Depth(j);if(diff < 0){RE LCA(j,i,j_prev,i_prev);}if(diff > 0){i_prev = Ancestor(i,diff - 1);i = Parent(i_prev);assert(i != -1);if(i == j){j_prev = -1;RE i;}}else if(! m_set_doubling){SetDoubling();}int d = digit;WH(--d >= 0){CO int(&doubling_d)[V_max] = m_doubling[d];CRI doubling_d_i = doubling_d[i];CRI doubling_d_j = doubling_d[j];if(doubling_d_i != doubling_d_j){i = doubling_d_i;j = doubling_d_j;assert(i != -1);assert(j != -1);}}i_prev = i;j_prev = j;RE Parent(i_prev);}TE <int V_max,LI<int> E(CRI),int digit>VO DepthFirstSearchOnTree<V_max,E,digit>::SetChildren(){assert(!m_set_children);m_set_children = true;CRI V = DepthFirstSearch<V_max,E>::SZ();m_children.resize(V);m_children_num.resize(V);for(int i = 0;i < V;i++){CRI j = Parent(i);if(j == -1){m_children_num[i] = -1;}else{VE<int>& m_children_j = m_children[j];m_children_num[i] = m_children_j.SZ();m_children_j.push_back(i);}}RE;}TE <int V_max,LI<int> E(CRI),int digit>VO DepthFirstSearchOnTree<V_max,E,digit>::SetDepth(){assert(!m_set_depth);m_set_depth = true;CRI V = DepthFirstSearch<V_max,E>::SZ();m_depth.resize(V);for(int i = 0;i < V;i++){CRI parent_i = Parent(i);if(parent_i != -1){m_depth[i] = m_depth[parent_i] + 1;}}RE;}TE <int V_max,LI<int> E(CRI),int digit>VO DepthFirstSearchOnTree<V_max,E,digit>::SetHeight(){assert(!m_set_height);m_set_height = true;CRI V = DepthFirstSearch<V_max,E>::SZ();m_height.resize(V);for(int i = 0;i < V;i++){CRI reversed_i = m_reversed[i];CRI parent_i = Parent(reversed_i);if(parent_i != -1){int& height_parent_i = m_height[parent_i];CRI height_i = m_height[reversed_i];height_parent_i > height_i?height_parent_i:height_parent_i = height_i + 1;}}RE;}TE <int V_max,LI<int> E(CRI),int digit>VO DepthFirstSearchOnTree<V_max,E,digit>::SetWeight(){assert(!m_set_weight);m_set_weight = true;CRI V = DepthFirstSearch<V_max,E>::SZ();m_weight.resize(V);for(int i = 0;i < V;i++){CRI reversed_i = m_reversed[i];CRI parent_i = Parent(reversed_i);if(parent_i != -1){m_weight[parent_i] += m_weight[reversed_i] + 1;}}RE;}TE <int V_max,LI<int> E(CRI),int digit>VO DepthFirstSearchOnTree<V_max,E,digit>::SetDoubling(){assert(!m_set_doubling);m_set_doubling = true;CRI V = DepthFirstSearch<V_max,E>::SZ();{VE<int>& doubling_0 = m_doubling[0];doubling_0.reserve(V);CRI r = Root();for(int i = 0;i < V;i++ ){doubling_0.push_back(Parent(i));}}for(int d = 1;d < digit;d++ ){VE<int>& doubling_d = m_doubling[d];VE<int>& doubling_d_minus = m_doubling[d-1];doubling_d.reserve(V);for(int i = 0;i < V;i++){CRI doubling_d_minus_i = doubling_d_minus[i];doubling_d.push_back(doubling_d_minus_i == -1?-1:doubling_d_minus[doubling_d_minus_i]);}}}TE <int V_max,LI<int> E(CRI),int digit> TE <TY T,T m_T(const T&,const T&)>T DepthFirstSearchOnTree<V_max,E,digit>::RootingDP(const T(&a)[V_max]){if(! m_set_children){SetChildren();}CRI V = DepthFirstSearch<V_max,E>::SZ();LI<T> children_value[V_max] = {};T temp;for(int n = 0;n < V;n++){CRI i = NodeNumber(n,true);LI<T>& children_value_i = children_value[i];temp = a[i];WH(!children_value_i.empty()){temp = m_T(temp,children_value_i.front());children_value_i.pop_front();}CRI j = Parent(i);if(j != -1){children_value[j].push_back(temp);}}RE temp;}TE <int V_max,LI<int> E(CRI),int digit> TE <TY T,T m_T(CO T&,CO T&),CO T& e_T(),T f(CO T&,CRI)>VO DepthFirstSearchOnTree<V_max,E,digit>::RerootingDP(T(&d)[V_max]){if(! m_set_children){SetChildren();}CRI V = DepthFirstSearch<V_max,E>::SZ();CO T& e = e_T();VE<T> children_value[V_max] ={};VE<T> left_sum[V_max] ={};VE<T> right_sum[V_max] ={};for(int i = 0;i < V;i++){children_value[i].resize(m_children[i].SZ());}for(int n = 0;n < V;n++){CRI i = NodeNumber(n,true);CO VE<T>& children_value_i = children_value[i];CO int SZ_i = children_value_i.SZ();T temp = e;VE<T>& left_sum_i = left_sum[i];left_sum_i.reserve(SZ_i + 1);left_sum_i.push_back(temp);for(int m = 0;m < SZ_i;m++){left_sum_i.push_back(temp = m_T(temp,children_value_i[m]));}CRI j = Parent(i);if(j != -1){children_value[j][m_children_num[i]] = f(temp,i);}temp = e;VE<T>& right_sum_i = right_sum[i];right_sum_i.resize(SZ_i);for(int m = 1;m <= SZ_i;m++){right_sum_i[ SZ_i - m ] = temp;temp = m_T(children_value_i[SZ_i - m],temp);}}for(int n = 1;n < V;n++){CRI i = NodeNumber(n);CRI j = Parent(i);CRI k = ChildrenNumber(i);VE<T>& left_sum_i = left_sum[i];VE<T>& right_sum_i = right_sum[i];CO int SZ_i = right_sum_i.SZ();CO T rest_i = f(m_T(left_sum[j][k],right_sum[j][k]),j);for(int m = 0;m <= SZ_i;m++){T& left_sum_im = left_sum_i[m];left_sum_im = m_T(rest_i,left_sum_im);}}for(int i = 0;i < V;i++){d[i] = f(left_sum[i].back(),i);}RE;} // inline CEXPR( ll , P , 998244353 ); // inline CEXPR( ll , P , 1000000007 ); // inline CEXPR( int , bound_N , 10 ); inline DEXPR( int , bound_N , 300000 , 100 ); // 0が5個 // inline CEXPR( int , bound_N , 1000000000 ); // 0が9個 // inline CEXPR( ll , bound_N , 1000000000000000000 ); // 0が18個 TYPE_OF( bound_N ) N; // // inline CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); // // inline CEXPR( int , bound_M , 10 ); // inline DEXPR( int , bound_M , 100000 , 100 ); // 0が5個 // // inline CEXPR( int , bound_M , 1000000000 ); // 0が9個 // // inline CEXPR( ll , bound_M , 1000000000000000000 ); // 0が18個 // TYPE_OF( bound_M ) M; // inline DEXPR( int , bound_H , 1000 , 10 ); // // inline DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // // inline CEXPR( int , bound_H , 1000000000 ); // 0が9個 // inline CEXPR( int , bound_W , bound_H ); // #if bound_H < ( 1 << 16 ) // inline CEXPR( int , bound_HW , bound_H * bound_W ); // #else // inline CEXPR( ll , bound_HW , ll( bound_H ) * bound_W ); // #endif // // CEXPR( int , bound_HW , 100000 ); // 0が5個 // // CEXPR( int , bound_HW , 1000000000 ); // 0が5個 // int H , W , H_minus , W_minus; // inline pair<int,int> EnumHW( const int& v ) { return { v / W , v % W }; } // inline int EnumHW_inv( const int& h , const int& w ) { return h * W + w; } // inline void SetEdgeOnGrid( const string& Si , const int& i , list<int> ( &e )[bound_HW] , const char& walkable = '.' ){FOR(j,0,W){if(Si[j]==walkable){int v = EnumHW_inv(i,j);if(i>0){e[EnumHW_inv(i-1,j)].push_back(v);}if(i+1<H){e[EnumHW_inv(i+1,j)].push_back(v);}if(j>0){e[EnumHW_inv(i,j-1)].push_back(v);}if(j+1<W){e[EnumHW_inv(i,j+1)].push_back(v);}}}} // const string direction[4] = {"U","R","D","L"}; // inline int DirectionNumberOnGrid( const int& i , const int& j , const int& k , const int& h ){return i<k?2:i>k?0:j<h?1:j>h?3:(assert(false),-1);} // inline int DirectionNumberOnGrid( const int& v , const int& w ){auto [i,j]=EnumHW(v);auto [k,h]=EnumHW(w);return DirectionNumberOnGrid(i,j,k,h);} // inline int ReverseDirectionNumberOnGrid( const int& n ){assert(0<=n&&n<4);return(n+2)%4;} using path_type = int; // using path_type = pair<int,ll>; list<path_type> e[bound_N]; // list<path_type> e[bound_HW]; list<path_type> E( const int& i ) { list<path_type> answer = e[i]; // 入力によらない処理 return answer; } // template <typename T> inline T add( const T& t0 , const T& t1 ) { return t0 + t1; } // template <typename T> inline const T& zero() { static const T z = 0; return z; } // template <typename T> inline T multiply( const T& t0 , const T& t1 ) { return t0 * t1; } // template <typename T> inline const T& one() { static const T o = 1; return o; } // inline int id( const int& v ) { return v; } int main() { UNTIE; // LIBRARY_SEARCH; START_MAIN; // DEXPR( int , bound_T , 100000 , 100 ); // CIN_ASSERT( T , 1 , bound_T ); // REPEAT( T ){ // } // CIN( int , N ); // CIN( ll , N ); SET_ASSERT( N , 1 , bound_N ); // // CIN( int , M ); // // CIN( ll , M ); // SET_ASSERT( M , 1 , bound_M ); // // CIN( int , K ); // // CIN( ll , K ); // CIN( string , S ); // CIN( string , T ); // SET_ASSERT( H , 1 , bound_H ); // SET_ASSERT( W , 1 , bound_W ); // H_minus = H - 1; // W_minus = W - 1; // TYPE_OF( bound_HW ) HW = TYPE_OF( bound_HW )( H ) * W; // assert( HW <= bound_HW ); // FOR( i , 0 , H ){ // CIN( string , Si ); // SetEdgeOnGrid( Si , i , e ); // } // // CEXPR( int , bound_Ai , 10 ); // // CEXPR( int , bound_Ai , 100000 ); // 0が5個 // CEXPR( int , bound_Ai , 1000000000 ); // 0が9個 // // CEXPR( ll , bound_Ai , 1000000000000000000 ); // 0が18個 // // CEXPR( int , bound_Bi , bound_Ai ); // int A[N]; // ll A[N]; // // int A[bound_N]; // // ll A[bound_N]; // int B[N]; // // ll B[N]; // // int B[bound_N]; // // ll B[bound_N]; // FOR( i , 0 , N ){ // CIN( int , Ai ); // // CIN( ll , Ai ); // // CIN_ASSERT( Ai , 0 , bound_Ai ); // A[i] = Ai; // CIN( int , Bi ); // // CIN( ll , Bi ); // // CIN_ASSERT( Bi , 0 , bound_Bi ); // B[i] = Bi; // } FOR( i , 1 , N ){ CIN_ASSERT( ui , 1 , N ); CIN_ASSERT( vi , 1 , N ); ui--; vi--; e[ui].push_back( vi ); e[vi].push_back( ui ); } DepthFirstSearchOnTree<bound_N,E> dfst{ N , 0 }; FOR( i , 0 , N ){ int answer_i = 0; const int& j = dfst.Parent( i ); if( j != -1 ){ const int& k = dfst.Parent( j ); if( k != -1 ){ answer_i++; } answer_i += dfst.Children( j ).size(); answer_i--; } const vector<int>& children = dfst.Children( i ); FOR_ITR( children ){ answer_i += dfst.Children( *itr ).size(); } COUT( answer_i ); } // DEXPR( int , bound_Q , 100000 , 100 ); // CIN_ASSERT( Q , 1 , bound_Q ); // REPEAT( Q ){ // COUT( N ); // } // ll guchoku = Guchoku(); // ll answer = 0; // COUT( ( answer ) ); // if( answer == guchoku ){ // CERR( answer << " == " << guchoku ); // } else { // CERR( answer << " != " << guchoku ); // QUIT; // } FINISH_MAIN; QUIT; }