結果

問題 No.2293 無向辺 2-SAT
ユーザー ぷらぷら
提出日時 2023-05-05 22:04:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,303 bytes
コンパイル時間 2,477 ms
コンパイル使用メモリ 211,324 KB
実行使用メモリ 20,368 KB
最終ジャッジ日時 2024-05-02 16:19:07
合計ジャッジ時間 14,112 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 20 ms
16,512 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 139 ms
16,384 KB
testcase_05 AC 102 ms
9,856 KB
testcase_06 AC 98 ms
9,856 KB
testcase_07 AC 58 ms
16,384 KB
testcase_08 AC 130 ms
16,512 KB
testcase_09 AC 120 ms
16,512 KB
testcase_10 AC 121 ms
16,640 KB
testcase_11 AC 134 ms
16,384 KB
testcase_12 AC 133 ms
16,512 KB
testcase_13 AC 69 ms
5,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 139 ms
16,512 KB
testcase_17 AC 144 ms
16,896 KB
testcase_18 AC 133 ms
16,512 KB
testcase_19 AC 57 ms
9,856 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int mod = 998244353;

long long modpow(long long a,long long b) {
    long long ans = 1;
    while(b) {
        if(b & 1) {
            (ans *= a) %= mod;
        }
        (a *= a) %= mod;
        b /= 2;
    }
    return ans;
}

struct UnionFind {
    vector<int> par;
    vector<int> size;
    vector<vector<int>>cld;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        cld.resize(n);
        for(int i = 0; i < n; i++) {
            par[i] = i;
            cld[i].push_back(i);
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    bool unite(int x,int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return false;
        }
        if(size[x] > size[y]) {
            swap(x,y);
        }
        par[x] = y;
        size[y] += size[x];
        for(auto i:cld[x]) {
            cld[y].push_back(i);
        }
        return true;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,Q;
    cin >> N >> Q;
    UnionFind uf(N);
    vector<int>r(N,-1);
    int cnt = N;
    bool ok = true;
    vector<int>tmp;
    for(int i = 0; i < Q; i++) {
        int f;
        cin >> f;
        if(f == 1) {
            int u,v;
            cin >> u >> v;
            u--;
            v--;
            tmp.push_back(u);
            tmp.push_back(v);
            if(!uf.same(u,v)) {
                cnt--;
                if(uf.size[u] < uf.size[v]) {
                    swap(u,v);
                }
                if(r[u] == -1) {
                    r[u] = 0;
                }
                for(int j:uf.cld[v]) {
                    r[j] = r[u];
                }
                uf.unite(u,v);
            }
            else if(r[u] != r[v]) {
                ok = false;
            }
            if(ok) {
                cout << modpow(2,cnt) << "\n";
            }
            else {
                cout << 0 << "\n";
            }
        }
        if(f == 2) {
            int u,v;
            cin >> u >> v;
            u--;
            v--;
            tmp.push_back(u);
            tmp.push_back(v);
            if(!uf.same(u,v)) {
                cnt--;
                if(uf.size[u] < uf.size[v]) {
                    swap(u,v);
                }
                if(r[u] == -1) {
                    r[u] = 0;
                }
                for(int j:uf.cld[v]) {
                    r[j] = !r[u];
                }
                uf.unite(u,v);
            }
            else if(r[u] == r[v]) {
                ok = false;
            }
            if(ok) {
                cout << modpow(2,cnt) << "\n";
            }
            else {
                cout << 0 << "\n";
            }
        }
        if(f == 3) {
            cnt = N;
            ok = true;
            for(int j:tmp) {
                uf.par[j] = j;
                uf.size[j] = 1;
                uf.cld[j].clear();
                r[j] = -1;
            }
            tmp.clear();
            cout << modpow(2,N) << "\n";
        }
    }
}
0