結果
問題 | No.3034 コーエン-マコーレー抽象単体複体 |
ユーザー |
👑 |
提出日時 | 2025-01-24 21:09:55 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 229 ms / 2,000 ms |
コード長 | 45,673 bytes |
コンパイル時間 | 18,038 ms |
コンパイル使用メモリ | 320,160 KB |
実行使用メモリ | 20,352 KB |
最終ジャッジ日時 | 2025-02-06 22:27:42 |
合計ジャッジ時間 | 18,326 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 36 |
ソースコード
// 入力制約/フォーマットチェック#ifndef INCLUDE_MODE#define INCLUDE_MODE// #define REACTIVE#define USE_GETLINE#endif#ifdef INCLUDE_MAINvoid Solve(){// N,Mの入力受け取りCEXPR( int , bound , 1e3 );GETLINE_COUNT( NM_str , 2 , ' ' );STOI( NM_str , N , 3 , bound );STOI( NM_str , M , 1 , bound );// {0,...,N-1}の要素がDeltaに現れたか否かを管理vector<bool> appeared( N );// Deltaの頂点数int vertex = 0;// C_1->C_0の表現行列の転置の行基本変形と、その階数と、// そのi-1列目まで0でi列目が1である行の行番号// (何となく列基本変形より行基本変形で処理したかったので転置した)vector<bitset<bound>> t_f10{};int rank10 = 0;vector<int> row10( N , -1 );// C_2->C_1の表現行列の行基本変形と、その階数と、// そのi-1列目まで0でi列目が1である行の行番号CEXPR( int , bound3 , bound * 3 );vector<bitset<bound3>> f21( M );int rank21 = 0;vector<int> row21( M * 3 , -1 );// {0,...,N-1}や{0,...,N-1}-{i}の要素にDeltaの辺を貼ったグラフG,G_iの連結成分管理UnionFindForest uff_total{ N };vector uff_each( N , uff_total );// 辺番号の管理(C_1の基底の管理)vector edge_num( N , vector( N , -1 ) );// 無向辺を2本の有向辺として管理vector e( N , vector<int>() );// M回クエリ処理FOR( m , 0 , M ){// ABCの入力受け取り&0-indexed化GETLINE_COUNT( ABC_str , 3 , ' ' );STOI_A( ABC_str , 0 , 3 , ABC , 1 , N ); --ABC;assert( ABC[0] < ABC[1] && ABC[1] < ABC[2] );// i=A,B,Cの順に処理RUN( ABC , i ){// Deltaの頂点情報を更新if( !appeared[i] ){appeared[i] = true;vertex++;}}// E=AB,BC,ACの順に処理// 辺(Eの左成分)->(Eの右成分)の追加を行う。FOR( r , 0 , 3 ){auto& L = ABC[r==1?1:0];auto& R = ABC[r==0?1:2];auto& num = edge_num[L][R];// 辺L->Rが初登場の場合の分岐if( num == -1 ){// 辺番号と有向辺の設定num = t_f10.size();e[L].push_back( R );e[R].push_back( L );// 辺L->RがC_1の基底に追加されるので、C_1->C_0の表現行列の転置に対応する行を末尾に追加t_f10.push_back( bitset<bound>() );t_f10[num][L] = t_f10[num][R] = 1;// C_1->C_0の表現行列の転置に追加した行に掃き出し法を継続FOR( j , L , N ){if( t_f10[num][j] == 1 ){// j-1行目まで0でj行目が1の行がまだ作れていない場合の分岐if( row10[j] == -1 ){rank10++;row10[j] = num;break;} else {t_f10[num] ^= t_f10[row10[j]];}}}// 辺L->RをGに追加uff_total.Graft( L , R );FOR( i , 0 , N ){if( i != L && i != R ){// 辺L->RをG_iに追加uff_each[i].Graft( L , R );}}}// C_2->C_1の表現行列m行目に辺L->Rを反映f21[m][num] = 1;}// C_2->C_1の表現行列のm行目に掃き出し法を継続FOR( j , 0 , bound3 ){if( f21[m][j] == 1 ){// j-1行目まで0でj行目が1の行がまだ作れていない場合の分岐if( row21[j] == -1 ){rank21++;row21[j] = m;break;} else {f21[m] ^= f21[row21[j]];}}}// {1,...,N}にDeltaの辺を貼ったグラフの連結成分数を用いてDeltaの連結性判定if( uff_total.RootSize() - ( N - vertex ) != 1 ){COUT( "CO" );// kerとimの次元比較でH_1の消滅判定} else if( int( t_f10.size() ) - rank10 > rank21 ){COUT( "H1" );} else {bool LK = true;FOR( i , 0 , N ){if( appeared[i] ){// G_iを用いてlink_Delta{i}の連結性判定int size = e[i].size();int root = uff_each[i].RootOfNode( e[i][0] );FOR( j , 1 , size ){LK &= uff_each[i].RootOfNode( e[i][j] ) == root;}}}COUT( LK ? "CM" : "LK" );}}}REPEAT_MAIN(1);#else // INCLUDE_MAIN#ifdef INCLUDE_LIBRARY// https://github.com/p-adic/cpp// VVV ライブラリは以下に挿入する。redefinitionを避けるため圧縮元はincludeしない。/* 圧縮用 */#define TE template#define TY typename#define US using#define ST static#define AS assert#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 LE length#define PW Power#define MO move#define TH this#define CRI CO int&#define CRUI CO uint&#define CRL CO ll&#define VI virtual#define IS basic_istream<char,Traits>#define OS basic_ostream<char,Traits>#define ST_AS static_assert#define reMO_CO remove_const#define is_COructible_v is_constructible_v#define rBE rbegin#define DC_OF_CPOINT(POINT)IN CO U& POINT()CO NE#define DC_OF_POINT(POINT)IN U& POINT()NE#define DF_OF_CPOINT(POINT)TE <TY U> IN CO U& VirtualPointedSet<U>::POINT()CO NE{RE Point();}#define DF_OF_POINT(POINT)TE <TY U> IN U& VirtualPointedSet<U>::POINT()NE{RE Point();}TE <TY U>CL UnderlyingSet{PU:US type = U;};TE <TY U>CL VirtualPointedSet:VI PU UnderlyingSet<U>{PU:VI CO U& Point()CO NE = 0;VI U& Point()NE = 0;DC_OF_CPOINT(Unit);DC_OF_CPOINT(Zero);DC_OF_CPOINT(One);DC_OF_CPOINT(Infty);DC_OF_POINT(init);DC_OF_POINT(root);};TE <TY U>CL PointedSet:VI PUVirtualPointedSet<U>{PU:U m_b_U;IN PointedSet(U b_u = U());IN CO U& Point()CO NE;IN U& Point()NE;};TE <TY U>CL VirtualNSet:VI PU UnderlyingSet<U>{PU:VI U Transfer(CO U& u)= 0;IN U Inverse(CO U& u);};TE <TY U,TY F_U>CL AbstractNSet:VI PU VirtualNSet<U>{PU:F_U m_f_U;IN AbstractNSet(F_U f_U);IN AbstractNSet<U,F_U>& OP=(CO AbstractNSet&)NE;IN U Transfer(CO U& u);};TE <TY U>CL VirtualMagma:VI PU UnderlyingSet<U>{PU:VI U Product(U u0,CO U& u1)= 0;IN U Sum(U u0,CO U& u1);};TE <TY U = ll>CL AdditiveMagma:VI PU VirtualMagma<U>{PU:IN U Product(U u0,CO U& u1);};TE <TY U = ll>CLMultiplicativeMagma:VI PU VirtualMagma<U>{PU:IN U Product(U u0,CO U& u1);};TE <TY U,TY M_U>CL AbstractMagma:VI PU VirtualMagma<U>{PU:M_U m_m_U;INAbstractMagma(M_U m_U);IN AbstractMagma<U,M_U>& OP=(CO AbstractMagma<U,M_U>&)NE;IN U Product(U u0,CO U& u1);};TE <TY U> IN PointedSet<U>::PointedSet(U b_U):m_b_U(MO(b_U)){}TE <TY U> IN CO U& PointedSet<U>::Point()CO NE{RE m_b_U;}TE <TY U> IN U& PointedSet<U>::Point()NE{RE m_b_U;}DF_OF_CPOINT(Unit);DF_OF_CPOINT(Zero);DF_OF_CPOINT(One);DF_OF_CPOINT(Infty);DF_OF_POINT(init);DF_OF_POINT(root);TE <TY U,TY F_U> IN AbstractNSet<U,F_U>::AbstractNSet(F_U f_U):m_f_U(MO(f_U)){ST_AS(is_invocable_r_v<U,F_U,U>);}TE <TY U,TY F_U> IN AbstractNSet<U,F_U>&AbstractNSet<U,F_U>::operator=(CO AbstractNSet<U,F_U>&)NE{RE *TH;}TE <TY U,TY F_U> IN U AbstractNSet<U,F_U>::Transfer(CO U& u){RE m_f_U(u);}TE<TY U> IN U VirtualNSet<U>::Inverse(CO U& u){RE Transfer(u);}TE <TY U,TY M_U> IN AbstractMagma<U,M_U>::AbstractMagma(M_U m_U):m_m_U(MO(m_U)){ST_AS(is_invocable_r_v<U,M_U,U,U>);}TE <TY U,TY M_U> IN AbstractMagma<U,M_U>& AbstractMagma<U,M_U>::OP=(CO AbstractMagma<U,M_U>&)NE{RE *TH;}TE<TY U> IN U AdditiveMagma<U>::Product(U u0,CO U& u1){RE MO(u0 += u1);}TE <TY U> IN U MultiplicativeMagma<U>::Product(U u0,CO U& u1){RE MO(u0 *=u1);}TE <TY U,TY M_U> IN U AbstractMagma<U,M_U>::Product(U u0,CO U& u1){RE m_m_U(MO(u0),u1);}TE <TY U> IN U VirtualMagma<U>::Sum(U u0,CO U& u1){RE Product(MO(u0),u1);}TE <TY U>CL VirtualMonoid:VI PU VirtualMagma<U>,VI PU VirtualPointedSet<U>{};TE <TY U = ll>CL AdditiveMonoid:VI PU VirtualMonoid<U>,PU AdditiveMagma<U>,PU PointedSet<U>{};TE <TY U = ll>CL MultiplicativeMonoid:VI PU VirtualMonoid<U>,PU MultiplicativeMagma<U>,PU PointedSet<U>{PU:INMultiplicativeMonoid(U e_U);};TE <TY U,TY M_U>CL AbstractMonoid:VI PU VirtualMonoid<U>,PU AbstractMagma<U,M_U>,PU PointedSet<U>{PU:INAbstractMonoid(M_U m_U,U e_U);};TE <TY U> IN MultiplicativeMonoid<U>::MultiplicativeMonoid(U e_U):PointedSet<U>(MO(e_U)){}TE <TY U,TY M_U> IN AbstractMonoid<U,M_U>::AbstractMonoid(M_U m_U,U e_U):AbstractMagma<U,M_U>(MO(m_U)),PointedSet<U>(MO(e_U)){}TE <TY U>CL VirtualGroup:VI PU VirtualMonoid<U>,VI PU VirtualPointedSet<U>,VI PU VirtualNSet<U>{};TE <TY U = ll>CL AdditiveGroup:VI PU VirtualGroup<U>,PU AdditiveMonoid<U>{PU:IN U Transfer(CO U& u);};TE <TY U,TY M_U,TY I_U>CL AbstractGroup:VI PU VirtualGroup<U>,PU AbstractMonoid<U,M_U>,PUAbstractNSet<U,I_U>{PU:IN AbstractGroup(M_U m_U,U e_U,I_U i_U);};TE <TY U,TY M_U,TY I_U> IN AbstractGroup<U,M_U,I_U>::AbstractGroup(M_U m_U,U e_U,I_U i_U):AbstractMonoid<U,M_U>(MO(m_U),MO(e_U)),AbstractNSet<U,I_U>(MO(i_U)){}TE <TY U> IN U AdditiveGroup<U>::Transfer(CO U& u){RE -u;}TE <TY T,TY R1,TY R2,TY E>CL VirtualGraph:VI PU UnderlyingSet<T>{PU:VI R1 Enumeration(CRI i)= 0;IN R2 Enumeration_inv(CO T& t);TE <TY PATH> IN R2Enumeration_inv(CO PATH& p);IN VO Reset();VI CRI SZ()CO NE = 0;VI E& edge()NE = 0;VI ret_t<E,T> Edge(CO T& t)= 0;TE <TY PATH> IN ret_t<E,T> Edge(CO PATH& p);ST IN CO T& Vertex(CO T& t)NE;TE <TY PATH> ST IN CO T& Vertex(CO PATH& e)NE;VI R2 Enumeration_inv_Body(CO T& t)= 0;};TE <TY T,TY R1,TY R2,TY E>CL EdgeImplimentation:VI PU VirtualGraph<T,R1,R2,E>{PU:int m_SZ;E m_edge;IN EdgeImplimentation(CRI SZ,E edge);IN CRI SZ()CO NE;IN E&edge()NE;IN ret_t<E,T> Edge(CO T& t);};TE <TY E>CL Graph:PU EdgeImplimentation<int,CRI,CRI,E>{PU:IN Graph(CRI SZ,E edge);IN CRI Enumeration(CRI i);TE <TY F> IN Graph<F> GetGraph(F edge)CO;IN CRI Enumeration_inv_Body(CRI t);};TE <TY T,TY Enum_T,TY Enum_T_inv,TY E>CL EnumerationGraph:PUEdgeImplimentation<T,ret_t<Enum_T,int>,ret_t<Enum_T_inv,T>,E>{PU:Enum_T m_enum_T;Enum_T_inv m_enum_T_inv;IN EnumerationGraph(CRI SZ,Enum_T enum_T,Enum_T_inv enum_T_inv,E edge);IN ret_t<Enum_T,int> Enumeration(CRI i);TE <TY F> IN EnumerationGraph<T,Enum_T,Enum_T_inv,F> GetGraph(F edge)CO;INret_t<Enum_T_inv,T> Enumeration_inv_Body(CO T& t);};TE <TY Enum_T,TY Enum_T_inv,TY E> EnumerationGraph(CRI SZ,Enum_T enum_T,Enum_T_inv enum_T_inv,E edge)-> EnumerationGraph<decldecay_t(declval<Enum_T>()(0)),Enum_T,Enum_T_inv,E>;TE <TY T,TY E>CL MemorisationGraph:PU EdgeImplimentation<T,T,CRI,E>{PU:int m_LE;VE<T> m_memory;Map<T,int> m_memory_inv;IN MemorisationGraph(CRI SZ,CO T& dummy,E edge);IN T Enumeration(CRI i);IN VO Reset();TE <TY F> IN MemorisationGraph<T,F> GetGraph(F edge)CO;IN CRI Enumeration_inv_Body(CO T& t);};TE <TY T,TY R1,TY R2,TY E> IN EdgeImplimentation<T,R1,R2,E>::EdgeImplimentation(CRI SZ,E edge):m_SZ(SZ),m_edge(MO(edge)){ST_AS(is_COructible_v<T,R1>&& is_COructible_v<int,R2> && is_invocable_v<E,T>);}TE <TY E> IN Graph<E>::Graph(CRI SZ,E edge):EdgeImplimentation<int,CRI,CRI,E>(SZ,MO(edge)){}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN EnumerationGraph<T,Enum_T,Enum_T_inv,E>::EnumerationGraph(CRI SZ,Enum_T enum_T,Enum_T_invenum_T_inv,E edge):EdgeImplimentation<T,ret_t<Enum_T,int>,ret_t<Enum_T_inv,T>,E>(SZ,MO(edge)),m_enum_T(MO(enum_T)),m_enum_T_inv(MO(enum_T_inv)){}TE <TY T,TY E> IN MemorisationGraph<T,E>::MemorisationGraph(CRI SZ,CO T& dummy,E edge):EdgeImplimentation<T,T,CRI,E>(SZ,MO(edge)),m_LE(),m_memory(),m_memory_inv(){ST_AS(is_invocable_v<E,T>);}TE <TY E> IN CRI Graph<E>::Enumeration(CRI i){RE i;}TE <TY T,TY Enum_T,TY Enum_T_inv,TYE> IN ret_t<Enum_T,int> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::Enumeration(CRI i){RE m_enum_T(i);}TE <TY T,TY E> IN T MemorisationGraph<T,E>::Enumeration(CRI i){AS(0 <= i && i < m_LE);RE m_memory[i];}TE <TY T,TY R1,TY R2,TY E> IN R2 VirtualGraph<T,R1,R2,E>::Enumeration_inv(CO T& t){RE Enumeration_inv_Body(t);}TE <TY T,TY R1,TY R2,TY E> TE <TY PATH> IN R2 VirtualGraph<T,R1,R2,E>::Enumeration_inv(CO PATH& p){REEnumeration_inv_Body(get<0>(p));}TE <TY E> IN CRI Graph<E>::Enumeration_inv_Body(CRI i){RE i;}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN ret_t<Enum_T_inv,T> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::Enumeration_inv_Body(CO T& t){RE m_enum_T_inv(t);}TE <TY T,TY E> IN CRI MemorisationGraph<T,E>::Enumeration_inv_Body(CO T& t){if(m_memory_inv.count(t)== 0){AS(m_LE < TH->SZ());m_memory.push_back(t);RE m_memory_inv[t]= m_LE++;}REm_memory_inv[t];}TE <TY T,TY R1,TY R2,TY E> VO VirtualGraph<T,R1,R2,E>::Reset(){}TE <TY T,TY E> IN VO MemorisationGraph<T,E>::Reset(){m_LE = 0;m_memory.clear();m_memory_inv.clear();}TE <TY T,TY R1,TY R2,TY E> IN CRI EdgeImplimentation<T,R1,R2,E>::SZ()CO NE{RE m_SZ;}TE <TY T,TY R1,TY R2,TY E> IN E& EdgeImplimentation<T,R1,R2,E>::edge()NE{RE m_edge;}TE <TY T,TY R1,TY R2,TY E> IN ret_t<E,T> EdgeImplimentation<T,R1,R2,E>::Edge(COT& t){RE m_edge(t);}TE <TY T,TY R1,TY R2,TY E> TE <TY PATH> IN ret_t<E,T> VirtualGraph<T,R1,R2,E>::Edge(CO PATH& p){RE Edge(get<0>(p));}TE <TY E>TE <TY F> IN Graph<F> Graph<E>::GetGraph(F edge)CO{RE Graph<F>(TH->SZ(),MO(edge));}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> TE <TY F> INEnumerationGraph<T,Enum_T,Enum_T_inv,F> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::GetGraph(F edge)CO{RE EnumerationGraph<T,Enum_T,Enum_T_inv,F>(TH->SZ(),m_enum_T,m_enum_T_inv,MO(edge));}TE <TY T,TY E> TE <TY F> IN MemorisationGraph<T,F> MemorisationGraph<T,E>::GetGraph(F edge)CO{REMemorisationGraph<T,F>(TH->SZ(),MO(edge));}TE <TY T,TY R1,TY R2,TY E> IN CO T& VirtualGraph<T,R1,R2,E>::Vertex(CO T& t)NE{RE t;}TE <TY T,TY R1,TYR2,TY E> TE <TY PATH> IN CO T& VirtualGraph<T,R1,R2,E>::Vertex(CO PATH& e)NE{RE Vertex(get<0>(e));}CL LinearEdge{PU:int m_SZ;int m_direction;IN LinearEdge(CRI SZ,CRI direction);IN VE<pair<int,int>> OP()(CRI t);};CL LinearGraph:PU Graph<LinearEdge>{PU:IN LinearGraph(CRI SZ,CRI direction = 1);};IN LinearEdge::LinearEdge(CRI SZ,CRI direction):m_SZ(SZ),m_direction(direction){}IN VE<pair<int,int>> LinearEdge::OP()(CRI t){VE<pair<int,int>> AN{};if((m_direction >> 1)== 1 && t > 0){AN.push_back({t - 1,1});}if((m_direction & 1)== 1 && t + 1 < m_SZ){AN.push_back({t + 1,1});}RE AN;}INLinearGraph::LinearGraph(CRI SZ,CRI direction):Graph<LinearEdge>(SZ,LinearEdge(SZ,direction)){}TE <TY T,TY GRAPH,TY U,TY ABEL_GROUP>CL AbstractUnionFindForest{PU:GRAPH& m_G;ABEL_GROUP m_M;int m_root_SZ;VE<int> m_pred;VE<int> m_height;VE<U> m_w;AbstractUnionFindForest(GRAPH& G,ABEL_GROUP M);CO decltype((declval<GRAPH>().Enumeration(0)))RootOfNode(CO T&);TE <TE <TY...> TY V> VE<T>GetRoot()CO;IN U Potential(CO T& t0,CO T& t1);IN CRI NodeSize()CO NE;IN CRI RootSize()CO NE;bool Graft(CO T& t0,CO T& t1,CO U& w = U());TE <TYPATH> IN bool Graft(CO T& t0,CO PATH& t1);};TE <TY GRAPH,TY ABEL_GROUP> AbstractUnionFindForest(GRAPH& G,ABEL_GROUP M)-> AbstractUnionFindForest<inner_t<GRAPH>,GRAPH,inner_t<ABEL_GROUP>,ABEL_GROUP>;TE <TY U = int>CL UnionFindForest:PU LinearGraph,PU AbstractUnionFindForest<int,LinearGraph,U,AdditiveGroup<U>>{PU:IN UnionFindForest(CRI SZ);};TE <TY T,TY GRAPH,TY U,TY ABEL_GROUP>AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::AbstractUnionFindForest(GRAPH& G,ABEL_GROUP M):m_G(G),m_M(MO(M)),m_root_SZ(m_G.SZ()),m_pred(m_root_SZ),m_height(m_root_SZ,1),m_w(m_root_SZ,m_M.Zero()){CRI SZ = m_G.SZ();for(int i = 0;i < SZ;i++){m_pred[i]= i;}for(int i = 0;i < SZ;i++){auto&& s = m_G.Enumeration(i);for(auto& t:m_G.Edge(s)){Graft(s,t);}}}TE <TY U> IN UnionFindForest<U>::UnionFindForest(CRI SZ):LinearGraph(SZ,0),AbstractUnionFindForest<int,LinearGraph,U,AdditiveGroup<U>>(*TH,AdditiveGroup<U>()){}TE <TY T,TY GRAPH,TY U,TYABEL_GROUP>CO decltype((declval<GRAPH>().Enumeration(0)))AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::RootOfNode(CO T& t){auto&& num = m_G.Enumeration_inv(t);int& pred1 = m_pred[num];WH(true){int& pred2 = m_pred[pred1];if(pred1 == pred2){break;}m_w[num]= m_M.Sum(m_w[num],m_w[pred1]=m_M.Sum(m_w[pred1],m_w[pred2]));pred1 = pred2 = m_pred[pred2];}RE m_G.Enumeration(pred1);}TE <TY T,TY GRAPH,TY U,TY ABEL_GROUP> TE <TE <TY...> TYV>VE<T> AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::GetRoot()CO{VE<T> AN{};AN.reserve(m_root_SZ);CRI SZ = m_G.SZ();for(int i = 0;i < SZ;i++){if(i == m_pred[i]){AN.push_back(m_G.Enumeration(i));}}RE AN;}TE <TY T,TY GRAPH,TY U,TY ABEL_GROUP>U AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::Potential(CO T& t0,CO T& t1){CO T& root0 = RootOfNode(t0);CO T& root1 = RootOfNode(t1);AS(root0 == root1);RE m_M.Sum(m_w[m_G.Enumeration_inv(t0)],m_M.Inverse(m_w[m_G.Enumeration_inv(t1)]));}TE <TY T,TY GRAPH,TY U,TY ABEL_GROUP> IN CRI AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::NodeSize()CO NE{RE m_G.SZ();}TE <TY T,TY GRAPH,TY U,TY ABEL_GROUP> IN CRI AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::RootSize()CO NE{RE m_root_SZ;}TE <TY T,TY GRAPH,TY U,TY ABEL_GROUP>bool AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::Graft(CO T& t0,CO T& t1,CO U& w){CO T& root0 = RootOfNode(t0);CO T& root1 = RootOfNode(t1);if(root0 == root1){RE Potential(t0,t1)== w;}auto&& num0 = m_G.Enumeration_inv(t0);auto&& num1 = m_G.Enumeration_inv(t1);auto&& rnum0 = m_G.Enumeration_inv(root0);auto&& rnum1 = m_G.Enumeration_inv(root1);int& height0 =m_height[rnum0];CRI height1 = m_height[rnum1];CO int* p_reMOd_root;CO int* p_reMOd_node;CO int* p_kept_root;if(height0 < height1){p_reMOd_root =&rnum0;p_reMOd_node = &num0;p_kept_root = &rnum1;m_w[*p_reMOd_root]= m_M.Sum(m_w[*p_reMOd_root],m_M.Sum(m_M.Sum(m_w[num1],w),m_M.Inverse(m_w[num0])));}else{if(height0 == height1){height0++;}p_reMOd_root = &rnum1;p_reMOd_node = &num1;p_kept_root = &rnum0;m_w[*p_reMOd_root]= m_M.Sum(m_w[*p_reMOd_root],m_M.Sum(m_M.Inverse(m_M.Sum(m_w[num1],w)),m_w[num0]));}if(*p_reMOd_node != *p_reMOd_root){m_w[*p_reMOd_node]= m_M.Sum(m_w[*p_reMOd_node],m_w[*p_reMOd_root]);}m_pred[*p_reMOd_node]= m_pred[*p_reMOd_root]= *p_kept_root;m_root_SZ--;RE true;}TE <TY T,TY GRAPH,TY U,TYABEL_GROUP> TE <TY PATH> IN bool AbstractUnionFindForest<T,GRAPH,U,ABEL_GROUP>::Graft(CO T& t0,CO PATH& t1){RE Graft(t0,get<0>(t1),get<1>(t1));}// AAA ライブラリは以上に挿入する。#define INCLUDE_MAIN#include __FILE__#else // INCLUDE_LIBRARY#ifdef DEBUG#define _GLIBCXX_DEBUG#define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE2 )#define SIGNAL signal( SIGABRT , &AlertAbort );#define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX )); assert( ( MIN ) <= A && A <= ( MAX ) )#define COUT( ... ) VariadicCout( cout << "出力:" , __VA_ARGS__ ) << endl#define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ ) << flush#define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl#define CERRNS( ... ) VariadicCout( cerr , __VA_ARGS__ ) << flush#define COUT_A( A , N ) OUTPUT_ARRAY( cout << "出力:" , A , N ) << endl#define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endlint exec_mode = 0;#else#pragma GCC optimize ( "O3" )#pragma GCC optimize ( "unroll-loops" )#pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" )#define SIGNAL#define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE1 )#define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) )#define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL#define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ )#define CERR( ... )#define CERRNS( ... )#define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << ENDL#define CERR_A( A , N )#endif#ifdef REACTIVE#ifdef DEBUG#define RSET( A , ... ) A = __VA_ARGS__#else#define RSET( A , ... ) cin >> A#endif#define RCIN( LL , A , ... ) LL A; RSET( A , __VA_ARGS__ )#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 , ... ) string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ )#define GETLINE( ... ) GETLINE_SEPARATE( '\n' , __VA_ARGS__ )#else#define SET_LL( A ) cin >> A#define CIN( LL , ... ) LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ )#define SET_A( I , N , ... ) VariadicResize( N + I , __VA_ARGS__ ); FOR( VARIABLE_FOR_SET_A , 0 , N ){ VariadicSet( cin , VARIABLE_FOR_SET_A + I ,__VA_ARGS__ ); }#define CIN_A( LL , I , N , ... ) vector<LL> __VA_ARGS__; SET_A( I , N , __VA_ARGS__ )#define CIN_AA( LL , I0 , N0 , I1 , N1 , VAR ) vector<vector<LL>> VAR( N0 + I0 ); FOR( VARIABLE_FOR_CIN_AA , 0 , N0 ){ SET_A( I1 , N1 ,VAR[VARIABLE_FOR_CIN_AA + I0] ); }#endif#include <bits/stdc++.h>using namespace std;#define REPEAT_MAIN( BOUND ) int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ); SIGNAL; CEXPR( int , bound_test_case_num , BOUND );int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ CERR( "テストケースの個数を入力してください。" ); SET_ASSERT( test_case_num , 1 ,bound_test_case_num ); } FOR( test_case , 0 , test_case_num ){ if constexpr( bound_test_case_num > 1 ){ CERR( "testcase" , test_case , ":" ); }Solve(); CERR( "" ); } CHECK_REDUNDANT_INPUT; }#define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now()#define CURRENT_TIME static_cast<double>( chrono::duration_cast<chrono::microseconds>( chrono::system_clock::now() - watch ).count() / 1000.0 )#define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 )#define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE#define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX )#define SET_A_ASSERT( I , N , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_A , 0 , N ){ SET_ASSERT( A[VARIABLE_FOR_SET_A + I] , MIN , MAX ); }#define SET_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_AA0 , 0 , N0 ){ FOR( VARIABLE_FOR_SET_AA1 , 0 , N1 ){ SET_ASSERT(A[VARIABLE_FOR_SET_AA0 + I0][VARIABLE_FOR_SET_AA1 + I1] , MIN , MAX ); } }#define CIN_ASSERT( A , MIN , MAX ) decldecay_t( MAX ) A; SET_ASSERT( A , MIN , MAX )#define CIN_A_ASSERT( I , N , A , MIN , MAX ) vector<decldecay_t( MAX )> A( N + I ); SET_A_ASSERT( I , N , A , MIN , MAX )#define CIN_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) vector A( N0 + I0 , vector<decldecay_t( MAX )>( N1 + I1 ) ); SET_AA_ASSERT( I0 , N0 , I1 ,N1 , A , MIN , MAX )#define SET_MAX( A , X ) A = max( A , decltype( A )( X ) )#define SET_MIN( A , X ) A = min( A , decltype( A )( X ) )#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( decldecay_t( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ )#define FOREQ( VAR , INITIAL , FINAL ) for( decldecay_t( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ )#define FOREQINV( VAR , INITIAL , FINAL ) for( decldecay_t( INITIAL ) VAR = INITIAL ; VAR + 1 > FINAL ; VAR -- )#define ITR( ARRAY ) auto begin_ ## ARRAY = ARRAY .BE() , itr_ ## ARRAY = begin_ ## ARRAY , end_ ## ARRAY = ARRAY .EN()#define FOR_ITR( ARRAY ) for( ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ )#define RUN( ARRAY , ... ) for( auto&& __VA_ARGS__ : ARRAY )#define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES )#define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS )#define RETURN( ... ) COUT( __VA_ARGS__ ); return// 型のエイリアス#define decldecay_t( VAR ) decay_t<decltype( VAR )>template <typename F , typename...Args> using ret_t = decltype( declval<F>()( declval<Args>()... ) );template <typename T> using inner_t = typename T::type;using uint = unsigned int;using ll = long long;using ull = unsigned long long;using ld = long double;using lld = __float128;template <typename INT> using T2 = pair<INT,INT>;template <typename INT> using T3 = tuple<INT,INT,INT>;template <typename INT> using T4 = tuple<INT,INT,INT,INT>;using path = pair<int,ll>;/* VVV 常設ライブラリの非圧縮版は以下に挿入する。*/// Randomll GetRand( const int& Rand_min , const int& Rand_max ) { assert( Rand_min <= Rand_max ); ll answer = time( NULL ); return answer * rand() % (Rand_max + 1 - Rand_min ) + Rand_min; }// Set#define DECLARATION_OF_HASH( ... ) \struct hash<__VA_ARGS__> \{ \\inline size_t operator()( const __VA_ARGS__& n ) const; \\}; \class is_ordered{private:is_ordered() = delete;template <typename T> static constexpr auto Check( const T& t ) -> decltype( t < t , true_type() );static constexpr false_type Check( ... );public:template <typename T> static constexpr const bool value = is_same_v< decltype( Check( declval<T>() ) ) , true_type >;};template <typename T>using Set = conditional_t<is_constructible_v<unordered_set<T>>,unordered_set<T>,conditional_t<is_ordered::value<T>,set<T>,void>>;// Tuple#define DECLARATION_OF_ARITHMETIC_FOR_TUPLE( OPR ) \template <typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const V<T,U>& t1 ) -> decltype(( get<0>( t0 ) , t0 ) )&; \template <typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const tuple<T,U,V>& t1 ); \template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , const tuple<T,U,V,W>&t1 ); \template <typename ARG , typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const ARG& t1 )-> decltype( ( get<0>( t0 ) , t0 ) )&; \template <typename ARG , typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const ARG& t1 ); \template <typename ARG , typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , constARG& t1 ); \template <template <typename...> typename V , typename...ARGS , typename ARG> inline auto operator OPR( const V<ARGS...>& t0 , const ARG& t1 ) ->decldecay_t( ( get<0>( t0 ) , t0 ) ) \#define DEFINITION_OF_ARITHMETIC_FOR_TUPLE( OPR ) \template <typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const V<T,U>& t1 ) -> decltype(( get<0>( t0 ) , t0 ) )& { get<0>( t0 ) OPR ## = get<0>( t1 ); get<1>( t0 ) OPR ## = get<1>( t1 ); return t0; } \template <typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const tuple<T,U,V>& t1 ) { get<0>( t0 )OPR ## = get<0>( t1 ); get<1>( t0 ) OPR ## = get<1>( t1 ); get<2>( t0 ) OPR ## = get<2>( t1 ); return t0; } \template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , const tuple<T,U,V,W>&t1 ) { get<0>( t0 ) OPR ## = get<0>( t1 ); get<1>( t0 ) OPR ## = get<1>( t1 ); get<2>( t0 ) OPR ## = get<2>( t1 ); get<3>( t0 ) OPR ## = get<3>( t1 ); return t0; } \template <typename ARG , typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const ARG& t1 )-> decltype( ( get<0>( t0 ) , t0 ) )& { get<0>( t0 ) OPR ## = t1; get<1>( t0 ) OPR ## = t1; return t0; } \template <typename ARG , typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const ARG& t1 ) { get<0>(t0 ) OPR ## = t1; get<1>( t0 ) OPR ## = t1; get<2>( t0 ) OPR ## = t1; return t0; } \template <typename ARG , typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , constARG& t1 ) { get<0>( t0 ) OPR ## = t1; get<1>( t0 ) OPR ## = t1; get<2>( t0 ) OPR ## = t1; get<3>( t0 ) OPR ## = t1; return t0; } \template <template <typename...> typename V , typename...ARGS , typename ARG> inline auto operator OPR( const V<ARGS...>& t0 , const ARG& t1 ) ->decldecay_t( ( get<0>( t0 ) , t0 ) ) { auto t = t0; return move( t OPR ## = t1 ); } \#define DECLARATION_OF_INCREMENT_FOR_TUPLE( INCR ) \template <typename T , typename U , template <typename...> typename V> inline auto operator INCR( V<T,U>& t ) -> decltype( ( get<0>( t ) , t ) )&;\template <typename T , typename U , typename V> inline tuple<T,U,V>& operator INCR ( tuple<T,U,V>& t ); \template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator INCR ( tuple<T,U,V,W>& t ); \#define DEFINITION_OF_INCREMENT_FOR_TUPLE( INCR ) \template <typename T , typename U , template <typename...> typename V> inline auto operator INCR( V<T,U>& t ) -> decltype( ( get<0>( t ) , t ) )& {INCR get<0>( t ); INCR get<1>( t ); return t; } \template <typename T , typename U , typename V> inline tuple<T,U,V>& operator INCR ( tuple<T,U,V>& t ) { INCR get<0>( t ); INCR get<1>( t ); INCRget<2>( t ); return t; } \template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator INCR ( tuple<T,U,V,W>& t ) { INCR get<0>( t ); INCRget<1>( t ); INCR get<2>( t ); INCR get<3>( t ); return t; } \DEFINITION_OF_ARITHMETIC_FOR_TUPLE( + );DEFINITION_OF_ARITHMETIC_FOR_TUPLE( - );DEFINITION_OF_ARITHMETIC_FOR_TUPLE( * );DEFINITION_OF_ARITHMETIC_FOR_TUPLE( / );DEFINITION_OF_ARITHMETIC_FOR_TUPLE( % );DEFINITION_OF_INCREMENT_FOR_TUPLE( ++ );DEFINITION_OF_INCREMENT_FOR_TUPLE( -- );template <class Traits , typename T> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , tuple<T>& arg ){ return is >>get<0>( arg ); }template <class Traits , typename T , typename U , template <typename...> typename V> inline auto operator>>( basic_istream<char,Traits>& is , V<T,U>& arg ) -> decltype((get<0>(arg),is))& { return is >> get<0>( arg ) >> get<1>( arg ); }template <class Traits , typename T , typename U , typename V> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , tuple<T,U,V>& arg ) { return is >> get<0>( arg ) >> get<1>( arg ) >> get<2>( arg ); }template <class Traits , typename T , typename U , typename V , typename W> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , tuple<T,U,V,W>& arg ) { return is >> get<0>( arg ) >> get<1>( arg ) >> get<2>( arg ) >> get<3>( arg ); }template <class Traits , typename T> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const tuple<T>& arg ) { returnos << get<0>( arg ); }template <class Traits , typename T , typename U , template <typename...> typename V> inline auto operator<<( basic_ostream<char,Traits>& os , constV<T,U>& arg ) -> decltype((get<0>(arg),os))& { return os << get<0>( arg ) << " " << get<1>( arg ); }template <class Traits , typename T , typename U , typename V> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , consttuple<T,U,V>& arg ) { return os << get<0>( arg ) << " " << get<1>( arg ) << " " << get<2>( arg ); }template <class Traits , typename T , typename U , typename V , typename W> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const tuple<T,U,V,W>& arg ) { return os << get<0>( arg ) << " " << get<1>( arg ) << " " << get<2>( arg ) << " " << get<3>( arg ); }#define DEFINITION_OF_HASH_FOR_TUPLE( PAIR ) \template <typename T , typename U> inline size_t hash<PAIR<T,U>>::operator()( const PAIR<T,U>& n ) const { static const size_t seed = ( GetRand(1e3 , 1e8 ) << 1 ) | 1; static const hash<T> h0; static const hash<U> h1; return ( h0( get<0>( n ) ) * seed ) ^ h1( get<1>( n ) ); } \template <typename T> DECLARATION_OF_HASH( tuple<T> );template <typename T , typename U> DECLARATION_OF_HASH( pair<T,U> );template <typename T , typename U> DECLARATION_OF_HASH( tuple<T,U> );template <typename T , typename U , typename V> DECLARATION_OF_HASH( tuple<T,U,V> );template <typename T , typename U , typename V , typename W> DECLARATION_OF_HASH( tuple<T,U,V,W> );template <typename T> inline size_t hash<tuple<T>>::operator()( const tuple<T>& n ) const { static const hash<T> h; return h(get<0>( n ) ); }DEFINITION_OF_HASH_FOR_TUPLE( pair );DEFINITION_OF_HASH_FOR_TUPLE( tuple );template <typename T , typename U , typename V> inline size_t hash<tuple<T,U,V>>::operator()( const tuple<T,U,V>& n ) const { static const size_tseed = ( GetRand( 1e3 , 1e8 ) << 1 ) | 1; static const hash<pair<T,U>> h01; static const hash<V> h2; return ( h01( { get<0>( n ) , get<1>( n ) }) * seed ) ^ h2( get<2>( n ) ); }template <typename T , typename U , typename V , typename W> inline size_t hash<tuple<T,U,V,W>>::operator()( const tuple<T,U,V,W>& n ) const { staticconst size_t seed = ( GetRand( 1e3 , 1e8 ) << 1 ) | 1; static const hash<pair<T,U>> h01; static const hash<pair<V,W>> h23; return ( h01( { get<0>( n ) , get<1>( n ) } ) * seed ) ^ h23( { get<2>( n ) , get<3>( n ) } ); }// Vector#define DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , OPR ) \template <typename T> inline V<T>& operator OPR ## = ( V<T>& a , const T& t ); \template <typename T> inline V<T>& operator OPR ## = ( V<T>& a0 , const V<T>& a1 ); \template <typename T , typename U> inline V<T> operator OPR( V<T> a , const U& u ); \#define DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , OPR ) \template <typename T> inline V<T>& operator OPR ## = ( V<T>& a , const T& t ) { for( auto& s : a ){ s OPR ## = t; } return a; } \template <typename T> inline V<T>& operator OPR ## = ( V<T>& a0 , const V<T>& a1 ) { assert( a0.size() <= a1.size() ); auto itr0 = a0.begin() ,end0 = a0.end(); auto itr1 = a1.begin(); while( itr0 != end0 ){ *( itr0++ ) OPR ## = *( itr1++ ); } return a0; } \template <typename T , typename U> inline V<T> operator OPR( V<T> a , const U& u ) { return move( a OPR ## = u ); } \#define DECLARATION_OF_INCREMENT_FOR_VECTOR( V , INCR ) \template <typename T> inline V<T>& operator INCR( V<T>& a ); \#define DEFINITION_OF_INCREMENT_FOR_VECTOR( V , INCR ) \template <typename T> inline V<T>& operator INCR( V<T>& a ) { for( auto& i : a ){ INCR i; } return a; } \#define DECLARATION_OF_ARITHMETICS_FOR_VECTOR( V ) \DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , + ); \DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , - ); \DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , * ); \DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , / ); \DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , % ); \DECLARATION_OF_INCREMENT_FOR_VECTOR( V , ++ ); \DECLARATION_OF_INCREMENT_FOR_VECTOR( V , -- ); \template <typename T> inline V<T> operator*( const T& scalar , V<T> v ) \#define DEFINITION_OF_ARITHMETICS_FOR_VECTOR( V ) \DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , + ); \DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , - ); \DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , * ); \DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , / ); \DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , % ); \DEFINITION_OF_INCREMENT_FOR_VECTOR( V , ++ ); \DEFINITION_OF_INCREMENT_FOR_VECTOR( V , -- ); \template <typename T> inline V<T> operator*( const T& scalar , V<T> v ) { for( auto& t : v ){ t *= scalar; } return move( v ); } \DEFINITION_OF_ARITHMETICS_FOR_VECTOR( vector );DEFINITION_OF_ARITHMETICS_FOR_VECTOR( list );template <typename V> inline auto Get( V& a ) { return [&]( const int& i = 0 ) -> const decldecay_t( a[0] )& { return a[i]; }; }template <typename T = int> inline vector<T> id( const int& size ) { vector<T> answer( size ); for( int i = 0 ; i < size ; i++ ){ answer[i] = i; }return answer; }template <typename T> inline void Sort( vector<T>& a , const bool& reversed = false ) { if( reversed ){ static auto comp = []( const T& t0 , const T&t1 ) { return t1 < t0; }; sort( a.begin() , a.end() , comp ); } else { sort( a.begin() , a.end() ); } }template <typename T0 , typename T1> inline void Sort( vector<T0>& a , vector<T1>& b , const bool& reversed = false ) { const int size = a.size();assert( size == int( b.size() ) ); vector<pair<T0,T1>> v( size ); for( int i = 0 ; i < size ; i++ ){ v[i] = { move( a[i] ) , move( b[i] ) }; }Sort( v , reversed ); for( int i = 0 ; i < size ; i++ ){ a[i] = move( v[i].first ); b[i] = move( v[i].second ); } }template <typename T> inline vector<int> IndexSort( const vector<T>& a , const bool& reversed = false ) { auto index = id<int>( a.size() ); if(reversed ){ sort( index.begin() , index.end() , [&]( const int& i , const int& j ) { return a[j] < a[i]; } ); } else { sort( index.begin() ,index.end() , [&]( const int& i , const int& j ) { return a[i] < a[j]; } ); } return index; }#define DECLARATION_OF_COUT_FOR_VECTOR( V ) template <class Traits , typename Arg> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const V<Arg>& arg )#define DEFINITION_OF_COUT_FOR_VECTOR( V ) template <class Traits , typename Arg> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const V<Arg>& arg ) { auto begin = arg.begin() , end = arg.end(); auto itr = begin; while( itr != end ){ ( itr == begin ? os : os<< " " ) << *itr; itr++; } return os; }DEFINITION_OF_COUT_FOR_VECTOR( vector );DEFINITION_OF_COUT_FOR_VECTOR( list );DEFINITION_OF_COUT_FOR_VECTOR( set );DEFINITION_OF_COUT_FOR_VECTOR( unordered_set );inline void VariadicResize( const int& size ) {}template <typename Arg , typename... ARGS> inline void VariadicResize( const int& size , Arg& arg , ARGS&... args ) { arg.resize( size );VariadicResize( size , args... ); }// Map#define DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , OPR ) \template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a , const pair<T,U>& v ); \template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a0 , const MAP<T,U>& a1 ); \template <typename T , typename U , typename ARG> inline MAP<T,U> operator OPR( MAP<T,U> a , const ARG& arg ); \#define DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , OPR ) \template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a , const pair<T,U>& v ) { a[v.first] OPR ## = v.second; return a; } \template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a0 , const MAP<T,U>& a1 ) { for( auto& [t,u] : a1 ){ a0[t] OPR ##= u; } return a0; } \template <typename T , typename U , typename ARG> inline MAP<T,U> operator OPR( MAP<T,U> a , const ARG& arg ) { return move( a OPR ## = arg ); } \#define DECLARATION_OF_ARITHMETICS_FOR_MAP( MAP ) \DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , + ); \DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , - ); \DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , * ); \DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , / ); \DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , % ); \#define DEFINITION_OF_ARITHMETICS_FOR_MAP( MAP ) \DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , + ); \DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , - ); \DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , * ); \DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , / ); \DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , % ); \template <typename T , typename U>using Map = conditional_t<is_constructible_v<unordered_map<T,int>>,unordered_map<T,U>,conditional_t<is_ordered::value<T>,map<T,U>,void>>;DEFINITION_OF_ARITHMETICS_FOR_MAP( map );DEFINITION_OF_ARITHMETICS_FOR_MAP( unordered_map );// StdStreamtemplate <class Traits> inline basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is ) { return is; }template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is , Arg& arg ,ARGS&... args ) { return VariadicCin( is >> arg , args... ); }template <class Traits> inline basic_istream<char,Traits>& VariadicSet( basic_istream<char,Traits>& is , const int& i ) { return is; }template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicSet( basic_istream<char,Traits>& is , const int&i , Arg& arg , ARGS&... args ) { return VariadicSet( is >> arg[i] , i , args... ); }template <class Traits> inline basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , const char& separator ) { return is; }template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , constchar& separator , Arg& arg , ARGS&... args ) { return VariadicGetline( getline( is , arg , separator ) , separator , args... ); }template <class Traits , typename Arg> inline basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , Arg&& arg ) { return os <<forward<Arg>( arg ); }template <class Traits , typename Arg1 , typename Arg2 , typename... ARGS> inline basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , Arg1&& arg1 , Arg2&& arg2 , ARGS&&... args ) { return VariadicCout( os << forward<Arg1>( arg1 ) << " " , forward<Arg2>( arg2 ) , forward<ARGS>( args )... ); }template <class Traits , typename Arg> inline basic_ostream<char,Traits>& VariadicCoutNonSep( basic_ostream<char,Traits>& os , Arg&& arg ) { returnos << forward<Arg>( arg ); }template <class Traits , typename Arg1 , typename Arg2 , typename... ARGS> inline basic_ostream<char,Traits>& VariadicCoutNonSep( basic_ostream<char,Traits>& os , Arg1&& arg1 , Arg2&& arg2 , ARGS&&... args ) { return VariadicCoutNonSep( os << forward<Arg1>( arg1 ) , forward<Arg2>( arg2 ) ,forward<ARGS>( args )... ); }template <class Traits , typename ARRAY> inline basic_ostream<char,Traits>& CoutArray( basic_ostream<char,Traits>& os , const int& i_start , constint& i_ulim , ARRAY&& a ) { for( int i = i_start ; i < i_ulim ; i++ ){ ( i == i_start ? os : ( os << " " ) ) << a[i]; } return os; }/* AAA 常設ライブラリの非圧縮版は以上に挿入する。*/// デバッグ用#ifdef DEBUGinline void AlertAbort( int n ) { CERR("abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); }#endif// 入力フォーマットチェック用// 1行中の変数の個数をSEPARATOR区切りで確認#define GETLINE_COUNT( S , VARIABLE_NUMBER , SEPARATOR ) GETLINE( S ); int VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S = 0; intVARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S = S.size(); { int size = S.size(); int count = 0; for( int i = 0 ; i < size ; i++ ){ if( S[i] ==SEPARATOR ){ count++; } } assert( VARIABLE_NUMBER == 0 ? size == 0 : count + 1 == VARIABLE_NUMBER ); }// 余計な入力の有無を確認#if defined( DEBUG ) || defined( REACTIVE )#define CHECK_REDUNDANT_INPUT#else#ifdef USE_GETLINE#define CHECK_REDUNDANT_INPUT string VARIABLE_FOR_CHECK_REDUNDANT_INPUT = ""; getline( cin , VARIABLE_FOR_CHECK_REDUNDANT_INPUT ); assert(VARIABLE_FOR_CHECK_REDUNDANT_INPUT == "" ); assert( ! cin )#else#define CHECK_REDUNDANT_INPUT string VARIABLE_FOR_CHECK_REDUNDANT_INPUT = ""; cin >> VARIABLE_FOR_CHECK_REDUNDANT_INPUT; assert(VARIABLE_FOR_CHECK_REDUNDANT_INPUT == "" ); assert( ! cin )#endif#endif// MIN <= N <= MAXを満たすNをSから構築#define STOI( S , N , MIN , MAX ) decldecay_t( MAX ) N = 0; decldecay_t( MAX ) BOUND ## N = max( decldecay_t( MAX )( abs( MIN ) ) , abs( MAX ) ); {bool VARIABLE_FOR_POSITIVITY_FOR_GETLINE = true; assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); if( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) == "-" ){ VARIABLE_FOR_POSITIVITY_FOR_GETLINE = false;VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); }assert( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) != " " ); string VARIABLE_FOR_LETTER_FOR_GETLINE{}; intVARIABLE_FOR_DIGIT_FOR_GETLINE{}; while( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ? (VARIABLE_FOR_LETTER_FOR_GETLINE = S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) ) != " " : false ){ VARIABLE_FOR_DIGIT_FOR_GETLINE =stoi( VARIABLE_FOR_LETTER_FOR_GETLINE ); assert( N < BOUND ## N / 10 ? true : N == BOUND ## N / 10 && VARIABLE_FOR_DIGIT_FOR_GETLINE <= BOUND ##N % 10 ); N = N * 10 + VARIABLE_FOR_DIGIT_FOR_GETLINE; VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } if( ! VARIABLE_FOR_POSITIVITY_FOR_GETLINE){ N *= -1; } if( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ){ VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S++; } ASSERT( N , MIN , MAX ); }#define STOI_A( S , I , N , A , MIN , MAX ) vector<decldecay_t( MAX )> A( N + I ); FOR( VARIABLE_FOR_STOI_A , 0 , N ){ STOI( S , A##_VARIABLE_FOR_STOI_A , MIN , MAX ); A[VARIABLE_FOR_STOI_A + I] = A ##_VARIABLE_FOR_STOI_A; }// Sをstring SEPARATORで区切りTを構築#define SEPARATE( S , T , SEPARATOR ) string T{}; { assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); intVARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S_prev = VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S; assert( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_## S , 1 ) != SEPARATOR ); string VARIABLE_FOR_LETTER_FOR_GETLINE{}; while( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S <VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ? ( VARIABLE_FOR_LETTER_FOR_GETLINE = S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) ) !=SEPARATOR : false ){ VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } T = S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S_prev ,VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S - VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S_prev ); if( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S <VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ){ VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } }#define INCLUDE_LIBRARY#include __FILE__#endif // INCLUDE_LIBRARY#endif // INCLUDE_MAIN