結果
問題 | No.2293 無向辺 2-SAT |
ユーザー | rlangevin |
提出日時 | 2023-05-06 01:37:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,177 ms / 4,000 ms |
コード長 | 2,217 bytes |
コンパイル時間 | 428 ms |
コンパイル使用メモリ | 82,408 KB |
実行使用メモリ | 117,452 KB |
最終ジャッジ日時 | 2024-11-23 14:05:35 |
合計ジャッジ時間 | 38,646 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
52,608 KB |
testcase_01 | AC | 47 ms
64,640 KB |
testcase_02 | AC | 37 ms
52,096 KB |
testcase_03 | AC | 1,177 ms
117,452 KB |
testcase_04 | AC | 288 ms
83,840 KB |
testcase_05 | AC | 272 ms
80,896 KB |
testcase_06 | AC | 262 ms
80,768 KB |
testcase_07 | AC | 116 ms
82,688 KB |
testcase_08 | AC | 269 ms
83,948 KB |
testcase_09 | AC | 263 ms
84,252 KB |
testcase_10 | AC | 262 ms
84,892 KB |
testcase_11 | AC | 303 ms
84,256 KB |
testcase_12 | AC | 286 ms
83,840 KB |
testcase_13 | AC | 288 ms
78,640 KB |
testcase_14 | AC | 395 ms
80,012 KB |
testcase_15 | AC | 564 ms
81,724 KB |
testcase_16 | AC | 300 ms
83,840 KB |
testcase_17 | AC | 339 ms
89,216 KB |
testcase_18 | AC | 297 ms
83,968 KB |
testcase_19 | AC | 193 ms
80,256 KB |
testcase_20 | AC | 663 ms
89,708 KB |
testcase_21 | AC | 841 ms
90,496 KB |
testcase_22 | AC | 689 ms
93,428 KB |
testcase_23 | AC | 623 ms
91,828 KB |
testcase_24 | AC | 646 ms
92,056 KB |
testcase_25 | AC | 614 ms
91,032 KB |
testcase_26 | AC | 654 ms
92,340 KB |
testcase_27 | AC | 646 ms
91,780 KB |
testcase_28 | AC | 673 ms
92,576 KB |
testcase_29 | AC | 664 ms
91,636 KB |
testcase_30 | AC | 675 ms
93,252 KB |
testcase_31 | AC | 644 ms
93,248 KB |
testcase_32 | AC | 840 ms
91,060 KB |
testcase_33 | AC | 846 ms
92,500 KB |
testcase_34 | AC | 830 ms
91,720 KB |
testcase_35 | AC | 843 ms
92,432 KB |
testcase_36 | AC | 843 ms
92,472 KB |
testcase_37 | AC | 841 ms
92,240 KB |
testcase_38 | AC | 839 ms
91,136 KB |
testcase_39 | AC | 810 ms
91,320 KB |
testcase_40 | AC | 849 ms
91,372 KB |
testcase_41 | AC | 383 ms
86,544 KB |
testcase_42 | AC | 624 ms
88,948 KB |
testcase_43 | AC | 828 ms
91,692 KB |
testcase_44 | AC | 857 ms
91,872 KB |
testcase_45 | AC | 839 ms
92,404 KB |
testcase_46 | AC | 847 ms
91,116 KB |
testcase_47 | AC | 777 ms
89,892 KB |
testcase_48 | AC | 721 ms
89,576 KB |
testcase_49 | AC | 673 ms
89,128 KB |
testcase_50 | AC | 660 ms
88,968 KB |
testcase_51 | AC | 639 ms
88,528 KB |
testcase_52 | AC | 609 ms
88,968 KB |
testcase_53 | AC | 601 ms
90,224 KB |
testcase_54 | AC | 617 ms
92,496 KB |
testcase_55 | AC | 576 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)