結果

問題 No.2293 無向辺 2-SAT
ユーザー t98slidert98slider
提出日時 2023-02-12 01:43:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 4,000 ms
コード長 1,901 bytes
コンパイル時間 1,655 ms
コンパイル使用メモリ 173,980 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-05-02 03:36:50
合計ジャッジ時間 12,160 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,504 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 82 ms
8,192 KB
testcase_04 AC 72 ms
5,632 KB
testcase_05 AC 70 ms
5,376 KB
testcase_06 AC 65 ms
5,376 KB
testcase_07 AC 24 ms
5,632 KB
testcase_08 AC 65 ms
5,632 KB
testcase_09 AC 59 ms
5,504 KB
testcase_10 AC 59 ms
5,504 KB
testcase_11 AC 70 ms
5,632 KB
testcase_12 AC 69 ms
5,632 KB
testcase_13 AC 58 ms
5,376 KB
testcase_14 AC 56 ms
5,376 KB
testcase_15 AC 59 ms
5,376 KB
testcase_16 AC 71 ms
5,632 KB
testcase_17 AC 71 ms
5,760 KB
testcase_18 AC 74 ms
5,632 KB
testcase_19 AC 35 ms
5,376 KB
testcase_20 AC 76 ms
5,760 KB
testcase_21 AC 72 ms
5,504 KB
testcase_22 AC 76 ms
5,632 KB
testcase_23 AC 77 ms
5,632 KB
testcase_24 AC 74 ms
5,504 KB
testcase_25 AC 74 ms
5,504 KB
testcase_26 AC 76 ms
5,632 KB
testcase_27 AC 79 ms
5,632 KB
testcase_28 AC 75 ms
5,504 KB
testcase_29 AC 77 ms
5,632 KB
testcase_30 AC 79 ms
5,504 KB
testcase_31 AC 76 ms
5,632 KB
testcase_32 AC 70 ms
5,632 KB
testcase_33 AC 71 ms
5,504 KB
testcase_34 AC 71 ms
5,504 KB
testcase_35 AC 72 ms
5,504 KB
testcase_36 AC 73 ms
5,504 KB
testcase_37 AC 72 ms
5,504 KB
testcase_38 AC 73 ms
5,632 KB
testcase_39 AC 73 ms
5,632 KB
testcase_40 AC 71 ms
5,632 KB
testcase_41 AC 66 ms
5,632 KB
testcase_42 AC 69 ms
5,504 KB
testcase_43 AC 72 ms
5,504 KB
testcase_44 AC 73 ms
5,504 KB
testcase_45 AC 73 ms
5,632 KB
testcase_46 AC 76 ms
5,632 KB
testcase_47 AC 78 ms
5,504 KB
testcase_48 AC 77 ms
5,504 KB
testcase_49 AC 78 ms
5,632 KB
testcase_50 AC 77 ms
5,504 KB
testcase_51 AC 76 ms
5,632 KB
testcase_52 AC 76 ms
5,632 KB
testcase_53 AC 75 ms
5,632 KB
testcase_54 AC 78 ms
5,760 KB
testcase_55 AC 79 ms
5,760 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] << 1;
        if(pow2[i + 1] >= 998244353) pow2[i + 1] -= 998244353;
    }

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

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

    auto merge = [&](int u, int v){ //頂点uと頂点vを重みwの辺でマージ
        int x = leader(u), y = leader(v);
        if(x == y) 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;
    };

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

    int type, u, v;
    while(Q--){
        cin >> type;
        if(type <= 2){
            cin >> u >> v;
            u--, v--;
            if(type == 1){
                merge(u, v);
                merge(u + N, v + N);
            }else{
                merge(u, v + N);
                merge(u + N, v);
            }
            if(leader(u) == leader(u + N)) is_zero = true;
        }else{
            reset();
        }
        cout << (is_zero ? 0 : pow2[num_component >> 1]) << '\n';
    }
}
0