結果
問題 | No.2202 贅沢てりたまチキン |
ユーザー | 👑 p-adic |
提出日時 | 2024-01-02 09:36:15 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 52 ms / 2,000 ms |
コード長 | 7,278 bytes |
コンパイル時間 | 1,136 ms |
コンパイル使用メモリ | 83,468 KB |
実行使用メモリ | 8,064 KB |
最終ジャッジ日時 | 2024-09-27 17:39:28 |
合計ジャッジ時間 | 3,465 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 5 ms
8,064 KB |
testcase_11 | AC | 2 ms
6,944 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,944 KB |
testcase_14 | AC | 36 ms
7,936 KB |
testcase_15 | AC | 35 ms
7,936 KB |
testcase_16 | AC | 29 ms
7,936 KB |
testcase_17 | AC | 31 ms
7,936 KB |
testcase_18 | AC | 15 ms
6,944 KB |
testcase_19 | AC | 21 ms
8,064 KB |
testcase_20 | AC | 39 ms
7,936 KB |
testcase_21 | AC | 38 ms
7,936 KB |
testcase_22 | AC | 25 ms
6,940 KB |
testcase_23 | AC | 29 ms
6,940 KB |
testcase_24 | AC | 28 ms
6,940 KB |
testcase_25 | AC | 52 ms
7,936 KB |
testcase_26 | AC | 38 ms
7,936 KB |
testcase_27 | AC | 37 ms
7,808 KB |
ソースコード
#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> // Tが加法について可換群をなす場合にのみサポート。 // - 構築 O(size) // - 一点根取得 O(α(size)) // - 全根取得 O(size) // - 根数取得 O(1) // - 二点符号付き重み取得 O(α(size)) // - 二点接合 O(α(size)) template <typename T = int> class UnionFindForest { private: uint m_node_size; uint m_root_size; vector<uint> m_pred; vector<uint> m_height; // m_w[num]はnum番目のnodeがrootならば0、rootでないならば親nodeへ向かうパスの符号付き重み vector<T> m_w; public: inline UnionFindForest( const uint& size ); // num番目のnodeのrootを計算して返す。 const uint& RootOfNode( const uint& num ); // rootを全て格納する。 template <template <typename...> typename V> void SetRoot( V<uint>& a ) const; // num1番目のnodeからnum0番目のnodeへ向かうパスの符号付き重みを返す。 inline T Weight( const uint& num0 , const uint& num1 ); inline const uint& SizeOfNode() const noexcept; inline const uint& SizeOfRoot() const noexcept; // num1番目のnodeからnum0番目のnodeへ符号付き重みwの有向辺を結ぶ操作と整合的に // なるようにrootを接合。符号付き重みの整合性が取れない場合はfalseを返す。 bool Graft( const uint& num0 , const uint& num1 , const T& w = 0 ); }; template <typename T> inline UnionFindForest<T>::UnionFindForest( const uint& size ) : m_node_size( size ) , m_root_size( m_node_size ) , m_pred( m_node_size ) , m_height( m_node_size , 1 ) , m_w( m_node_size ) { for( uint i = 0 ; i < m_node_size ; i++ ){ m_pred[i] = i; } } template <typename T> const uint& UnionFindForest<T>::RootOfNode( const uint& num ) { uint& pred1 = m_pred[num]; while( true ){ uint& pred2 = m_pred[pred1]; if( pred1 == pred2 ){ break; } m_w[num] += m_w[pred1] += m_w[pred2]; pred1 = pred2 = m_pred[pred2]; } return pred1; } template <typename T> template <template <typename...> typename V> void UnionFindForest<T>::SetRoot( V<uint>& a ) const { a.clear(); for( uint i = 0 ; i < m_node_size ; i++ ){ if( i == m_pred[i] ){ a.push_back( i ); } } return; } template <typename T> T UnionFindForest<T>::Weight( const uint& num0 , const uint& num1 ) { assert( num0 < m_node_size && num1 < m_node_size ); const uint& root0 = RootOfNode( num0 ); const uint& root1 = RootOfNode( num1 ); assert( root0 == root1 ); return m_w[num1] - m_w[num0]; } template <typename T> inline const uint& UnionFindForest<T>::SizeOfNode() const noexcept { return m_node_size; } template <typename T> inline const uint& UnionFindForest<T>::SizeOfRoot() const noexcept { return m_root_size; } template <typename T> bool UnionFindForest<T>::Graft( const uint& num0 , const uint& num1 , const T& w ) { assert( num0 < m_node_size && num1 < m_node_size ); const uint& root0 = RootOfNode( num0 ); const uint& root1 = RootOfNode( num1 ); if( root0 == root1 ){ return Weight( num0 , num1 ) == w; } uint& height0 = m_height[root0]; const uint& height1 = m_height[root1]; const uint* p_removed_root; const uint* p_removed_node; const uint* p_kept_root; if( height0 < height1 ){ p_removed_root = &root0; p_removed_node = &num0; p_kept_root = &root1; m_w[*p_removed_root] -= w - m_w[num1] + m_w[num0]; } else { if( height0 == height1 ){ height0++; } p_removed_root = &root1; p_removed_node = &num1; p_kept_root = &root0; m_w[*p_removed_root] += w - m_w[num1] + m_w[num0]; } if( *p_removed_node != *p_removed_root ){ m_w[*p_removed_node] += m_w[*p_removed_root]; } m_pred[*p_removed_node] = m_pred[*p_removed_root] = *p_kept_root; m_root_size--; return 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" ); }