結果
問題 | No.2293 無向辺 2-SAT |
ユーザー | rlangevin |
提出日時 | 2023-05-06 01:37:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,127 ms / 4,000 ms |
コード長 | 2,217 bytes |
コンパイル時間 | 313 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 118,108 KB |
最終ジャッジ日時 | 2024-05-02 19:59:34 |
合計ジャッジ時間 | 37,866 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,480 KB |
testcase_01 | AC | 46 ms
64,768 KB |
testcase_02 | AC | 35 ms
52,736 KB |
testcase_03 | AC | 1,127 ms
118,108 KB |
testcase_04 | AC | 296 ms
84,096 KB |
testcase_05 | AC | 264 ms
81,152 KB |
testcase_06 | AC | 259 ms
81,280 KB |
testcase_07 | AC | 120 ms
82,944 KB |
testcase_08 | AC | 266 ms
84,096 KB |
testcase_09 | AC | 256 ms
84,012 KB |
testcase_10 | AC | 259 ms
84,636 KB |
testcase_11 | AC | 294 ms
84,224 KB |
testcase_12 | AC | 276 ms
83,840 KB |
testcase_13 | AC | 271 ms
79,156 KB |
testcase_14 | AC | 354 ms
80,136 KB |
testcase_15 | AC | 536 ms
82,108 KB |
testcase_16 | AC | 284 ms
83,968 KB |
testcase_17 | AC | 315 ms
89,216 KB |
testcase_18 | AC | 287 ms
83,840 KB |
testcase_19 | AC | 188 ms
80,256 KB |
testcase_20 | AC | 638 ms
89,384 KB |
testcase_21 | AC | 743 ms
90,156 KB |
testcase_22 | AC | 641 ms
93,300 KB |
testcase_23 | AC | 587 ms
91,832 KB |
testcase_24 | AC | 618 ms
92,696 KB |
testcase_25 | AC | 581 ms
91,116 KB |
testcase_26 | AC | 622 ms
92,340 KB |
testcase_27 | AC | 618 ms
92,168 KB |
testcase_28 | AC | 624 ms
92,956 KB |
testcase_29 | AC | 624 ms
91,888 KB |
testcase_30 | AC | 741 ms
93,000 KB |
testcase_31 | AC | 728 ms
93,236 KB |
testcase_32 | AC | 780 ms
91,572 KB |
testcase_33 | AC | 787 ms
92,504 KB |
testcase_34 | AC | 798 ms
91,716 KB |
testcase_35 | AC | 796 ms
91,916 KB |
testcase_36 | AC | 809 ms
92,208 KB |
testcase_37 | AC | 774 ms
92,000 KB |
testcase_38 | AC | 766 ms
90,876 KB |
testcase_39 | AC | 764 ms
91,448 KB |
testcase_40 | AC | 794 ms
91,492 KB |
testcase_41 | AC | 376 ms
86,664 KB |
testcase_42 | AC | 587 ms
88,948 KB |
testcase_43 | AC | 762 ms
91,820 KB |
testcase_44 | AC | 836 ms
91,936 KB |
testcase_45 | AC | 846 ms
92,532 KB |
testcase_46 | AC | 805 ms
91,428 KB |
testcase_47 | AC | 697 ms
89,640 KB |
testcase_48 | AC | 661 ms
89,704 KB |
testcase_49 | AC | 626 ms
89,252 KB |
testcase_50 | AC | 605 ms
88,968 KB |
testcase_51 | AC | 581 ms
88,528 KB |
testcase_52 | AC | 583 ms
88,968 KB |
testcase_53 | AC | 538 ms
90,820 KB |
testcase_54 | AC | 565 ms
92,500 KB |
testcase_55 | AC | 546 ms
94,976 KB |
ソースコード
import sys input = sys.stdin.readline class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] self.diff_weight = [0 for _ in range(n)] self.flag = 1 def find(self, x): if self.par[x] == x: return x else: root = self.find(self.par[x]) self.diff_weight[x] ^= self.diff_weight[self.par[x]] self.par[x] = root return self.par[x] def union(self, x, y, w): rx = self.find(x) ry = self.find(y) w ^= self.diff_weight[y]^self.diff_weight[x] if rx != ry: if self.rank[rx] < self.rank[ry]: rx, ry = ry, rx if self.rank[rx] == self.rank[ry]: self.rank[rx] += 1 self.par[ry] = rx self.size[rx] += self.size[ry] self.diff_weight[ry] = w else: if w: self.flag = 0 def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] def diff(self, x, y): return self.diff_weight[self.find(y)]^self.diff_weight[self.find(x)] N, Q = map(int, input().split()) mod = 998244353 N2 = pow(2, N, mod) ans = N2 inv = pow(2, mod - 2, mod) U = UnionFind(N) stack = set() for _ in range(Q): query = list(map(int, input().split())) if query[0] == 1: u, v = query[1:] u, v = u - 1, v - 1 stack.add(u) stack.add(v) if not U.is_same(u, v): ans *= inv ans %= mod U.union(u, v, 0) elif query[0] == 2: u, v = query[1:] u, v = u - 1, v - 1 stack.add(u) stack.add(v) if not U.is_same(u, v): ans *= inv ans %= mod U.union(u, v, 1) else: ans = N2 U.flag = 1 for s in stack: U.diff_weight[s] = 0 U.par[s] = s U.rank[s] = 0 U.size[s] = 1 stack = set() if U.flag: print(ans) else: print(0)