結果

問題 No.2293 無向辺 2-SAT
ユーザー SSRSSSRS
提出日時 2023-05-05 22:04:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 467 ms / 4,000 ms
コード長 1,547 bytes
コンパイル時間 2,615 ms
コンパイル使用メモリ 170,964 KB
実行使用メモリ 9,464 KB
最終ジャッジ日時 2023-08-15 04:08:33
合計ジャッジ時間 32,150 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,600 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 467 ms
9,464 KB
testcase_04 AC 446 ms
4,816 KB
testcase_05 AC 422 ms
4,380 KB
testcase_06 AC 421 ms
4,384 KB
testcase_07 AC 302 ms
4,548 KB
testcase_08 AC 433 ms
4,556 KB
testcase_09 AC 408 ms
4,652 KB
testcase_10 AC 405 ms
4,708 KB
testcase_11 AC 445 ms
4,700 KB
testcase_12 AC 451 ms
4,712 KB
testcase_13 AC 395 ms
4,384 KB
testcase_14 AC 387 ms
4,380 KB
testcase_15 AC 366 ms
4,384 KB
testcase_16 AC 446 ms
4,768 KB
testcase_17 AC 467 ms
5,016 KB
testcase_18 AC 453 ms
4,720 KB
testcase_19 AC 215 ms
4,388 KB
testcase_20 AC 454 ms
4,556 KB
testcase_21 AC 457 ms
4,720 KB
testcase_22 AC 460 ms
5,032 KB
testcase_23 AC 467 ms
4,844 KB
testcase_24 AC 452 ms
5,192 KB
testcase_25 AC 456 ms
5,016 KB
testcase_26 AC 449 ms
4,840 KB
testcase_27 AC 453 ms
4,960 KB
testcase_28 AC 457 ms
5,012 KB
testcase_29 AC 446 ms
4,880 KB
testcase_30 AC 449 ms
4,848 KB
testcase_31 AC 453 ms
4,876 KB
testcase_32 AC 435 ms
4,716 KB
testcase_33 AC 434 ms
4,592 KB
testcase_34 AC 424 ms
4,588 KB
testcase_35 AC 431 ms
4,548 KB
testcase_36 AC 429 ms
4,608 KB
testcase_37 AC 425 ms
4,592 KB
testcase_38 AC 436 ms
4,548 KB
testcase_39 AC 421 ms
4,884 KB
testcase_40 AC 424 ms
4,708 KB
testcase_41 AC 415 ms
4,652 KB
testcase_42 AC 430 ms
4,632 KB
testcase_43 AC 428 ms
4,648 KB
testcase_44 AC 438 ms
4,648 KB
testcase_45 AC 434 ms
4,556 KB
testcase_46 AC 437 ms
4,644 KB
testcase_47 AC 443 ms
4,716 KB
testcase_48 AC 448 ms
4,552 KB
testcase_49 AC 447 ms
4,600 KB
testcase_50 AC 439 ms
4,560 KB
testcase_51 AC 448 ms
5,040 KB
testcase_52 AC 455 ms
4,928 KB
testcase_53 AC 451 ms
4,944 KB
testcase_54 AC 456 ms
5,024 KB
testcase_55 AC 457 ms
5,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long HALF = 499122177;
struct unionfind{
  vector<int> p;
  vector<int> id;
  unionfind(int N){
    p = vector<int>(N, -1);
  }
  int root(int x){
    if (p[x] < 0){
      return x;
    } else {
      p[x] = root(p[x]);
      return p[x];
    }
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    id.push_back(x);
    id.push_back(y);
    x = root(x);
    y = root(y);
    if (x != y){
      if (p[x] < p[y]){
        swap(x, y);
      }
      p[y] += p[x];
      p[x] = y;
    }
  }
  void reset(){
    for (int v : id){
      p[v] = -1;
    }
    id.clear();
  }
};
int main(){
  int N, Q;
  cin >> N >> Q;
  long long a = 1;
  for (int i = 0; i < N; i++){
    a *= 2;
    a %= MOD;
  }
  unionfind UF(N * 2);
  long long ans = a;
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 1){
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      if (UF.same(u, N + v)){
        ans = 0;
      } else if (!UF.same(u, v)){
        ans *= HALF;
        ans %= MOD;
        UF.unite(u, v);
        UF.unite(N + u, N + v);
      }
    }
    if (t == 2){
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      if (UF.same(u, v)){
        ans = 0;
      } else if (!UF.same(u, N + v)){
        ans *= HALF;
        ans %= MOD;
        UF.unite(u, N + v);
        UF.unite(N + u, v);
      }
    }
    if (t == 3){
      ans = a;
      UF.reset();
    }
    cout << ans << endl;
  }
}
0