結果

問題 No.2202 贅沢てりたまチキン
ユーザー 👑 p-adicp-adic
提出日時 2024-01-02 09:52:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 5,498 bytes
コンパイル時間 1,057 ms
コンパイル使用メモリ 84,364 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-09-27 17:39:37
合計ジャッジ時間 2,484 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 5 ms
7,936 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 36 ms
7,936 KB
testcase_15 AC 36 ms
7,936 KB
testcase_16 AC 31 ms
7,936 KB
testcase_17 AC 32 ms
7,936 KB
testcase_18 AC 16 ms
6,940 KB
testcase_19 AC 20 ms
7,936 KB
testcase_20 AC 37 ms
7,936 KB
testcase_21 AC 39 ms
7,936 KB
testcase_22 AC 26 ms
6,940 KB
testcase_23 AC 30 ms
6,940 KB
testcase_24 AC 28 ms
6,940 KB
testcase_25 AC 52 ms
7,936 KB
testcase_26 AC 35 ms
7,936 KB
testcase_27 AC 37 ms
7,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ( "O3" )
#pragma GCC optimize( "unroll-loops" )
#pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" )
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdint.h>
#include <cassert>
using namespace std;

using uint = unsigned int;

#define TYPE_OF( VAR ) remove_const<remove_reference<decltype( VAR )>::type >::type
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr )
#define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE
#define CIN( LL , A ) LL A; cin >> A
#define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) )
#define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); 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 QUIT return 0
#define COUT( ANSWER ) cout << ( ANSWER ) << "\n";
#define RETURN( ANSWER ) COUT( ANSWER ); QUIT

// 入力フォーマットチェック用
// 余分な改行を許さない
#define CHECK_REDUNDANT_INPUT string VARIABLE_FOR_CHECK_REDUNDANT_INPUT = ""; cin >> VARIABLE_FOR_CHECK_REDUNDANT_INPUT; assert( VARIABLE_FOR_CHECK_REDUNDANT_INPUT == "" ); assert( ! cin );
// #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 );
// |N| <= BOUNDを満たすNをSから構築
#define STOI( S , N , BOUND ) TYPE_OF( BOUND ) N = 0; { bool VARIABLE_FOR_POSITIVITY_FOR_STOI = true;  assert( S != "" ); if( S.substr( 0 , 1 ) == "-" ){ VARIABLE_FOR_POSITIVITY_FOR_STOI = false; S = S.substr( 1 ); assert( S != "" ); } assert( S.substr( 0 , 1 ) != " " ); while( S == "" ? false : S.substr( 0 , 1 ) != " " ){ assert( N < BOUND / 10 ? true : N == BOUND / 10 && stoi( S.substr( 0 , 1 ) ) <= BOUND % 10 ); N = N * 10 + stoi( S.substr( 0 , 1 ) ); S = S.substr( 1 ); } if( ! VARIABLE_FOR_POSITIVITY_FOR_STOI ){ N *= -1; } if( S != "" ){ S = S.substr( 1 ); } } 
// 1行で入力される変数の個数が適切か確認(半角空白の個数+1を調べる)
#define COUNT_VARIABLE( S , VARIABLE_NUMBER ) { int size = S.size(); int count = 0; for( int i = 0 ; i < size ; i++ ){ if( S.substr( i , 1 ) == " " ){ count++; } } assert( count + 1 == VARIABLE_NUMBER ); } 

#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&

#include <vector>

TE <TY T = int>CL UnionFindForest{PU:uint m_node_SZ;uint m_root_SZ;VE<uint> m_pred;VE<uint> m_height;VE<T> m_w;IN UnionFindForest(CRUI SZ);CRUI RootOfNode(CRUI num);TE <TE <TY...> TY V> VO SetRoot(V<uint>& a)CO;IN T Weight(CRUI num0,CRUI num1);IN CRUI SZOfNode()CO NE;IN CRUI SZOfRoot()CO NE;bool Graft(CRUI num0,CRUI num1,CO T& w = 0);};
TE <TY T> IN UnionFindForest<T>::UnionFindForest(CRUI SZ):m_node_SZ(SZ),m_root_SZ(m_node_SZ),m_pred(m_node_SZ),m_height(m_node_SZ,1),m_w(m_node_SZ){for(uint i = 0;i < m_node_SZ;i++){m_pred[i]= i;}}TE <TY T>CRUI UnionFindForest<T>::RootOfNode(CRUI num){uint& pred1 = m_pred[num];WH(true){uint& pred2 = m_pred[pred1];if(pred1 == pred2){break;}m_w[num]+= m_w[pred1]+= m_w[pred2];pred1 = pred2 = m_pred[pred2];}RE pred1;}TE <TY T> TE <TE <TY...> TY V>VO UnionFindForest<T>::SetRoot(V<uint>& a)CO{a.clear();for(uint i = 0;i < m_node_SZ;i++){if(i == m_pred[i]){a.push_back(i);}}RE;}TE <TY T>T UnionFindForest<T>::Weight(CRUI num0,CRUI num1){assert(num0 < m_node_SZ && num1 < m_node_SZ);CRUI root0 = RootOfNode(num0);CRUI root1 = RootOfNode(num1);assert(root0 == root1);RE m_w[num1]- m_w[num0];}TE <TY T> IN CRUI UnionFindForest<T>::SZOfNode()CO NE{RE m_node_SZ;}TE <TY T> IN CRUI UnionFindForest<T>::SZOfRoot()CO NE{RE m_root_SZ;}TE <TY T>bool UnionFindForest<T>::Graft(CRUI num0,CRUI num1,CO T& w){assert(num0 < m_node_SZ && num1 < m_node_SZ);CRUI root0 = RootOfNode(num0);CRUI root1 = RootOfNode(num1);if(root0 == root1){RE Weight(num0,num1)== w;}uint& height0 = m_height[root0];CRUI height1 = m_height[root1];CO uint* p_reMOd_root;CO uint* p_reMOd_node;CO uint* p_kept_root;if(height0 < height1){p_reMOd_root = &root0;p_reMOd_node = &num0;p_kept_root = &root1;m_w[*p_reMOd_root]-= w - m_w[num1]+ m_w[num0];}else{if(height0 == height1){height0++;}p_reMOd_root = &root1;p_reMOd_node = &num1;p_kept_root = &root0;m_w[*p_reMOd_root]+= w - m_w[num1]+ m_w[num0];}if(*p_reMOd_node != *p_reMOd_root){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;}

int main()
{
  UNTIE;
  CEXPR( uint , bound , 200000 );
  CIN_ASSERT( N , 1 , bound );
  CIN_ASSERT( M , 0 , N < ( 1 << 15 ) ? min( bound , ( N * ( N - 1 ) ) >> 1 ) : bound );
  UnionFindForest<> uff{ N << 1 };
  FOR( i , 0 , M ){
    CIN_ASSERT( Ai , 1 , N );
    CIN_ASSERT( Bi , 1 , N );
    assert( Ai != Bi );
    Ai--;
    Bi--;
    uff.Graft( Ai , Bi + N );
    uff.Graft( Ai + N , Bi );
  }
  CHECK_REDUNDANT_INPUT;
  FOR( j , 0 , N ){
    if( uff.RootOfNode( j ) != uff.RootOfNode( j + N ) ){
      RETURN( "No" );
    }
  }
  RETURN( "Yes" );
}
0