// https://yukicoder.me/submissions/934698 // フリーズの原因を調べ中。 #ifdef INCLUDE_MAIN inline void Solve() { // // 数 // // DEXPR( ll , bound_N , 100000 , 100 ); // 0が5個 // // // DEXPR( ll , bound_N , 1000000000 , 100 ); // 0が9個 // // // DEXPR( ll , bound_N , 1000000000000000000 , 100 ); // 0が18個 // // CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); // // // DEXPR( ll , bound_M , 100000 , 100 ); // 0が5個 // // // DEXPR( ll , bound_M , 1000000000 , 100 ); // 0が9個 // // // DEXPR( ll , bound_M , 1000000000000000000 , 100 ); // 0が18個 CIN( ll , N , M ); // // CIN( ll , M ); // // CIN( ll , N , M , K ); // // CIN_ASSERT( N , 1 , bound_N ); // ランダムテスト用。上限のデフォルト値は10^5。 // // CIN_ASSERT( M , 1 , bound_M ); // ランダムテスト用。上限のデフォルト値は10^5。 // ll answer = 0; // // MP answer = 0; // // auto answer = Answer( N , M , K ); // // COUT( answer ); // RETURN( answer ); // // 文字列 // CIN( string , S ); // // CIN( string , T ); // ll answer = 0; // // MP answer = 0; // // auto answer = Answer( N , M , K ); // // COUT( answer ); // RETURN( answer ); // 配列 // CIN_A( ll , A , N ); // CIN_A( ll , B , N ); // ll A[bound_N]; // 関数(コンストラクタ)の引数に使う。長さのデフォルト値は10^5。 // ll B[bound_N]; // 関数(コンストラクタ)の引数に使う。長さのデフォルト値は10^5。 // MP answer = 0; // auto answer = Answer( N , M , K ); // COUT( answer ); // COUT_A( A , N ); // RETURN( answer ); // // 順列 // vector A( N ); // vector A_inv( N ); // FOR( i , 0 , N ){ // cin >> A[i]; // A_inv[--A[i]] = i; // } // ll answer = 0; // // MP answer = 0; // // auto answer = Answer( N , M , K ); // // COUT( answer ); // // COUT_A( A , N ); // RETURN( answer ); UnionFindForest uff( N ); FOR( i , 0 , N ){ CIN_ASSERT( Ci , 1 , N ); uff.push_RightMost( --Ci ); } // グラフ e.resize( N ); // e.resize( N ); FOR( j , 0 , M ){ CIN_ASSERT( uj , 1 , N ); CIN_ASSERT( vj , 1 , N ); uj--; vj--; if( uff[uj] == uff[vj] ){ uff.Graft( uj , vj ); } // e[uj].push_back( vj ); // e[vj].push_back( uj ); // CIN( ll , wj ); // e[uj].push_back( { vj , wj } ); // e[vj].push_back( { uj , wj } ); } ll answer = 0; cerr << __LINE__ << endl; int C[N]; cerr << __LINE__ << endl; FOR( i , 0 , N ){ C[i] = 0; } cerr << __LINE__ << endl; const uint& size = uff.GetSZOfRoot(); cerr << __LINE__ << endl; FOR( i , 0 , size ){ const int& Ci = uff[uff.GetRoot( i )]; ASSERT( Ci , 0 , N - 1 ); C[Ci]++; } cerr << __LINE__ << endl; FOR( i , 0 , N ){ answer += max( C[i] - 1 , 0 ); } cerr << __LINE__ << endl; // MP answer = 0; // auto answer = Answer( N , M , K ); // COUT( answer ); // COUT_A( A , N ); RETURN( answer ); // // 座標圧縮や単一クエリタイプなどのための入力格納 // vector > data( M ); // FOR( j , 0 , M ){ // CIN( ll , x , y , z ); // data[j] = { x , y , z }; // } // ll answer = 0; // // MP answer = 0; // // auto answer = Answer( N , M , K ); // // COUT( answer ); // // COUT_A( A , N ); // RETURN( answer ); // // 一般のクエリ // CIN( int , Q ); // // DEXPR( int , bound_Q , 100000 , 100 ); // 基本不要。 // // CIN_ASSERT( Q , 1 , bound_Q ); // 基本不要。 // // vector > query( Q ); // // vector > query( Q ); // FOR( q , 0 , Q ){ // CIN( int , type ); // if( type == 1 ){ // CIN( int , x , y ); // // query[q] = { type , x , y }; // } else if( type == 2 ){ // CIN( int , x , y ); // // query[q] = { type , x , y }; // } else { // CIN( int , x , y ); // // query[q] = { type , x , y }; // } // // CIN( int , x , y ); // // // query[q] = { x , y }; // } // // sort( query , query + Q ); // // FOR( q , 0 , Q ){ // // auto& [x,y] = query[q]; // // // auto& [type,x,y] = query[q]; // // } // auto answer = Answer( N , M , K ); // // COUT( answer ); // // COUT_A( A , N ); // RETURN( answer ); // // グリッド // // DEXPR( int , bound_H , 2000 , 30 ); // // // DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // // // CEXPR( int , bound_H , 1000000000 ); // 0が9個 // // // CEXPR( int , bound_W , bound_H ); // // static_assert( ll( bound_H ) * bound_W < ll( 1 ) << 31 ); // // CEXPR( int , bound_HW , bound_H * bound_W ); // // // CEXPR( int , bound_HW , 100000 ); // 0が5個 // // // CEXPR( int , bound_HW , 1000000 ); // 0が6個 // cin >> H >> W; // // SET_ASSERT( H , 1 , bound_H ); // ランダムテスト用。上限のデフォルト値は2*10^3。 // // SET_ASSERT( W , 1 , bound_W ); // ランダムテスト用。上限のデフォルト値は2*10^3。 // H_minus = H - 1; // W_minus = W - 1; // HW = H * W; // // assert( HW <= bound_HW ); // 基本不要。上限のデフォルト値は4*10^6。 // vector S( H ); // FOR( i , 0 , H ){ // cin >> S[i]; // // SetEdgeOnGrid( S[i] , i , e ); // // SetWallOnGrid( S[i] , i , non_wall ); // } // // {h,w}へデコード: EnumHW( v ) // // {h,w}をコード: EnumHW_inv( h , w ); // // (i,j)->(k,h)の方向番号を取得: DirectionNumberOnGrid( i , j , k , h ); // // v->wの方向番号を取得: DirectionNumberOnGrid( v , w ); // // 方向番号の反転U<->D、R<->L: ReverseDirectionNumberOnGrid( n ); // ll answer = 0; // // MP answer = 0; // // auto answer = Answer( N , M , K ); // // COUT( answer ); // // COUT_A( A , N ); // RETURN( answer ); } REPEAT_MAIN(1); #else // INCLUDE_MAIN #ifdef INCLUDE_SUB template list E( const int& i ) { // list answer{}; list answer = e[i]; // VVV 入力によらない処理は以下に挿入する。 // AAA 入力によらない処理は以上に挿入する。 return answer; } template inline T F( const T& t ){ return f[t]; } template inline T G( const int& i ){ return g[i]; } ll Naive( int N , int M , int K ) { ll answer = N + M + K; return answer; } ll Answer( ll N , ll M , ll K ) { // START_WATCH; ll answer = N + M + K; // // TLに準じる乱択や全探索。デフォルトの猶予は100.0[ms]。 // CEXPR( double , TL , 2000.0 ); // while( CHECK_WATCH( TL ) ){ // } return answer; } inline void Experiment() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // COUT( N , M , K , ":" , Naive( N , M , K ) ); // } // } // // cout << Naive( N ) << ",\n"[N==bound]; // } } inline void SmallTest() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // COMPARE( N , M , K ); // } // } // // COMPARE( N ); // } } #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_SUB #ifdef INCLUDE_LIBRARY /* C-x 3 C-x o C-x C-fによるファイル操作用 BFS: c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/BreadthFirstSearch/compress.txt CoordinateCompress: c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/CoordinateCompress/compress.txt DFSOnTree c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/DepthFirstSearch/Tree/a.hpp Divisor: c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Prime/Divisor/compress.txt Polynomial c:/Users/user/Documents/Programming/Mathematics/Polynomial/compress.txt UnionFind c:/Users/user/Documents/Programming/Utility/VLTree/UnionFindForest/compress.txt */ // VVV 常設でないライブラリは以下に挿入する。 // 1つ目のコンストラクタは領域を確保するだけなのでpush_RightMostで要素を追加する必要があることに注意。 TE CL EntryOfVLTree{PU:T m_t;EntryOfVLTree* m_left_branch;EntryOfVLTree* m_right_branch;EntryOfVLTree* m_leftmost_node;EntryOfVLTree* m_rightmost_node;IN EntryOfVLTree();TE IN EntryOfVLTree(CO Arg&);TE IN EntryOfVLTree(CO Arg&,EntryOfVLTree* CO&,EntryOfVLTree* CO&);IN EntryOfVLTree(CO EntryOfVLTree&);EntryOfVLTree& OP=(CO EntryOfVLTree&);}; TE IN EntryOfVLTree::EntryOfVLTree(): m_t(),m_left_branch(TH),m_right_branch(TH),m_leftmost_node(TH),m_rightmost_node(TH){}TE TE IN EntryOfVLTree::EntryOfVLTree(CO Arg& t): EntryOfVLTree(t,TH,TH){}TE TE IN EntryOfVLTree::EntryOfVLTree(CO Arg& t,EntryOfVLTree* CO& left_branch,EntryOfVLTree* CO& right_branch): m_t(t),m_left_branch(left_branch),m_right_branch(right_branch),m_leftmost_node(TH),m_rightmost_node(TH){}TE IN EntryOfVLTree::EntryOfVLTree(CO EntryOfVLTree& e): m_t(e.m_t),m_left_branch(e.m_left_branch == &e?TH:e.m_left_branch),m_right_branch(e.m_right_branch == &e?TH:e.m_right_branch),m_leftmost_node(TH),m_rightmost_node(TH){if(e.m_leftmost_node != &e){m_leftmost_node = e.m_leftmost_node;m_rightmost_node = e.m_rightmost_node;}}TE EntryOfVLTree& EntryOfVLTree::OP=(CO EntryOfVLTree& e){m_t = e.m_t;m_left_branch =(e.m_left_branch == &e?TH:e.m_left_branch);m_right_branch =(e.m_right_branch == &e?TH:e.m_right_branch);if(e.m_leftmost_node == &e){m_leftmost_node = m_rightmost_node = TH;}else{m_leftmost_node = e.m_leftmost_node;m_rightmost_node = e.m_rightmost_node;}RE *TH;} TE CL IteratorOfVLTree{PU:EntryOfVLTree* m_p;IN IteratorOfVLTree()NE;IN IteratorOfVLTree(EntryOfVLTree* CO&)NE;IN IteratorOfVLTree(CO IteratorOfVLTree&)NE;IN T& OP*()CO;IN T* OP->()CO;IN IteratorOfVLTree& OP=(CO IteratorOfVLTree&)NE;IteratorOfVLTree& OP++(int)NE;IteratorOfVLTree& OP--(int)NE;IteratorOfVLTree& OP[](CO int&);IteratorOfVLTree& Shift()NE;TE IteratorOfVLTree& Shift(CO int&,CO Args&...);IN bool IsLeaf()CO NE;IN bool IsLeftMost()CO NE;IN bool IsRightMost()CO NE;IN bool IsValid()CO NE;}; TE CL COIteratorOfVLTree{PU:CO EntryOfVLTree* m_p;IN COIteratorOfVLTree()NE;IN COIteratorOfVLTree(CO EntryOfVLTree* CO&)NE;IN COIteratorOfVLTree(CO COIteratorOfVLTree&)NE;IN COIteratorOfVLTree(CO IteratorOfVLTree&)NE;IN CO T& OP*()CO;IN CO T* OP->()CO;IN COIteratorOfVLTree& OP=(CO COIteratorOfVLTree&)NE;IN COIteratorOfVLTree& OP=(CO IteratorOfVLTree&)NE;COIteratorOfVLTree& OP++(int)NE;COIteratorOfVLTree& OP--(int)NE;COIteratorOfVLTree& OP[](CO int&);COIteratorOfVLTree& Shift()NE;TE COIteratorOfVLTree& Shift(CO int&,CO Args&...);IN bool IsLeaf()CO NE;IN bool IsLeftMost()CO NE;IN bool IsRightMost()CO NE;IN bool IsValid()CO NE;ST IN bool Equal(CO IteratorOfVLTree&,CO IteratorOfVLTree&)NE;ST IN bool Equal(CO COIteratorOfVLTree&,CO IteratorOfVLTree&)NE;ST IN bool Equal(CO IteratorOfVLTree&,CO COIteratorOfVLTree&)NE;ST IN bool Equal(CO COIteratorOfVLTree&,CO COIteratorOfVLTree&)NE;}; TE IN IteratorOfVLTree::IteratorOfVLTree()NE:m_p(nullptr){}TE IN IteratorOfVLTree::IteratorOfVLTree(EntryOfVLTree* CO& p)NE:m_p(p){}TE IN IteratorOfVLTree::IteratorOfVLTree(CO IteratorOfVLTree& itr)NE:m_p(itr.m_p){}TE IN T& IteratorOfVLTree::OP*()CO{RE m_p->m_t;}TE IN T* IteratorOfVLTree::OP->()CO{RE &(*(*TH));}TE IN IteratorOfVLTree& IteratorOfVLTree::OP=(CO IteratorOfVLTree& itr)NE{m_p = itr.m_p;RE *TH;}TE IteratorOfVLTree& IteratorOfVLTree::OP++(int)NE{if(m_p == nullptr){RE *TH;}if(m_p == m_p->m_right_branch){m_p = nullptr;}else{m_p = m_p->m_right_branch;}RE *TH;}TE IteratorOfVLTree& IteratorOfVLTree::OP--(int)NE{if(m_p == nullptr){RE *TH;}if(m_p == m_p->m_left_branch){m_p = nullptr;}else{m_p = m_p->m_left_branch;}RE *TH;}TE IteratorOfVLTree& IteratorOfVLTree::OP[](CO int& n){if(n > 0){m_p = m_p->m_leftmost_node;for(int i = 1;i < n;i++){m_p = m_p->m_right_branch;}}else{if(n < 0){m_p = m_p->m_rightmost_node;for(int i = -1;n < i;i--){m_p = m_p->m_left_branch;}}}RE *TH;}TE IteratorOfVLTree& IteratorOfVLTree::Shift()NE{RE *TH;}TE TE IteratorOfVLTree& IteratorOfVLTree::Shift(CO int& n,CO Args&... args){OP[](n);RE Shift(args...);}TE IN bool IteratorOfVLTree::IsLeaf()CO NE{RE(m_p == nullptr)? false :(m_p == m_p->m_leftmost_node);}TE IN bool IteratorOfVLTree::IsLeftMost()CO NE{RE(m_p == nullptr)? false :(m_p == m_p->m_left_branch);}TE IN bool IteratorOfVLTree::IsRightMost()CO NE{RE(m_p == nullptr)? false :(m_p == m_p->m_right_branch);}TE IN bool IteratorOfVLTree::IsValid()CO NE{RE m_p != nullptr;}TE IN COIteratorOfVLTree::COIteratorOfVLTree()NE:m_p(nullptr){}TE IN COIteratorOfVLTree::COIteratorOfVLTree(CO EntryOfVLTree* CO& p)NE:m_p(p){}TE IN COIteratorOfVLTree::COIteratorOfVLTree(CO COIteratorOfVLTree& itr)NE:m_p(itr.m_p){}TE IN COIteratorOfVLTree::COIteratorOfVLTree(CO IteratorOfVLTree& itr)NE:m_p(itr.m_p){}TE IN CO T& COIteratorOfVLTree::OP*()CO{RE m_p->m_t;};TE IN CO T* COIteratorOfVLTree::OP->()CO{RE &(*(*TH));}TE IN COIteratorOfVLTree& COIteratorOfVLTree::OP=(CO COIteratorOfVLTree& itr)NE{m_p = itr.m_p;RE *TH;}TE IN COIteratorOfVLTree& COIteratorOfVLTree::OP=(CO IteratorOfVLTree& itr)NE{m_p = itr.m_p;RE *TH;}TE COIteratorOfVLTree& COIteratorOfVLTree::OP++(int)NE{if(m_p == nullptr){RE *TH;}if(m_p == m_p->m_right_branch){m_p = nullptr;}else{m_p = m_p->m_right_branch;}RE *TH;}TE COIteratorOfVLTree& COIteratorOfVLTree::OP--(int)NE{if(m_p == nullptr){RE *TH;}if(m_p == m_p->m_left_branch){m_p = nullptr;}else{m_p = m_p->m_left_branch;}RE *TH;}TE COIteratorOfVLTree& COIteratorOfVLTree::OP[](CO int& n){if(n > 0){m_p = m_p->m_leftmost_node;for(int i = 1;i < n;i++){m_p = m_p->m_right_branch;}}if(n < 0){m_p = m_p->m_rightmost_node;for(int i = -1;n < i;i--){m_p = m_p->m_left_branch;}}RE *TH;}TE COIteratorOfVLTree& COIteratorOfVLTree::Shift()NE{RE *TH;}TE TE COIteratorOfVLTree& COIteratorOfVLTree::Shift(CO int& n,CO Args&... args){OP[](n);RE Shift(args...);}TE IN bool COIteratorOfVLTree::IsLeaf()CO NE{RE(m_p == nullptr)? false :(m_p == m_p->m_leftmost_mode);}TE IN bool COIteratorOfVLTree::IsLeftMost()CO NE{RE(m_p == nullptr)? false :(m_p == m_p->m_left_branch);}TE IN bool COIteratorOfVLTree::IsRightMost()CO NE{RE(m_p == nullptr)? false :(m_p == m_p->m_right_branch);}TE IN bool COIteratorOfVLTree::IsValid()CO NE{RE m_p != nullptr;}TE IN bool COIteratorOfVLTree::Equal(CO IteratorOfVLTree& itr0,CO IteratorOfVLTree& itr1)NE{RE itr0.m_p == itr1.m_p;}TE IN bool COIteratorOfVLTree::Equal(CO COIteratorOfVLTree& itr0,CO IteratorOfVLTree& itr1)NE{RE itr0.m_p == itr1.m_p;}TE IN bool COIteratorOfVLTree::Equal(CO IteratorOfVLTree& itr0,CO COIteratorOfVLTree& itr1)NE{RE itr0.m_p == itr1.m_p;}TE IN bool COIteratorOfVLTree::Equal(CO COIteratorOfVLTree& itr0,CO COIteratorOfVLTree& itr1)NE{RE itr0.m_p == itr1.m_p;}TE IN bool OP==(CO IteratorOfVLTree& itr0,CO IteratorOfVLTree& itr1)NE{RE COIteratorOfVLTree::Equal(itr0,itr1);}TE IN bool OP!=(CO IteratorOfVLTree& itr0,CO IteratorOfVLTree& itr1)NE{RE !(itr0 == itr1);}TE IN bool OP==(CO COIteratorOfVLTree& itr0,CO IteratorOfVLTree& itr1)NE{RE COIteratorOfVLTree::Equal(itr0,itr1);}TE IN bool OP!=(CO COIteratorOfVLTree& itr0,CO IteratorOfVLTree& itr1)NE{RE !(itr0 == itr1);}TE IN bool OP==(CO IteratorOfVLTree& itr0,CO COIteratorOfVLTree& itr1)NE{RE COIteratorOfVLTree::Equal(itr0,itr1);}TE IN bool OP!=(CO IteratorOfVLTree& itr0,CO COIteratorOfVLTree& itr1)NE{RE !(itr0 == itr1);}TE IN bool OP==(CO COIteratorOfVLTree& itr0,CO COIteratorOfVLTree& itr1)NE{RE COIteratorOfVLTree::Equal(itr0,itr1);}TE IN bool OP!=(CO COIteratorOfVLTree& itr0,CO COIteratorOfVLTree& itr1)NE{RE !(itr0 == itr1);} TE CL WrappedType{PU:Arg m_t;TE IN WrappedType(CO ARGS&... args);IN VO Set(CO Arg& t);IN CO Arg& Get()CO NE;}; TE CL WrappedTypes{}; TE TE IN WrappedType::WrappedType(CO ARGS&... args): m_t(args...){}TE IN VO WrappedType::Set(CO Arg& t){m_t = t;}TE IN CO Arg& WrappedType::Get()CO NE{RE m_t;} TE CL VLTree; TE CL VLSubTree{PU:EntryOfVLTree m_e;EntryOfVLTree* m_p_root;uint m_SZ;IN VLSubTree();TE IN VLSubTree(CO Arg1&,CO Arg2&...);TE IN VLSubTree(CO WrappedType& t);IN VLSubTree(CO VLSubTree&);IN VLSubTree(EntryOfVLTree&);IN VLSubTree(CO IteratorOfVLTree&);IN VLSubTree(CO int&,CO EntryOfVLTree&);IN VLSubTree(CO int&,CO COIteratorOfVLTree&);VO LeafToTree(CO VLSubTree&);VO Graft(VLSubTree&);virtual ~VLSubTree()= default;VLSubTree& OP=(CO VLSubTree&);IN CRUI SZ()CO NE;IN VO CutBranches();IN bool IsLeaf()CO NE;IN VLSubTree LeftMostSubTree();IN VLSubTree RightMostSubTree();IN VLTree LeftMostSubTreeCopy()CO;IN VLTree RightMostSubTreeCopy()CO;IN VO push_RightMost()CO NE;TE VO push_RightMost(CO Arg1&,CO Arg2&...);TE VO push_RightMost(CO VLTree&,CO Args&...);TE VO push_LeftMost(CO Arg&);VO pop_RightMost();VO pop_LeftMost();VO pop_Root();US iterator = IteratorOfVLTree;US CO_iterator = COIteratorOfVLTree;IN iterator LeftMostNode()NE;IN CO_iterator LeftMostNode()CO NE;IN iterator RightMostNode()NE;IN CO_iterator RightMostNode()CO NE;iterator LeftMostLeaf()NE;CO_iterator LeftMostLeaf()CO NE;iterator RightMostLeaf()NE;CO_iterator RightMostLeaf()CO NE;IN iterator Root()NE;IN CO_iterator Root()CO NE;TE IN iterator GetIterator(CO Args&...);TE IN CO_iterator GetIterator(CO Args&...)CO;TE VO insert(CO iterator&,CO Arg&);iterator erase(iterator&);IN CO T& GetRoot()CO NE;IN T& RefRoot()NE;IN VO SetRoot(CO T&);TE IN CO T& GetNode(CO Args&...)CO;VLSubTree OP[](CRUI);VLSubTree OP[](iterator&);VLTree OP[](CO CO_iterator&)CO;VLTree GetBranchCopy(CRUI)CO;VLTree GetBranchCopy(CO iterator&)CO;VLTree GetBranchCopy(CO CO_iterator&)CO;VO Concatenate(CO VLTree&);VO Concatenate(CO iterator&,CO VLTree&);bool CheckContain(CO iterator&)CO NE;bool CheckContain(CO CO_iterator&)CO NE;string Display()CO;}; TE IN VLSubTree::VLSubTree(): m_e(),m_p_root(&m_e),m_SZ(0){}TE TE IN VLSubTree::VLSubTree(CO Arg1& t0,CO Arg2&... t1): VLSubTree(){push_RightMost(t0,t1...);}TE TE IN VLSubTree::VLSubTree(CO WrappedType& t): m_e(t.Get()),m_p_root(&m_e),m_SZ(0){}TE IN VLSubTree::VLSubTree(CO VLSubTree& a): m_e(a.m_e.m_t),m_p_root(&m_e),m_SZ(0){LeafToTree(a);}TE VLSubTree::VLSubTree(EntryOfVLTree& e):m_e(e),m_p_root(&e),m_SZ(0){EntryOfVLTree* p = m_p_root->m_leftmost_node;if(p != m_p_root){m_SZ++;EntryOfVLTree* CO p_rightmost = m_p_root->m_rightmost_node;WH(p != p_rightmost){p = p->m_right_branch;m_SZ++;}}}TE IN VLSubTree::VLSubTree(CO IteratorOfVLTree& itr): VLSubTree(*(itr.m_p)){}TE VLSubTree::VLSubTree(CO int& dummy,CO EntryOfVLTree& e):m_e(e.m_t),m_p_root(&m_e),m_SZ(0){CO EntryOfVLTree* p = e.m_leftmost_node;CO EntryOfVLTree* CO p_rightmost = e.m_rightmost_node;bool b =(p != &e);WH(b){push_RightMost(VLTree(dummy,*p));b =(p != p_rightmost);p = p->m_right_branch;}}TE IN VLSubTree::VLSubTree(CO int& dummy,CO COIteratorOfVLTree& itr): VLSubTree(dummy,*(itr.m_p)){}TE VLSubTree& VLSubTree::OP=(CO VLSubTree& a){if(TH != &a){CutBranches();LeafToTree(a);}RE *TH;}TE VO VLSubTree::LeafToTree(CO VLSubTree& a){m_p_root->m_t = a.m_p_root->m_t;EntryOfVLTree* p = a.m_p_root->m_leftmost_node;CRUI N = a.m_SZ;for(uint n = 0;n < N;n++){push_RightMost(VLTree(0,*p));p = p->m_right_branch;}RE;}TE IN CRUI VLSubTree::SZ()CO NE{RE m_SZ;}TE IN VO VLSubTree::CutBranches(){WH(m_SZ > 0)pop_RightMost();}TE IN bool VLSubTree::IsLeaf()CO NE{RE m_SZ == 0;}TE IN VLSubTree VLSubTree::LeftMostSubTree(){RE VLSubTree(*(m_p_root->m_leftmost_node));}TE IN VLSubTree VLSubTree::RightMostSubTree(){RE VLSubTree(*(m_p_root->m_rightmost_node));}TE IN VLTree VLSubTree::LeftMostSubTreeCopy()CO{RE VLTree(0,*(m_p_root->m_leftmost_node));}TE IN VLTree VLSubTree::RightMostSubTreeCopy()CO{RE VLTree(0,*(m_p_root->m_rightmost_node));}TE IN VO VLSubTree::push_RightMost()CO NE{}TE TE VO VLSubTree::push_RightMost(CO Arg1& t0,CO Arg2&... t1){auto p = new EntryOfVLTree(t0);EntryOfVLTree*& p_rightmost = m_p_root->m_rightmost_node;if(p_rightmost == m_p_root){m_p_root->m_leftmost_node = p;}else{p->m_left_branch = p_rightmost;p_rightmost->m_right_branch = p;}p_rightmost = p;m_SZ++;push_RightMost(t1...);RE;}TE TE VO VLSubTree::push_RightMost(CO VLTree& t0,CO Args&... t1){push_RightMost(t0.m_p_root->m_t);Concatenate(t0);push_RightMost(t1...);RE;}TE TE VO VLSubTree::push_LeftMost(CO Arg& t){auto p = new EntryOfVLTree(t);EntryOfVLTree*& p_leftmost = m_p_root->m_leftmost_node;if(p_leftmost == m_p_root){m_p_root->m_rightmost_node = p;}else{p->m_right_branch = p_leftmost;p_leftmost->m_left_branch = p;}p_leftmost = p;m_SZ++;RE;}TE VO VLSubTree::pop_RightMost(){assert(m_SZ > 0);EntryOfVLTree* p_rightmost = m_p_root->m_rightmost_node;VLSubTree t{*p_rightmost};t.CutBranches();if(m_SZ == 1){m_p_root->m_leftmost_node = m_p_root;m_p_root->m_rightmost_node = m_p_root;}else{EntryOfVLTree* CO& p_rightmost_prev = p_rightmost->m_left_branch;m_p_root->m_rightmost_node = p_rightmost_prev;p_rightmost_prev->m_right_branch = p_rightmost_prev;}delete p_rightmost;m_SZ--;RE;}TE VO VLSubTree::pop_LeftMost(){assert(m_SZ > 0);EntryOfVLTree* p_leftmost = m_p_root->m_leftmost_node;VLSubTree t{*p_leftmost};t.CutBranches();if(m_SZ == 1){m_p_root->m_leftmost_node = m_p_root;m_p_root->m_rightmost_node = m_p_root;}else{EntryOfVLTree* CO& p_leftmost_next = p_leftmost->m_right_branch;m_p_root->m_leftmost_node = p_leftmost_next;p_leftmost_next->m_left_branch = p_leftmost_next;}delete p_leftmost;m_SZ--;RE;}TE VO VLSubTree::pop_Root(){assert(m_SZ == 1);EntryOfVLTree* p_unique_node = m_p_root->m_leftmost_node;m_p_root->m_t = p_unique_node->m_t;m_p_root->m_leftmost_node = p_unique_node->m_leftmost_node;m_p_root->m_rightmost_node = p_unique_node->m_rightmost_node;delete p_unique_node;EntryOfVLTree* p_node = m_p_root->m_leftmost_node;m_SZ = 0;if(p_node != m_p_root){m_SZ++;WH(p_node != p_node->m_right_branch){p_node = p_node->m_right_branch;m_SZ++;}}RE;}TE IN TY VLSubTree::iterator VLSubTree::LeftMostNode()NE{RE IteratorOfVLTree(m_p_root->m_leftmost_node);}TE IN TY VLSubTree::CO_iterator VLSubTree::LeftMostNode()CO NE{RE COIteratorOfVLTree(m_p_root->m_leftmost_node);}TE IN TY VLSubTree::iterator VLSubTree::RightMostNode()NE{RE IteratorOfVLTree(m_p_root->m_rightmost_node);}TE IN TY VLSubTree::CO_iterator VLSubTree::RightMostNode()CO NE{RE COIteratorOfVLTree(m_p_root->m_rightmost_node);}TE TY VLSubTree::iterator VLSubTree::LeftMostLeaf()NE{EntryOfVLTree* p = m_p_root->m_leftmost_node;WH(p != p->m_leftmost_node){p = p->m_leftmost_node;}RE IteratorOfVLTree(p);}TE TY VLSubTree::CO_iterator VLSubTree::LeftMostLeaf()CO NE{CO EntryOfVLTree* p = m_p_root->m_leftmost_node;WH(p != p->m_leftmost_node){p = p->m_leftmost_node;}RE COIteratorOfVLTree(p);}TE TY VLSubTree::iterator VLSubTree::RightMostLeaf()NE{EntryOfVLTree* p = m_p_root->m_rightmost_node;WH(p != p->m_rightmost_node){p = p->m_rightmost_node;}RE IteratorOfVLTree(p);}TE TY VLSubTree::CO_iterator VLSubTree::RightMostLeaf()CO NE{CO EntryOfVLTree* p = m_p_root->m_rightmost_node;WH(p != p->m_rightmost_node){p = p->m_rightmost_node;}RE COIteratorOfVLTree(p);}TE IN TY VLSubTree::iterator VLSubTree::Root()NE{RE IteratorOfVLTree(m_p_root);}TE IN TY VLSubTree::CO_iterator VLSubTree::Root()CO NE{RE COIteratorOfVLTree(m_p_root);}TE TE IN TY VLSubTree::iterator VLSubTree::GetIterator(CO Args&... args){RE Root().Shift(args...);}TE TE IN TY VLSubTree::CO_iterator VLSubTree::GetIterator(CO Args&... args)CO{RE Root().Shift(args...);}TE TE VO VLSubTree::insert(CO TY VLSubTree::iterator& itr,CO Arg& t){assert(CheckContain(itr));EntryOfVLTree* CO& p0 = itr.m_p;EntryOfVLTree* CO& p1 = p0->m_right_branch;auto p = new EntryOfVLTree(t,p0,p1);p1->m_left_branch = p;p0->m_right_branch = p;m_SZ++;RE;}TE TY VLSubTree::iterator VLSubTree::erase(TY VLSubTree::iterator& itr){assert(CheckContain(itr));EntryOfVLTree* CO p = itr.m_p;EntryOfVLTree* CO p0 = p->m_left_branch;EntryOfVLTree* CO p1 = p->m_right_branch;if(! itr.IsLeaf()){VLSubTree t_sub{*p};t_sub.CutBranches();}if(p0 != p){if(p != p1){itr++;p0->m_right_branch = p1;p1->m_left_branch = p0;}else{itr--;p0->m_right_branch = p0;m_p_root->m_rightmost_node = p0;}}else{if(p != p1){itr++;p1->m_left_branch = p1;m_p_root->m_leftmost_node = p1;}else{itr = Root();m_p_root->m_leftmost_node = m_p_root;m_p_root->m_rightmost_node = m_p_root;}}delete p;m_SZ--;RE itr;}TE IN CO T& VLSubTree::GetRoot()CO NE{RE m_p_root->m_t;}TE IN T& VLSubTree::RefRoot()NE{RE m_p_root->m_t;}TE IN VO VLSubTree::SetRoot(CO T& t){m_p_root->m_t = t;}TE TE IN CO T& VLSubTree::GetNode(CO Args&... args)CO{RE *(GetIterator(args...));}TE VLSubTree VLSubTree::OP[](CRUI i){assert(i < m_SZ);if(i <= m_SZ / 2){EntryOfVLTree* p = m_p_root->m_leftmost_node;for(uint n = 0;n < i;n++){p = p->m_right_branch;}RE VLSubTree(*p);}EntryOfVLTree* p = m_p_root->m_rightmost_node;for(uint n = m_SZ - 1;n > i;n--){p = p->m_left_branch;}RE VLSubTree(*p);}TE VLSubTree VLSubTree::OP[](TY VLSubTree::iterator& itr){assert(CheckContain(itr));RE VLSubTree(*(itr.m_p));}TE VLTree VLSubTree::OP[](CO TY VLSubTree::CO_iterator& itr)CO{assert(CheckContain(itr));RE VLTree(0,itr.m_p);}TE VLTree VLSubTree::GetBranchCopy(CRUI i)CO{assert(i < m_SZ);if(i <= m_SZ / 2){CO EntryOfVLTree* p = m_p_root->m_leftmost_node;for(uint n = 0;n < i;n++){p = p->m_right_branch;}RE VLTree(0,*p);}CO EntryOfVLTree* p = m_p_root->m_rightmost_node;for(uint n = m_SZ - 1;n > i;n--){p = p->m_left_branch;}RE VLTree(0,*p);}TE VLTree VLSubTree::GetBranchCopy(CO TY VLSubTree::iterator& itr)CO{assert(CheckContain(itr));RE VLTree(0,*(itr.m_p));}TE VLTree VLSubTree::GetBranchCopy(CO TY VLSubTree::CO_iterator& itr)CO{assert(CheckContain(itr));RE VLTree(0,*(itr.m_p));}TE VO VLSubTree::Concatenate(CO VLTree& t){EntryOfVLTree* CO p_rightmost = m_p_root->m_rightmost_node;assert(p_rightmost->m_rightmost_node == p_rightmost);if(m_p_root == p_rightmost){LeafToTree(t);}else{VLSubTree(*p_rightmost).LeafToTree(t);}RE;}TE VO VLSubTree::Concatenate(CO TY VLSubTree::iterator& itr,CO VLTree& t){assert(itr.IsLeaf());EntryOfVLTree* CO p = itr.m_p;if(m_p_root == p){LeafToTree(t);}else{VLSubTree(*p).LeafToTree(t);}RE;}TE VO VLSubTree::Graft(VLSubTree& t){EntryOfVLTree*& p_rightmost = m_p_root->m_rightmost_node;if(m_p_root == p_rightmost){p_rightmost = m_p_root->m_leftmost_node = t.m_p_root;}else{t.m_p_root->m_left_branch = p_rightmost;p_rightmost = p_rightmost->m_right_branch = t.m_p_root;}RE;}TE bool VLSubTree::CheckContain(CO iterator& itr)CO NE{auto p0 = itr.m_p;auto p1 = m_p_root->m_leftmost_node;for(uint i = 0;i < m_SZ;i++){if(p0 == p1){RE true;}p1 = p1->m_right_branch;}RE false;}TE bool VLSubTree::CheckContain(CO CO_iterator& itr)CO NE{auto p0 = itr.m_p;auto p1 = m_p_root->m_leftmost_node;for(uint i = 0;i < m_SZ;i++){if(p0 == p1){RE true;}p1 = p1->m_right_branch;}RE false;}TE string VLSubTree::Display()CO{string s = to_string(m_p_root->m_t);s += "(";CO EntryOfVLTree* p = m_p_root->m_leftmost_node;for(uint i = 0;i < m_SZ;i++){if(i > 0){s += ",";}s += VLTree(0,*p).Display();p = p->m_right_branch;}s += ")";RE s;}TE bool OP==(CO VLTree& t1,CO VLTree& t2){if(t1.GetRoot()!= t2.GetRoot()){RE false;}if(t1.IsLeaf()){RE t2.IsLeaf();}if(t2.IsLeaf()){RE false;}auto itr1 = t1.LeftMostNode();auto itr2 = t2.LeftMostNode();WH(itr1.IsValid()&& itr2.IsValid()){if(t1.GetBranchCopy(*itr1)!= t2.GetBranchCopy(*itr2)){RE false;}itr1++;itr2++;}RE !(itr1.IsValid()|| itr2.IsValid());}TE IN bool OP!=(CO VLTree& t1,CO VLTree& t2){RE !(t1 == t2);} TE CL EntryOfLinkedVE{PU:T m_t;uint m_prev_entry;uint m_next_entry;IN EntryOfLinkedVE();IN EntryOfLinkedVE(CRUI prev_entry,CRUI next_entry);IN EntryOfLinkedVE(EntryOfLinkedVE&& e);}; TE IN EntryOfLinkedVE::EntryOfLinkedVE(): m_t(),m_prev_entry(0),m_next_entry(0){}TE IN EntryOfLinkedVE::EntryOfLinkedVE(CRUI prev_entry,CRUI next_entry): m_t(),m_prev_entry(prev_entry),m_next_entry(next_entry){}TE IN EntryOfLinkedVE::EntryOfLinkedVE(EntryOfLinkedVE&& e): m_t(MO(e.m_t)),m_prev_entry(MO(e.m_prev_entry)),m_next_entry(MO(e.m_next_entry)){} TE CL LinkedVE; TE CL IteratorOfLinkedVE{PU:LinkedVE* m_p;uint m_i;IN IteratorOfLinkedVE(LinkedVE* CO& p,CRUI i)NE;IN IteratorOfLinkedVE(CO IteratorOfLinkedVE& itr)NE;IN T& OP*()CO;IN T* OP->()CO;IteratorOfLinkedVE& OP=(CO IteratorOfLinkedVE& itr)NE;IN VO OP++(int);IN VO OP--(int);IN CO LinkedVE& GetLinkedVE()CO NE;IN LinkedVE& RefLinkedVE()NE;IN CRUI GetIndex()CO NE;IN CRUI RefIndex()NE;}; TE CL COIteratorOfLinkedVE{PU:CO LinkedVE* m_p;uint m_i;IN COIteratorOfLinkedVE(CO LinkedVE* CO& p,CRUI i)NE;IN COIteratorOfLinkedVE(CO COIteratorOfLinkedVE& itr)NE;IN COIteratorOfLinkedVE(CO IteratorOfLinkedVE& itr)NE;IN CO T& OP*()CO;IN CO T* OP->()CO;COIteratorOfLinkedVE& OP=(CO COIteratorOfLinkedVE& itr)NE;COIteratorOfLinkedVE& OP=(CO IteratorOfLinkedVE& itr)NE;IN VO OP++(int);IN VO OP--(int);IN CO LinkedVE& GetLinkedVE()CO NE;IN CRUI GetIndex()CO NE;IN CRUI RefIndex()NE;ST IN bool Equal(CO IteratorOfLinkedVE&,CO IteratorOfLinkedVE&)NE;ST IN bool Equal(CO COIteratorOfLinkedVE&,CO IteratorOfLinkedVE&)NE;ST IN bool Equal(CO IteratorOfLinkedVE&,CO COIteratorOfLinkedVE&)NE;ST IN bool Equal(CO COIteratorOfLinkedVE&,CO COIteratorOfLinkedVE&)NE;}; TE IN IteratorOfLinkedVE::IteratorOfLinkedVE(LinkedVE* CO& p,CRUI i)NE:m_p(p),m_i(i){}TE IN IteratorOfLinkedVE::IteratorOfLinkedVE(CO IteratorOfLinkedVE& itr)NE:m_p(itr.m_p),m_i(itr.m_i){}TE IN T& IteratorOfLinkedVE::OP*()CO{RE(*m_p)[m_i];}TE IN T* IteratorOfLinkedVE::OP->()CO{RE &((*m_p)[m_i]);}TE IN IteratorOfLinkedVE& IteratorOfLinkedVE::OP=(CO IteratorOfLinkedVE& itr)NE{m_p = itr.m_p;m_i = itr.m_i;RE *TH;}TE IN VO IteratorOfLinkedVE::OP++(int){m_i = m_p->m_entry[m_i].m_next_entry;}TE IN VO IteratorOfLinkedVE::OP--(int){m_i = m_p->m_entry[m_i].m_prev_entry;}TE IN CO LinkedVE& IteratorOfLinkedVE::GetLinkedVE()CO NE{RE *m_p;}TE IN LinkedVE& IteratorOfLinkedVE::RefLinkedVE()NE{RE *m_p;}TE IN CRUI IteratorOfLinkedVE::GetIndex()CO NE{RE m_i;}TE IN CRUI IteratorOfLinkedVE::RefIndex()NE{RE m_i;}TE IN COIteratorOfLinkedVE::COIteratorOfLinkedVE(CO LinkedVE* CO& p,CRUI i)NE:m_p(p),m_i(i){}TE IN COIteratorOfLinkedVE::COIteratorOfLinkedVE(CO COIteratorOfLinkedVE& itr)NE:m_p(itr.m_p),m_i(itr.m_i){}TE IN COIteratorOfLinkedVE::COIteratorOfLinkedVE(CO IteratorOfLinkedVE& itr)NE:m_p(itr.m_p),m_i(itr.m_i){}TE IN CO T& COIteratorOfLinkedVE::OP*()CO{RE(*m_p)[m_i];}TE IN CO T* COIteratorOfLinkedVE::OP->()CO{RE &((*m_p)[m_i]);}TE COIteratorOfLinkedVE& COIteratorOfLinkedVE::OP=(CO COIteratorOfLinkedVE& itr)NE{m_p = itr.m_p;m_i = itr.m_i;RE *TH;}TE COIteratorOfLinkedVE& COIteratorOfLinkedVE::OP=(CO IteratorOfLinkedVE& itr)NE{m_p = itr.m_p;m_i = itr.m_i;RE *TH;}TE IN VO COIteratorOfLinkedVE::OP++(int){m_i = m_p->m_entry[m_i].m_next_entry;}TE IN VO COIteratorOfLinkedVE::OP--(int){m_i = m_p->m_entry[m_i].m_prev_entry;}TE IN CO LinkedVE& COIteratorOfLinkedVE::GetLinkedVE()CO NE{RE *m_p;}TE IN CRUI COIteratorOfLinkedVE::GetIndex()CO NE{RE m_i;}TE IN CRUI COIteratorOfLinkedVE::RefIndex()NE{RE m_i;}TE IN bool COIteratorOfLinkedVE::Equal(CO IteratorOfLinkedVE& itr0,CO IteratorOfLinkedVE& itr1)NE{RE itr0.m_p == itr1.m_p && itr0.m_i == itr1.m_i;}TE IN bool COIteratorOfLinkedVE::Equal(CO COIteratorOfLinkedVE& itr0,CO IteratorOfLinkedVE& itr1)NE{RE itr0.m_p == itr1.m_p && itr0.m_i == itr1.m_i;}TE IN bool COIteratorOfLinkedVE::Equal(CO IteratorOfLinkedVE& itr0,CO COIteratorOfLinkedVE& itr1)NE{RE itr0.m_p == itr1.m_p && itr0.m_i == itr1.m_i;}TE IN bool COIteratorOfLinkedVE::Equal(CO COIteratorOfLinkedVE& itr0,CO COIteratorOfLinkedVE& itr1)NE{RE itr0.m_p == itr1.m_p && itr0.m_i == itr1.m_i;}TE IN bool OP==(CO IteratorOfLinkedVE& itr0,CO IteratorOfLinkedVE& itr1)NE{RE COIteratorOfLinkedVE::Equal(itr0,itr1);}TE IN bool OP!=(CO IteratorOfLinkedVE& itr0,CO IteratorOfLinkedVE& itr1)NE{RE !(itr0 == itr1);}TE IN bool OP==(CO COIteratorOfLinkedVE& itr0,CO IteratorOfLinkedVE& itr1)NE{RE COIteratorOfLinkedVE::Equal(itr0,itr1);}TE IN bool OP!=(CO COIteratorOfLinkedVE& itr0,CO IteratorOfLinkedVE& itr1)NE{RE !(itr0 == itr1);}TE IN bool OP==(CO IteratorOfLinkedVE& itr0,CO COIteratorOfLinkedVE& itr1)NE{RE COIteratorOfLinkedVE::Equal(itr0,itr1);}TE IN bool OP!=(CO IteratorOfLinkedVE& itr0,CO COIteratorOfLinkedVE& itr1)NE{RE !(itr0 == itr1);}TE IN bool OP==(CO COIteratorOfLinkedVE& itr0,CO COIteratorOfLinkedVE& itr1)NE{RE COIteratorOfLinkedVE::Equal(itr0,itr1);}TE IN bool OP!=(CO COIteratorOfLinkedVE& itr0,CO COIteratorOfLinkedVE& itr1)NE{RE !(itr0 == itr1);} TE CL LinkedVE{PU:VE > m_entry;uint m_front_linked_entry;uint m_back_linked_entry;uint m_SZ_of_VE;uint m_SZ_of_link;IN LinkedVE();IN LinkedVE(CRUI max_SZ);IN CO T& OP[](CRUI i)CO;IN T& OP[](CRUI i);uint GetLinkedEntry(CRUI i)CO;IN CRUI GetFrontLinkedEntryIndex()CO NE;IN CRUI GetBackLinkedEntryIndex()CO NE;IN CRUI GetSZOfVE()CO NE;IN CRUI GetSZOfLink()CO NE;IN bool EmptyVE()CO NE;IN bool EmptyLink()CO NE;IN VO push_back();TE VO push_back(CO U& u);TE IN VO push_back(CO U& u,CO ARGS&... args);IN VO SetPreviousLink(CRUI i,CRUI j);IN VO SetNexttLink(CRUI i,CRUI j);IN CRUI GetPreviousLinkIndex(CRUI i)CO;IN CRUI GetNexttLinkIndex(CRUI i)CO;CRUI DeLink(CRUI i);VO ReLink(CRUI i);US iterator = IteratorOfLinkedVE;US CO_iterator = COIteratorOfLinkedVE;IN iterator GetIterator(CRUI i)NE;IN CO_iterator GetIterator(CRUI i)CO NE;IN iterator BE()NE;IN CO_iterator BE()CO NE;IN iterator EN()NE;IN CO_iterator EN()CO NE;iterator erase(iterator& itr);IN EntryOfLinkedVE& push_back_Body_0();IN VO push_back_Body_1(EntryOfLinkedVE& e);}; TE IN LinkedVE::LinkedVE(): m_entry(1),m_front_linked_entry(0),m_back_linked_entry(0),m_SZ_of_VE(0),m_SZ_of_link(0){}TE IN LinkedVE::LinkedVE(CRUI max_SZ): m_entry(),m_front_linked_entry(0),m_back_linked_entry(0),m_SZ_of_VE(0),m_SZ_of_link(0){m_entry.reserve(max_SZ + 1);m_entry.push_back(EntryOfLinkedVE());}TE IN CO T& LinkedVE::OP[](CRUI i)CO{RE m_entry[i].m_t;}TE IN T& LinkedVE::OP[](CRUI i){RE m_entry[i].m_t;}TE uint LinkedVE::GetLinkedEntry(CRUI i)CO{uint linked_entry = m_front_linked_entry;for(uint j = 0;j < i;j++){linked_entry = m_entry[linked_entry].m_next_entry;}RE linked_entry;}TE IN CRUI LinkedVE::GetFrontLinkedEntryIndex()CO NE{RE m_front_linked_entry;}TE IN CRUI LinkedVE::GetBackLinkedEntryIndex()CO NE{RE m_back_linked_entry;}TE IN CRUI LinkedVE::GetSZOfVE()CO NE{RE m_SZ_of_VE;}TE IN CRUI LinkedVE::GetSZOfLink()CO NE{RE m_SZ_of_link;}TE IN bool LinkedVE::EmptyVE()CO NE{RE m_SZ_of_VE == 0;}TE IN bool LinkedVE::EmptyLink()CO NE{RE m_SZ_of_link == 0;}TE IN VO LinkedVE::push_back(){}TE TE VO LinkedVE::push_back(CO U& u){EntryOfLinkedVE& e = push_back_Body_0();e.m_t = u;push_back_Body_1(e);RE;}TE TE IN VO LinkedVE::push_back(CO U& u,CO ARGS&... args){push_back(u);push_back(args...);}TE IN EntryOfLinkedVE& LinkedVE::push_back_Body_0(){m_entry.push_back(EntryOfLinkedVE(m_SZ_of_VE,m_front_linked_entry));RE m_entry[m_SZ_of_VE];}TE IN VO LinkedVE::push_back_Body_1(EntryOfLinkedVE& e){e.m_next_entry = m_SZ_of_VE + 1;m_entry[m_front_linked_entry].m_prev_entry = m_SZ_of_VE + 1;m_back_linked_entry = m_SZ_of_VE;m_SZ_of_VE++;m_SZ_of_link++;}TE IN VO LinkedVE::SetPreviousLink(CRUI i,CRUI j){m_entry[i].m_prev_entry = j;}TE IN VO LinkedVE::SetNexttLink(CRUI i,CRUI j){m_entry[i].m_next_entry = j;}TE IN CRUI LinkedVE::GetPreviousLinkIndex(CRUI i)CO{RE m_entry[i].m_prev_entry;}TE IN CRUI LinkedVE::GetNexttLinkIndex(CRUI i)CO{RE m_entry[i].m_next_entry;}TE CRUI LinkedVE::DeLink(CRUI i){CO EntryOfLinkedVE& e = m_entry[i];m_entry[e.m_prev_entry].m_next_entry = e.m_next_entry;m_entry[e.m_next_entry].m_prev_entry = e.m_prev_entry;if(m_front_linked_entry == i){m_front_linked_entry = e.m_next_entry;}if(m_back_linked_entry == i){m_back_linked_entry = e.m_prev_entry;}m_SZ_of_link--;RE e.m_next_entry;}TE VO LinkedVE::ReLink(CRUI i){EntryOfLinkedVE& current_entry = m_entry[i];if(m_SZ_of_link == 0){EntryOfLinkedVE& EN_entry = m_entry[m_SZ_of_VE];EN_entry.m_prev_entry = EN_entry.m_next_entry = i;current_entry.m_prev_entry = current_entry.m_next_entry = m_SZ_of_VE;m_front_linked_entry = m_back_linked_entry = i;}else{uint prev;if(m_front_linked_entry > i){m_front_linked_entry = i;prev = m_SZ_of_VE;}else{prev = m_front_linked_entry;}if(m_back_linked_entry < i){m_back_linked_entry = i;}prev = m_entry[prev].m_next_entry;WH(prev < i){prev = m_entry[prev].m_next_entry;}CO uint next = prev;EntryOfLinkedVE& next_entry = m_entry[next];prev = next_entry.m_prev_entry;EntryOfLinkedVE& prev_entry = m_entry[prev];prev_entry.m_next_entry = i;current_entry.m_prev_entry = prev;current_entry.m_next_entry = next;next_entry.m_prev_entry = i;}m_SZ_of_link++;RE;}TE IN TY LinkedVE::iterator LinkedVE::GetIterator(CRUI i)NE{RE TY LinkedVE::iterator(TH,i);}TE IN TY LinkedVE::CO_iterator LinkedVE::GetIterator(CRUI i)CO NE{RE TY LinkedVE::CO_iterator(TH,i);}TE IN TY LinkedVE::iterator LinkedVE::BE()NE{RE TY LinkedVE::iterator(TH,m_front_linked_entry);}TE IN TY LinkedVE::CO_iterator LinkedVE::BE()CO NE{RE TY LinkedVE::CO_iterator(TH,m_front_linked_entry);}TE IN TY LinkedVE::iterator LinkedVE::EN()NE{RE TY LinkedVE::iterator(TH,m_SZ_of_VE);}TE IN TY LinkedVE::CO_iterator LinkedVE::EN()CO NE{RE TY LinkedVE::CO_iterator(TH,m_SZ_of_VE);}TE TY LinkedVE::iterator LinkedVE::erase(TY LinkedVE::iterator& itr){RE TY LinkedVE::iterator(TH,DeLink(itr.m_i));} TE CL EntryOfUnionFindForest{PU:VLSubTree m_node;uint m_pred_node;uint m_root;uint m_depth;IN EntryOfUnionFindForest();IN EntryOfUnionFindForest(CO T& t,CRUI num);IN EntryOfUnionFindForest(EntryOfUnionFindForest&& e);};TE IN EntryOfUnionFindForest::EntryOfUnionFindForest(): m_node(),m_pred_node(0),m_root(0),m_depth(0){}TE IN EntryOfUnionFindForest::EntryOfUnionFindForest(CO T& t,CRUI num): m_node(),m_pred_node(num),m_root(num),m_depth(0){m_node.SetRoot(t);}TE IN EntryOfUnionFindForest::EntryOfUnionFindForest(EntryOfUnionFindForest&& e): m_node(),m_pred_node(MO(e.m_pred_node)),m_root(MO(e.m_root)),m_depth(MO(e.m_depth)){m_node.SetRoot(MO(e.m_node.m_p_root->m_t));} TE CL UnionFindForest :public LinkedVE >{PU:IN UnionFindForest(CRUI max_SZ);IN UnionFindForest(CRUI max_SZ,CRUI SZ);IN CO VLSubTree& GetSubTree(CRUI num)CO;IN CRUI GetPredecessorNode(CRUI num)CO;CRUI GetRootOfNode(CRUI num);uint GetRoot(CRUI num)CO;IN CO T& OP[](CRUI num)CO;IN T& OP[](CRUI num);IN CRUI GetSZOfNode()CO NE;IN CRUI GetSZOfRoot()CO NE;IN VO push_RightMost();VO push_RightMost(CO T& t);TE IN VO push_RightMost(CO T& t,CO ARGS&... args);IN VO push_back()= delete;TE VO push_back(CO U& u)= delete;TE IN VO push_back(CO U& u,CO ARGS&... args)= delete;IN VO SetPreviousLink(CRUI i,CRUI j)= delete;IN VO SetNexttLink(CRUI i,CRUI j)= delete;IN CRUI GetPreviousLinkIndex(CRUI i)CO = delete;IN CRUI GetNexttLinkIndex(CRUI i)CO = delete;CRUI DeLink(CRUI i)= delete;VO ReLink(CRUI i)= delete;VO Graft(CRUI num0,CRUI num1);}; TE IN UnionFindForest::UnionFindForest(CRUI max_SZ): LinkedVE >(max_SZ){}TE IN UnionFindForest::UnionFindForest(CRUI max_SZ,CRUI SZ): UnionFindForest(max_SZ){T t{};for(int i=0;i IN CO VLSubTree& UnionFindForest::GetSubTree(CRUI num)CO{RE LinkedVE >::OP[](num).m_node;}TE IN CRUI UnionFindForest::GetPredecessorNode(CRUI num)CO{RE LinkedVE >::OP[](num).m_pred_node;}TE CRUI UnionFindForest::GetRootOfNode(CRUI num){uint& root = LinkedVE >::OP[](num).m_root;if(root != LinkedVE >::OP[](root).m_root){root = GetRootOfNode(root);}RE root;}TE uint UnionFindForest::GetRoot(CRUI num)CO{auto itr = LinkedVE >::BE();for(uint i = 0;i < num;i++){itr++;}RE itr.GetIndex();}TE IN CO T& UnionFindForest::OP[](CRUI num)CO{RE LinkedVE >::OP[](num).m_node.GetRoot();}TE IN T& UnionFindForest::OP[](CRUI num){RE LinkedVE >::OP[](num).m_node.RefRoot();}TE IN CRUI UnionFindForest::GetSZOfNode()CO NE{RE LinkedVE >::GetSZOfVE();}TE IN CRUI UnionFindForest::GetSZOfRoot()CO NE{RE LinkedVE >::GetSZOfLink();}TE IN VO UnionFindForest::push_RightMost(){}TE VO UnionFindForest::push_RightMost(CO T& t){EntryOfLinkedVE >& e = LinkedVE >::push_back_Body_0();e.m_t.m_node.SetRoot(t);e.m_t.m_pred_node = e.m_t.m_root = LinkedVE >::m_SZ_of_VE;LinkedVE >::push_back_Body_1(e);RE;}TE TE IN VO UnionFindForest::push_RightMost(CO T& t,CO ARGS&... args){push_RightMost(t);push_RightMost(args...);}TE VO UnionFindForest::Graft(CRUI num0,CRUI num1){CRUI e0_root_index = GetRootOfNode(num0);CRUI e1_root_index = GetRootOfNode(num1);if(e0_root_index == e1_root_index){RE;}EntryOfUnionFindForest& e0_root = LinkedVE >::OP[](e0_root_index);EntryOfUnionFindForest& e1_root = LinkedVE >::OP[](e1_root_index);CO uint i0 =(e0_root.m_depth < e1_root.m_depth?0:1);CO uint i1 = 1 - i0;EntryOfUnionFindForest* CO p_e_root[2] ={&e0_root,&e1_root};EntryOfUnionFindForest& root_0 = *(p_e_root[i0]);EntryOfUnionFindForest& root_1 = *(p_e_root[i1]);if(root_0.m_depth == root_1.m_depth){root_1.m_depth++;}root_1.m_node.Graft(root_0.m_node);LinkedVE >::DeLink(root_0.m_root);root_0.m_root = root_0.m_pred_node = root_1.m_root;RE;} // AAA 常設でないライブラリは以上に挿入する。 #define INCLUDE_SUB #include __FILE__ #else // INCLUDE_LIBRARY // #define REACTIVE // #define USE_GETLINE #ifdef DEBUG #define _GLIBCXX_DEBUG #define REPEAT_MAIN( BOUND ) START_MAIN; signal( SIGABRT , &AlertAbort ); AutoCheck( exec_mode , use_getline ); if( exec_mode == sample_debug_mode || exec_mode == submission_debug_mode || exec_mode == library_search_mode ){ return 0; } else if( exec_mode == experiment_mode ){ Experiment(); return 0; } else if( exec_mode == small_test_mode ){ SmallTest(); return 0; }; DEXPR( int , bound_test_case_num , BOUND , min( BOUND , 100 ) ); int test_case_num = 1; if( exec_mode == solve_mode ){ if constexpr( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } } else if( exec_mode == random_test_mode ){ CERR( "ランダムテストを行う回数を指定してください。" ); SET_LL( test_case_num ); } FINISH_MAIN #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE ) #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) if( exec_mode == solve_mode ){ SET_LL( A ); ASSERT( A , MIN , MAX ); } else if( exec_mode == random_test_mode ){ CERR( #A , " = " , ( A = GetRand( MIN , MAX ) ) ); } else { assert( false ); } #define SOLVE_ONLY static_assert( __FUNCTION__[0] == 'S' ) #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl #define COUT( ... ) VariadicCout( cout << "出力: " , __VA_ARGS__ ) << endl #define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endl #define COUT_A( A , N ) cout << "出力: "; OUTPUT_ARRAY( cout , A , N ) << endl #define CERR_ITR( A ) OUTPUT_ITR( cerr , A ) << endl #define COUT_ITR( A ) cout << "出力: "; OUTPUT_ITR( cout , A ) << endl #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define REPEAT_MAIN( BOUND ) START_MAIN; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } FINISH_MAIN #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX ) #define SOLVE_ONLY #define CERR( ... ) #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL #define CERR_A( A , N ) #define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << ENDL #define CERR_ITR( A ) #define COUT_ITR( A ) OUTPUT_ITR( cout , A ) << ENDL #endif #ifdef REACTIVE #define ENDL endl #else #define ENDL "\n" #endif #ifdef USE_GETLINE #define SET_LL( A ) { GETLINE( A ## _str ); A = stoll( A_str ); } #define GETLINE_SEPARATE( SEPARATOR , ... ) SOLVE_ONLY; string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) SOLVE_ONLY; GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #else #define SET_LL( A ) cin >> A #define CIN( LL , ... ) SOLVE_ONLY; LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ ) #define SET_A( A , N ) SOLVE_ONLY; FOR( VARIABLE_FOR_CIN_A , 0 , N ){ cin >> A[VARIABLE_FOR_CIN_A]; } #define CIN_A( LL , A , N ) vector A( N ); SET_A( A , N ); #endif #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using lld = __float128; template using T2 = pair; template using T3 = tuple; template using T4 = tuple; using path = pair; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define START_MAIN int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define FINISH_MAIN REPEAT( test_case_num ){ if constexpr( bound_test_case_num > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_test_case_num , ":" ); } Solve(); CERR( "" ); } } #define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now() #define CURRENT_TIME static_cast( chrono::duration_cast( chrono::system_clock::now() - watch ).count() / 1000.0 ) #define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 ) #define TYPE_OF( VAR ) decay_t #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN_ASSERT( A , MIN , MAX ) TYPE_OF( MAX ) A; SET_ASSERT( A , MIN , MAX ) #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 OUTPUT_ARRAY( OS , A , N ) FOR( VARIABLE_FOR_OUTPUT_ARRAY , 0 , N ){ OS << A[VARIABLE_FOR_OUTPUT_ARRAY] << (VARIABLE_FOR_OUTPUT_ARRAY==N-1?"":" "); } OS #define OUTPUT_ITR( OS , A ) { auto ITERATOR_FOR_OUTPUT_ITR = A.begin() , END_FOR_OUTPUT_ITR = A.end(); bool VARIABLE_FOR_OUTPUT_ITR = ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR; while( VARIABLE_FOR_OUTPUT_ITR ){ OS << *ITERATOR_FOR_COUT_ITR; ( VARIABLE_FOR_OUTPUT_ITR = ++ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR ) ? OS : OS << " "; } } OS #define RETURN( ... ) SOLVE_ONLY; COUT( __VA_ARGS__ ); return #define COMPARE( ... ) auto naive = Naive( __VA_ARGS__ ); auto answer = Answer( __VA_ARGS__ ); bool match = naive == answer; COUT( #__VA_ARGS__ , ":" , naive , match ? "==" : "!=" , answer ); if( !match ){ return; } // 入出力用 template inline basic_istream& VariadicCin( basic_istream& is ) { return is; } template inline basic_istream& VariadicCin( basic_istream& is , Arg& arg , ARGS&... args ) { return VariadicCin( is >> arg , args... ); } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator ) { return is; } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator , Arg& arg , ARGS&... args ) { return VariadicGetline( getline( is , arg , separator ) , separator , args... ); } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg& arg ) { return os << arg; } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg1& arg1 , const Arg2& arg2 , const ARGS&... args ) { return VariadicCout( os << arg1 << " " , arg2 , args... ); } // グリッド問題用 int H , W , H_minus , W_minus , HW; vector > non_wall; inline T2 EnumHW( const int& v ) { return { v / W , v % W }; } inline int EnumHW_inv( const int& h , const int& w ) { return h * W + w; } const string direction[4] = {"U","R","D","L"}; // (i,j)->(k,h)の方向番号を取得 inline int DirectionNumberOnGrid( const int& i , const int& j , const int& k , const int& h ){return ik?0:jh?3:(assert(false),-1);} // v->wの方向番号を取得 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);} // 方向番号の反転U<->D、R<->L inline int ReverseDirectionNumberOnGrid( const int& n ){assert(0<=n&&n<4);return(n+2)%4;} inline void SetEdgeOnGrid( const string& Si , const int& i , list ( &e )[] , 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+10){e[EnumHW_inv(i,j-1)].push_back(v);}if(j+1 ( &e )[] , const char& walkable = '.' ){FOR(j,0,W){if(Si[j]==walkable){const int v=EnumHW_inv(i,j);if(i>0){e[EnumHW_inv(i-1,j)].push_back({v,1});}if(i+10){e[EnumHW_inv(i,j-1)].push_back({v,1});}if(j+1 >& non_wall , const char& walkable = '.' , const char& unwalkable = '#' ){non_wall.push_back(vector(W));auto& non_wall_i=non_wall[i];FOR(j,0,W){non_wall_i[j]=Si[j]==walkable?true:(assert(Si[j]==unwalkable),false);}} // グラフ用 template vector > e; template map f; template vector g; // デバッグ用 #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void AutoCheck( int& exec_mode , const bool& use_getline ); inline void Solve(); inline void Experiment(); inline void SmallTest(); inline void RandomTest(); ll GetRand( const ll& Rand_min , const ll& Rand_max ); int exec_mode; CEXPR( int , solve_mode , 0 ); CEXPR( int , sample_debug_mode , 1 ); CEXPR( int , submission_debug_mode , 2 ); CEXPR( int , library_search_mode , 3 ); CEXPR( int , experiment_mode , 4 ); CEXPR( int , small_test_mode , 5 ); CEXPR( int , random_test_mode , 6 ); #ifdef USE_GETLINE CEXPR( bool , use_getline , true ); #else CEXPR( bool , use_getline , false ); #endif #endif // 圧縮用 #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& // VVV 常設ライブラリは以下に挿入する。 // AAA 常設ライブラリは以上に挿入する。 #define INCLUDE_LIBRARY #include __FILE__ #endif // INCLUDE_LIBRARY #endif // INCLUDE_SUB #endif // INCLUDE_MAIN