結果

問題 No.2293 無向辺 2-SAT
ユーザー milanis48663220milanis48663220
提出日時 2023-05-05 22:14:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,088 ms / 4,000 ms
コード長 3,252 bytes
コンパイル時間 1,553 ms
コンパイル使用メモリ 128,432 KB
実行使用メモリ 19,696 KB
最終ジャッジ日時 2024-05-02 16:42:23
合計ジャッジ時間 35,924 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 21 ms
15,744 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2,088 ms
19,696 KB
testcase_04 AC 483 ms
15,744 KB
testcase_05 AC 442 ms
9,472 KB
testcase_06 AC 439 ms
9,344 KB
testcase_07 AC 300 ms
15,616 KB
testcase_08 AC 484 ms
15,744 KB
testcase_09 AC 439 ms
15,616 KB
testcase_10 AC 433 ms
15,744 KB
testcase_11 AC 493 ms
15,744 KB
testcase_12 AC 475 ms
15,744 KB
testcase_13 AC 339 ms
5,376 KB
testcase_14 AC 351 ms
5,376 KB
testcase_15 AC 333 ms
5,376 KB
testcase_16 AC 492 ms
15,616 KB
testcase_17 AC 523 ms
16,000 KB
testcase_18 AC 490 ms
15,616 KB
testcase_19 AC 233 ms
9,472 KB
testcase_20 AC 499 ms
17,024 KB
testcase_21 AC 467 ms
17,024 KB
testcase_22 AC 509 ms
17,024 KB
testcase_23 AC 507 ms
17,024 KB
testcase_24 AC 502 ms
16,896 KB
testcase_25 AC 506 ms
16,896 KB
testcase_26 AC 517 ms
17,024 KB
testcase_27 AC 510 ms
17,024 KB
testcase_28 AC 496 ms
16,896 KB
testcase_29 AC 503 ms
17,024 KB
testcase_30 AC 512 ms
16,896 KB
testcase_31 AC 516 ms
16,896 KB
testcase_32 AC 428 ms
16,512 KB
testcase_33 AC 423 ms
16,512 KB
testcase_34 AC 426 ms
16,512 KB
testcase_35 AC 421 ms
16,384 KB
testcase_36 AC 422 ms
16,384 KB
testcase_37 AC 424 ms
16,384 KB
testcase_38 AC 421 ms
16,512 KB
testcase_39 AC 414 ms
16,384 KB
testcase_40 AC 430 ms
16,384 KB
testcase_41 AC 362 ms
15,616 KB
testcase_42 AC 378 ms
15,616 KB
testcase_43 AC 414 ms
16,256 KB
testcase_44 AC 441 ms
16,640 KB
testcase_45 AC 453 ms
16,640 KB
testcase_46 AC 457 ms
16,768 KB
testcase_47 AC 468 ms
17,024 KB
testcase_48 AC 476 ms
17,024 KB
testcase_49 AC 483 ms
17,024 KB
testcase_50 AC 477 ms
17,024 KB
testcase_51 AC 479 ms
16,896 KB
testcase_52 AC 495 ms
16,768 KB
testcase_53 AC 499 ms
16,768 KB
testcase_54 AC 513 ms
16,640 KB
testcase_55 AC 515 ms
16,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <stack>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint998244353;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, q; cin >> n >> q;
    vector<int> data(n, -1);
    vector<int> parity(n);
    vector<vector<int>> components(n);
    for(int i = 0; i < n; i++) components[i].push_back(i);
    function<int(int)> root = [&](int x) {
        return data[x] < 0 ? x : root(data[x]);
    };
    function<int(int)> size = [&](int x) {
        return -data[root(x)];
    };
    auto unite = [&](int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
            for(int v: components[y]){
                components[x].push_back(v);
            }
            components[y].clear();
        }
        return x != y;
    };
    auto same = [&](int x, int y) {
        return root(x) == root(y);
    };
    mint ans = mint(2).pow(n);
    vector<int> buf;
    auto clear = [&](){
        for(int v: buf){
            parity[v] = 0;
            data[v] = -1;
            components[v] = {v};
        }
        ans = mint(2).pow(n);
        buf.clear();
    };
    while(q--){
        int t; cin >> t;
        if(t == 1 || t == 2){
            int p = t-1;
            int u, v; cin >> u >> v; u--; v--;
            if(same(u, v)){
                if((parity[u]^p) != parity[v]){
                    ans = 0;
                }
            }else{
                int us = size(u), vs = size(v);
                if(us > vs) swap(us, vs);
                if((parity[u]^p) != parity[v]){
                    int ur = root(u);
                    for(int x: components[ur]) parity[x] ^= 1;
                }
                ans /= 2;
                unite(u, v);
            }
            buf.push_back(u);
            buf.push_back(v);
        }else{
            clear();
        }
        cout << ans << endl;
    }
}
0