結果

問題 No.2293 無向辺 2-SAT
ユーザー milanis48663220milanis48663220
提出日時 2023-05-05 22:14:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,086 ms / 4,000 ms
コード長 3,252 bytes
コンパイル時間 3,221 ms
コンパイル使用メモリ 126,876 KB
実行使用メモリ 19,608 KB
最終ジャッジ日時 2023-08-15 04:31:12
合計ジャッジ時間 36,287 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 19 ms
15,760 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2,086 ms
19,608 KB
testcase_04 AC 479 ms
15,576 KB
testcase_05 AC 437 ms
9,292 KB
testcase_06 AC 429 ms
9,352 KB
testcase_07 AC 310 ms
15,408 KB
testcase_08 AC 469 ms
15,552 KB
testcase_09 AC 433 ms
15,404 KB
testcase_10 AC 425 ms
15,536 KB
testcase_11 AC 498 ms
15,636 KB
testcase_12 AC 472 ms
15,556 KB
testcase_13 AC 345 ms
4,380 KB
testcase_14 AC 342 ms
4,376 KB
testcase_15 AC 339 ms
4,376 KB
testcase_16 AC 487 ms
15,348 KB
testcase_17 AC 523 ms
15,848 KB
testcase_18 AC 480 ms
15,412 KB
testcase_19 AC 225 ms
9,532 KB
testcase_20 AC 473 ms
16,980 KB
testcase_21 AC 463 ms
16,732 KB
testcase_22 AC 530 ms
16,736 KB
testcase_23 AC 501 ms
16,984 KB
testcase_24 AC 500 ms
16,936 KB
testcase_25 AC 501 ms
16,668 KB
testcase_26 AC 507 ms
16,668 KB
testcase_27 AC 506 ms
16,664 KB
testcase_28 AC 511 ms
16,972 KB
testcase_29 AC 507 ms
16,664 KB
testcase_30 AC 516 ms
17,016 KB
testcase_31 AC 511 ms
16,724 KB
testcase_32 AC 423 ms
16,388 KB
testcase_33 AC 422 ms
16,388 KB
testcase_34 AC 418 ms
16,452 KB
testcase_35 AC 422 ms
16,412 KB
testcase_36 AC 421 ms
16,336 KB
testcase_37 AC 426 ms
16,408 KB
testcase_38 AC 422 ms
16,412 KB
testcase_39 AC 426 ms
16,408 KB
testcase_40 AC 427 ms
16,504 KB
testcase_41 AC 359 ms
15,540 KB
testcase_42 AC 390 ms
15,776 KB
testcase_43 AC 416 ms
16,276 KB
testcase_44 AC 439 ms
16,504 KB
testcase_45 AC 445 ms
16,600 KB
testcase_46 AC 466 ms
16,940 KB
testcase_47 AC 468 ms
16,896 KB
testcase_48 AC 479 ms
16,920 KB
testcase_49 AC 482 ms
16,924 KB
testcase_50 AC 481 ms
16,664 KB
testcase_51 AC 492 ms
16,668 KB
testcase_52 AC 496 ms
16,772 KB
testcase_53 AC 503 ms
16,672 KB
testcase_54 AC 514 ms
16,580 KB
testcase_55 AC 515 ms
16,308 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