結果

問題 No.2293 無向辺 2-SAT
ユーザー rlangevinrlangevin
提出日時 2023-05-05 22:46:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,910 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 266,208 KB
最終ジャッジ日時 2024-05-02 17:45:07
合計ジャッジ時間 10,050 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
52,608 KB
testcase_01 AC 63 ms
77,056 KB
testcase_02 AC 41 ms
52,352 KB
testcase_03 WA -
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

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)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            self.diff_weight[x] ^= self.diff_weight[self.par[x]]
            return self.par[x]

    def union(self, x, y, w):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
            self.diff_weight[y] = w

    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[y]^self.diff_weight[x]


N, Q = map(int, input().split())
flag = 1
mod = 998244353
ans = pow(2, N, mod)
inv = pow(2, mod - 2, mod)
U = UnionFind(N)
for _ in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        u, v = query[1:]
        u, v = u - 1, v - 1
        if U.is_same(u, v):
            if U.diff(u, v) == 1:
                flag = 0
        else:
            U.union(u, v, 0)
            ans *= inv
            ans %= mod
    elif query[0] == 2:
        u, v = query[1:]
        u, v = u - 1, v - 1
        if U.is_same(u, v):
            if U.diff(u, v) == 0:
                flag = 0
        else:
            U.union(u, v, 1)
            ans *= inv
            ans %= mod
    else:
        ans = pow(2, N, mod)
        U = UnionFind(N)
        flag = 1
        
    if flag:
        print(ans)
    else:
        print(0)
0