結果

問題 No.2293 無向辺 2-SAT
ユーザー 👑 tatyamtatyam
提出日時 2023-05-05 22:14:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 4,000 ms
コード長 2,302 bytes
コンパイル時間 6,327 ms
コンパイル使用メモリ 253,968 KB
実行使用メモリ 7,468 KB
最終ジャッジ日時 2023-08-15 04:28:55
合計ジャッジ時間 15,644 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 5 ms
5,660 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 90 ms
7,468 KB
testcase_04 AC 82 ms
5,536 KB
testcase_05 AC 69 ms
4,384 KB
testcase_06 AC 68 ms
4,384 KB
testcase_07 AC 29 ms
5,636 KB
testcase_08 AC 77 ms
5,680 KB
testcase_09 AC 71 ms
5,592 KB
testcase_10 AC 69 ms
5,548 KB
testcase_11 AC 82 ms
5,548 KB
testcase_12 AC 79 ms
5,652 KB
testcase_13 AC 57 ms
4,380 KB
testcase_14 AC 55 ms
4,380 KB
testcase_15 AC 53 ms
4,384 KB
testcase_16 AC 84 ms
5,544 KB
testcase_17 AC 81 ms
5,592 KB
testcase_18 AC 82 ms
5,752 KB
testcase_19 AC 36 ms
4,384 KB
testcase_20 AC 86 ms
5,628 KB
testcase_21 AC 87 ms
5,664 KB
testcase_22 AC 87 ms
5,540 KB
testcase_23 AC 90 ms
5,632 KB
testcase_24 AC 89 ms
5,648 KB
testcase_25 AC 85 ms
5,592 KB
testcase_26 AC 86 ms
5,580 KB
testcase_27 AC 86 ms
5,584 KB
testcase_28 AC 88 ms
5,600 KB
testcase_29 AC 86 ms
5,596 KB
testcase_30 AC 87 ms
5,648 KB
testcase_31 AC 87 ms
5,600 KB
testcase_32 AC 79 ms
5,632 KB
testcase_33 AC 82 ms
5,600 KB
testcase_34 AC 81 ms
5,636 KB
testcase_35 AC 82 ms
5,576 KB
testcase_36 AC 79 ms
5,668 KB
testcase_37 AC 80 ms
5,832 KB
testcase_38 AC 80 ms
5,628 KB
testcase_39 AC 80 ms
5,672 KB
testcase_40 AC 80 ms
5,668 KB
testcase_41 AC 69 ms
5,668 KB
testcase_42 AC 75 ms
5,708 KB
testcase_43 AC 79 ms
5,548 KB
testcase_44 AC 81 ms
5,644 KB
testcase_45 AC 84 ms
5,536 KB
testcase_46 AC 86 ms
5,632 KB
testcase_47 AC 85 ms
5,584 KB
testcase_48 AC 85 ms
5,588 KB
testcase_49 AC 88 ms
5,576 KB
testcase_50 AC 85 ms
5,732 KB
testcase_51 AC 84 ms
5,624 KB
testcase_52 AC 85 ms
5,708 KB
testcase_53 AC 88 ms
5,592 KB
testcase_54 AC 87 ms
5,636 KB
testcase_55 AC 85 ms
6,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/dsu>
#include <atcoder/modint>
using namespace std;
using mint = atcoder::modint998244353;

class potentialized_unionfind{
    /*
    Copyright (c) 2021 0214sh7
    https://github.com/0214sh7/library/
    */
    private:
    std::vector<int> UF,rank,pot;
    vector<int> history;
    public:
    
    void init(int N){
        UF.clear();
        rank.clear();
        for(int i=0;i<N;i++){
            UF.push_back(i);
            rank.push_back(0);
            pot.push_back(0);
        }
    }

    potentialized_unionfind(int N){
        init(N);
    }
    
    int root(int k){
        if(UF[k]==k){
            return k;
        }else{
            int r = root(UF[k]);
            pot[k] ^= pot[UF[k]];
            UF[k] = r;
            return UF[k];
        }
    }
    
    int potential(int k){
        root(k);
        return pot[k];
    }
    
    bool same(int p,int q){
        return root(p)==root(q);
    }
    
    bool unite(int P,int Q,bool d){
        history.push_back(P);
        history.push_back(Q);
        //pot(Q)-pot(P)=dを満たす
        d^=potential(P);
        d^=potential(Q);
        int p=root(P), q=root(Q);
        if(p==q)return false;
        if(rank[p]<rank[q]){
            std::swap(p,q);
        }
        UF[q]=p;
        if(rank[p]==rank[q])rank[p]++;
        pot[q]=d;
        
        return true;
    }
    
    int diff(int P,int Q){
        return potential(Q)^potential(P);
    }

    void clear() {
        for(int i : history) {
            UF[i]=i;
            rank[i]=0;
            pot[i]=0;
        }
        history.clear();
    }
};
int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    int N, Q;
    cin >> N >> Q;
    potentialized_unionfind uf(N);
    const mint all = mint{2}.pow(N), inv2 = 998244354 / 2;
    mint ans = all;

    while(Q--) {
        int t;
        cin >> t;
        if(t == 3) {
            uf.clear();
            ans = all;
        }
        else {
            int u, v;
            cin >> u >> v;
            u--; v--; t--;
            if(uf.same(u, v)) {
                if(uf.diff(u, v) != t) ans = 0;
            }
            else {
                uf.unite(u, v, t);
                ans *= inv2;
            }
        }
        cout << ans.val() << '\n';
    }
}
0