結果

問題 No.2293 無向辺 2-SAT
ユーザー hikikomorihikikomori
提出日時 2023-03-30 03:39:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 541 ms / 4,000 ms
コード長 3,493 bytes
コンパイル時間 8,002 ms
コンパイル使用メモリ 312,632 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-05-02 03:55:24
合計ジャッジ時間 41,112 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 4 ms
5,248 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 541 ms
11,392 KB
testcase_04 AC 503 ms
5,376 KB
testcase_05 AC 484 ms
5,376 KB
testcase_06 AC 475 ms
5,376 KB
testcase_07 AC 312 ms
5,376 KB
testcase_08 AC 480 ms
5,376 KB
testcase_09 AC 435 ms
5,376 KB
testcase_10 AC 445 ms
5,376 KB
testcase_11 AC 511 ms
5,376 KB
testcase_12 AC 481 ms
5,376 KB
testcase_13 AC 441 ms
5,376 KB
testcase_14 AC 431 ms
5,376 KB
testcase_15 AC 418 ms
5,376 KB
testcase_16 AC 496 ms
5,376 KB
testcase_17 AC 518 ms
5,376 KB
testcase_18 AC 490 ms
5,376 KB
testcase_19 AC 246 ms
5,376 KB
testcase_20 AC 502 ms
5,376 KB
testcase_21 AC 500 ms
5,376 KB
testcase_22 AC 496 ms
5,376 KB
testcase_23 AC 514 ms
5,376 KB
testcase_24 AC 518 ms
5,376 KB
testcase_25 AC 506 ms
5,376 KB
testcase_26 AC 529 ms
5,376 KB
testcase_27 AC 509 ms
5,376 KB
testcase_28 AC 508 ms
5,376 KB
testcase_29 AC 500 ms
5,376 KB
testcase_30 AC 503 ms
5,376 KB
testcase_31 AC 509 ms
5,376 KB
testcase_32 AC 483 ms
5,376 KB
testcase_33 AC 467 ms
5,376 KB
testcase_34 AC 462 ms
5,376 KB
testcase_35 AC 466 ms
5,376 KB
testcase_36 AC 483 ms
5,376 KB
testcase_37 AC 487 ms
5,376 KB
testcase_38 AC 467 ms
5,376 KB
testcase_39 AC 469 ms
5,376 KB
testcase_40 AC 474 ms
5,376 KB
testcase_41 AC 442 ms
5,376 KB
testcase_42 AC 454 ms
5,376 KB
testcase_43 AC 464 ms
5,376 KB
testcase_44 AC 472 ms
5,376 KB
testcase_45 AC 500 ms
5,376 KB
testcase_46 AC 501 ms
5,376 KB
testcase_47 AC 498 ms
5,376 KB
testcase_48 AC 504 ms
5,376 KB
testcase_49 AC 501 ms
5,376 KB
testcase_50 AC 495 ms
5,376 KB
testcase_51 AC 493 ms
5,376 KB
testcase_52 AC 501 ms
5,376 KB
testcase_53 AC 511 ms
5,376 KB
testcase_54 AC 510 ms
5,376 KB
testcase_55 AC 514 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using Graph = vector<vector<ll>>;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<long long, long long>;
using vpll = vector<pll>;
using mint = modint998244353;
const long double EPS = 1e-18;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); i++)
#define rep(i, n) for (ll i = (0); i < (ll)(n); i++)
#define rrep(i, n) for (ll i = (1); i < (ll)(n + 1); i++)
#define repd(i, n) for (ll i = n - 1; i >= 0; i--)
#define rrepd(i, n) for (ll i = n; i >= 1; i--)
#define ALL(n) begin(n), end(n)
#define IN(a, x, b) (a <= x && x < b)
#define INIT                          \
    std::ios::sync_with_stdio(false); \
    std::cin.tie(0);
template <class T>
inline T CHMAX(T& a, const T b) {
    return a = (a < b) ? b : a;
}
template <class T>
inline T CHMIN(T& a, const T b) {
    return a = (a > b) ? b : a;
}
struct RollbackUnionFind {
    vector<int> data;
    stack<pair<int, int>> history;
    int inner_snap;

    RollbackUnionFind(int sz) : inner_snap(0) { data.assign(sz, -1); }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        history.emplace(x, data[x]);
        history.emplace(y, data[y]);
        if (x == y) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }

    int find(int k) {
        if (data[k] < 0) return k;
        return find(data[k]);
    }

    int same(int x, int y) { return find(x) == find(y); }

    int size(int k) { return (-data[find(k)]); }

    void undo() {
        data[history.top().first] = history.top().second;
        history.pop();
        data[history.top().first] = history.top().second;
        history.pop();
    }

    void snapshot() { inner_snap = int(history.size() >> 1); }

    int get_state() { return int(history.size() >> 1); }

    void rollback(int state = -1) {
        if (state == -1) state = inner_snap;
        state <<= 1;
        assert(state <= (int)history.size());
        while (state < (int)history.size()) undo();
    }
};

/**
 * @brief RollbackつきUnion Find
 * @docs docs/data-structure/rollback-union-find.md
 */
int main() {
    ll N, Q;
    cin >> N >> Q;

    RollbackUnionFind uf(4e5 + 50);
    uf.snapshot();
    mint ans = 0;
    mint def = mint(2).pow(N);
    ans = def;
    while (Q--) {
        ll query;
        cin >> query;
        if (query == 1) {
            ll x, y;
            cin >> x >> y;
            if (!uf.same(x, y)) {
                ans /= 2;
            }
            uf.unite(x, y);
            uf.unite(x + N, y + N);
            if (uf.same(x, x + N) || uf.same(y, y + N)) {
                ans = 0;
            }
        } else if (query == 2) {
            ll x, y;
            cin >> x >> y;
            if (!uf.same(x, y + N)) {
                ans /= 2;
            }
            uf.unite(x, y + N);
            uf.unite(x + N, y);
            if (uf.same(x, x + N) || uf.same(y, y + N)) {
                ans = 0;
            }
        } else {
            uf.rollback(-1);
            ans = def;
        }
        cout << ans.val() << endl;
    }
}
0