結果

問題 No.2293 無向辺 2-SAT
ユーザー t98slidert98slider
提出日時 2023-02-10 07:18:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 347 ms / 4,000 ms
コード長 1,961 bytes
コンパイル時間 3,651 ms
コンパイル使用メモリ 170,964 KB
実行使用メモリ 6,724 KB
最終ジャッジ日時 2023-08-14 15:34:03
合計ジャッジ時間 26,816 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
5,336 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 345 ms
6,724 KB
testcase_04 AC 339 ms
5,316 KB
testcase_05 AC 323 ms
4,388 KB
testcase_06 AC 323 ms
4,388 KB
testcase_07 AC 275 ms
5,312 KB
testcase_08 AC 333 ms
5,336 KB
testcase_09 AC 316 ms
5,340 KB
testcase_10 AC 316 ms
5,260 KB
testcase_11 AC 334 ms
5,376 KB
testcase_12 AC 333 ms
5,336 KB
testcase_13 AC 307 ms
4,380 KB
testcase_14 AC 306 ms
4,384 KB
testcase_15 AC 297 ms
4,380 KB
testcase_16 AC 339 ms
5,264 KB
testcase_17 AC 338 ms
5,336 KB
testcase_18 AC 333 ms
5,268 KB
testcase_19 AC 163 ms
4,384 KB
testcase_20 AC 339 ms
5,408 KB
testcase_21 AC 336 ms
5,376 KB
testcase_22 AC 339 ms
5,264 KB
testcase_23 AC 336 ms
5,280 KB
testcase_24 AC 338 ms
5,280 KB
testcase_25 AC 335 ms
5,280 KB
testcase_26 AC 339 ms
5,284 KB
testcase_27 AC 337 ms
5,344 KB
testcase_28 AC 347 ms
5,320 KB
testcase_29 AC 344 ms
5,260 KB
testcase_30 AC 339 ms
5,480 KB
testcase_31 AC 340 ms
5,444 KB
testcase_32 AC 334 ms
5,276 KB
testcase_33 AC 333 ms
5,340 KB
testcase_34 AC 323 ms
5,316 KB
testcase_35 AC 334 ms
5,412 KB
testcase_36 AC 331 ms
5,388 KB
testcase_37 AC 326 ms
5,268 KB
testcase_38 AC 328 ms
5,592 KB
testcase_39 AC 329 ms
5,408 KB
testcase_40 AC 323 ms
5,276 KB
testcase_41 AC 320 ms
5,376 KB
testcase_42 AC 320 ms
5,344 KB
testcase_43 AC 325 ms
5,520 KB
testcase_44 AC 330 ms
5,448 KB
testcase_45 AC 333 ms
5,284 KB
testcase_46 AC 335 ms
5,460 KB
testcase_47 AC 342 ms
5,316 KB
testcase_48 AC 335 ms
5,404 KB
testcase_49 AC 334 ms
5,268 KB
testcase_50 AC 330 ms
5,436 KB
testcase_51 AC 336 ms
5,376 KB
testcase_52 AC 340 ms
5,580 KB
testcase_53 AC 345 ms
5,284 KB
testcase_54 AC 343 ms
5,376 KB
testcase_55 AC 341 ms
5,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q;
    cin >> N >> Q;

    //2のべき乗を求める
    vector<int> pow2(N + 1);
    pow2[0] = 1;
    for(int i = 0; i < N; i++){
        pow2[i + 1] = pow2[i] + pow2[i];
        if(pow2[i + 1] >= 998244353) pow2[i + 1] -= 998244353;
    }

    //Union Find
    bool is_zero = false; //0通りか否か
    int num_component = N; //連結成分の数
    vector<int> parent_or_size(N, -1); //連結成分のサイズ_or_親の頂点
    vector<int> diff(N); //連結成分の代表元からの偶奇
    stack<int> st; //頂点初期化用スタック

    function<int(int)> leader = [&](int v){ //連結成分の代表元の頂点を収得しながら経路圧縮
        if(parent_or_size[v] < 0) return v;
        int root = leader(parent_or_size[v]);
        diff[v] ^= diff[parent_or_size[v]];
        return parent_or_size[v] = root;
    };

    auto merge = [&](int u, int v, int w){ //頂点uと頂点vを重みwの辺でマージ
        int x = leader(u), y = leader(v);
        w ^= diff[u] ^ diff[v];
        if(x == y){
            if(w != (diff[x] ^ diff[y])) is_zero = true;
            return;
        }
        num_component--;
        st.push(x), st.push(y);
        if(-parent_or_size[x] < -parent_or_size[y]) swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        diff[y] = w;
    };

    auto reset = [&](){ //初期化
        is_zero = false;
        num_component = N;
        while(!st.empty()){
            parent_or_size[st.top()] = -1;
            diff[st.top()] = 0;
            st.pop();
        }
    };

    int type, u, v;
    while(Q--){
        cin >> type;
        if(type <= 2){
            cin >> u >> v;
            merge(--u, --v, --type);
        }else{
            reset();
        }
        cout << (is_zero ? 0 : pow2[num_component]) << endl;
    }
}
0