結果

問題 No.2293 無向辺 2-SAT
ユーザー t98slidert98slider
提出日時 2023-03-25 08:14:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,085 ms / 4,000 ms
コード長 1,676 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 31,104 KB
最終ジャッジ日時 2024-05-02 03:53:33
合計ジャッジ時間 55,404 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,880 KB
testcase_01 AC 147 ms
21,760 KB
testcase_02 AC 32 ms
10,752 KB
testcase_03 AC 1,085 ms
31,104 KB
testcase_04 AC 919 ms
22,016 KB
testcase_05 AC 828 ms
16,512 KB
testcase_06 AC 838 ms
16,512 KB
testcase_07 AC 590 ms
21,888 KB
testcase_08 AC 893 ms
21,888 KB
testcase_09 AC 812 ms
21,760 KB
testcase_10 AC 808 ms
21,888 KB
testcase_11 AC 920 ms
22,016 KB
testcase_12 AC 889 ms
21,888 KB
testcase_13 AC 707 ms
11,264 KB
testcase_14 AC 693 ms
11,008 KB
testcase_15 AC 691 ms
10,880 KB
testcase_16 AC 902 ms
21,888 KB
testcase_17 AC 970 ms
22,656 KB
testcase_18 AC 891 ms
22,016 KB
testcase_19 AC 455 ms
16,256 KB
testcase_20 AC 921 ms
21,760 KB
testcase_21 AC 908 ms
21,632 KB
testcase_22 AC 985 ms
22,400 KB
testcase_23 AC 1,006 ms
22,016 KB
testcase_24 AC 977 ms
22,144 KB
testcase_25 AC 988 ms
22,272 KB
testcase_26 AC 977 ms
22,400 KB
testcase_27 AC 990 ms
22,272 KB
testcase_28 AC 995 ms
22,272 KB
testcase_29 AC 991 ms
22,144 KB
testcase_30 AC 988 ms
22,016 KB
testcase_31 AC 993 ms
22,144 KB
testcase_32 AC 886 ms
21,632 KB
testcase_33 AC 872 ms
21,760 KB
testcase_34 AC 880 ms
21,632 KB
testcase_35 AC 888 ms
21,760 KB
testcase_36 AC 869 ms
21,632 KB
testcase_37 AC 884 ms
21,760 KB
testcase_38 AC 858 ms
21,632 KB
testcase_39 AC 860 ms
21,888 KB
testcase_40 AC 866 ms
21,760 KB
testcase_41 AC 771 ms
21,760 KB
testcase_42 AC 803 ms
21,760 KB
testcase_43 AC 846 ms
21,888 KB
testcase_44 AC 886 ms
21,760 KB
testcase_45 AC 913 ms
21,888 KB
testcase_46 AC 887 ms
21,760 KB
testcase_47 AC 904 ms
21,760 KB
testcase_48 AC 933 ms
21,760 KB
testcase_49 AC 912 ms
21,888 KB
testcase_50 AC 913 ms
21,888 KB
testcase_51 AC 929 ms
22,144 KB
testcase_52 AC 964 ms
22,016 KB
testcase_53 AC 1,010 ms
22,272 KB
testcase_54 AC 1,013 ms
22,272 KB
testcase_55 AC 1,018 ms
23,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin

class UnionFind():
    def __init__(self, n):
        self.N = n
        self.num_component = n
        self.parent_or_size = [-1] * n
        self.weight = [0] * n
        self.used_list = []
        self.is_zero = False

    def leader(self, x):
        if self.parent_or_size[x] < 0: return x
        root = self.leader(self.parent_or_size[x])
        self.weight[x] ^= self.weight[self.parent_or_size[x]]
        self.parent_or_size[x] = root
        return root
    
    def merge(self, x, y, w):
        rx, ry = self.leader(x), self.leader(y)
        w ^= self.weight[x] ^ self.weight[y]
        if rx == ry:
            if w: self.is_zero = True 
            return
        if self.parent_or_size[rx] > self.parent_or_size[ry]: rx, ry = ry, rx
        self.parent_or_size[rx] += self.parent_or_size[ry]
        self.parent_or_size[ry] = rx
        self.weight[ry] = w
        self.num_component -= 1
        self.used_list.append(ry)
        self.used_list.append(rx)
    
    def reset(self):
        self.num_component = self.N
        self.is_zero = False
        for pos in self.used_list:
            self.parent_or_size[pos] = -1
            self.weight[pos] = 0
        self.used_list.clear()

N, Q = map(int, stdin.readline().split())

uf = UnionFind(N)
pow2 = [0] * (N + 1)
pow2[0] = 1
for i in range(N):
    pow2[i + 1] = pow2[i] + pow2[i]
    if pow2[i + 1] >= 998244353: pow2[i + 1] -= 998244353

for _ in range(Q):
    query = list(map(int, stdin.readline().split()))
    if query[0] <= 2:
        uf.merge(query[1] - 1, query[2] - 1, query[0] - 1)
    else:
        uf.reset()
    print(0 if uf.is_zero else pow2[uf.num_component])
0