結果

問題 No.2293 無向辺 2-SAT
ユーザー lloyzlloyz
提出日時 2023-05-08 00:19:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,934 ms / 4,000 ms
コード長 2,691 bytes
コンパイル時間 1,294 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 143,464 KB
最終ジャッジ日時 2023-08-16 10:15:44
合計ジャッジ時間 82,735 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,524 KB
testcase_01 AC 186 ms
81,288 KB
testcase_02 AC 96 ms
71,380 KB
testcase_03 AC 1,934 ms
143,464 KB
testcase_04 AC 829 ms
84,728 KB
testcase_05 AC 765 ms
82,596 KB
testcase_06 AC 758 ms
82,500 KB
testcase_07 AC 626 ms
82,772 KB
testcase_08 AC 808 ms
84,956 KB
testcase_09 AC 782 ms
83,800 KB
testcase_10 AC 859 ms
85,376 KB
testcase_11 AC 817 ms
84,820 KB
testcase_12 AC 805 ms
84,816 KB
testcase_13 AC 776 ms
81,928 KB
testcase_14 AC 843 ms
82,524 KB
testcase_15 AC 1,071 ms
85,096 KB
testcase_16 AC 812 ms
84,556 KB
testcase_17 AC 870 ms
89,280 KB
testcase_18 AC 806 ms
84,596 KB
testcase_19 AC 494 ms
82,396 KB
testcase_20 AC 1,216 ms
91,204 KB
testcase_21 AC 1,404 ms
94,624 KB
testcase_22 AC 1,240 ms
96,960 KB
testcase_23 AC 1,201 ms
96,656 KB
testcase_24 AC 1,239 ms
98,292 KB
testcase_25 AC 1,168 ms
98,404 KB
testcase_26 AC 1,190 ms
97,028 KB
testcase_27 AC 1,217 ms
96,120 KB
testcase_28 AC 1,256 ms
98,640 KB
testcase_29 AC 1,235 ms
97,436 KB
testcase_30 AC 1,243 ms
96,504 KB
testcase_31 AC 1,204 ms
96,616 KB
testcase_32 AC 1,729 ms
98,620 KB
testcase_33 AC 1,764 ms
100,392 KB
testcase_34 AC 1,742 ms
96,956 KB
testcase_35 AC 1,747 ms
100,948 KB
testcase_36 AC 1,782 ms
102,604 KB
testcase_37 AC 1,763 ms
98,116 KB
testcase_38 AC 1,746 ms
98,492 KB
testcase_39 AC 1,781 ms
100,840 KB
testcase_40 AC 1,756 ms
99,136 KB
testcase_41 AC 1,134 ms
91,132 KB
testcase_42 AC 1,601 ms
97,444 KB
testcase_43 AC 1,711 ms
97,996 KB
testcase_44 AC 1,741 ms
96,928 KB
testcase_45 AC 1,597 ms
96,476 KB
testcase_46 AC 1,495 ms
95,028 KB
testcase_47 AC 1,412 ms
95,540 KB
testcase_48 AC 1,347 ms
94,436 KB
testcase_49 AC 1,330 ms
91,736 KB
testcase_50 AC 1,231 ms
93,440 KB
testcase_51 AC 1,155 ms
91,652 KB
testcase_52 AC 1,147 ms
91,400 KB
testcase_53 AC 1,156 ms
93,640 KB
testcase_54 AC 1,192 ms
97,380 KB
testcase_55 AC 1,108 ms
98,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n
        self.size = n
        self.used_list = []

    def find(self, x):
        '''
        要素xを含む集合の親を見つける関数。
        '''
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        要素xを含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素yを含む集合に統合される。
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x
        self.size -= 1
        self.used_list.append(x)
        self.used_list.append(y)

    def size(self, x):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

    def reset(self):
        self.size = self.n
        for p in self.used_list:
            self.parents[p] = -1
        self.used_list.clear()

n, q = map(int, input().split())
mod = 998244353

Pow2 = [pow(2, i, mod) for i in range(n + 1)]
UF = UnionFind(2 * n)
is_zero = False
for _ in range(q):
    L = list(map(int, input().split()))
    if L[0] <= 2:
        u, v = L[1:]
        u -= 1
        v -= 1
        if L[0] == 1:
            UF.union(u, v)
            UF.union(u + n, v + n)
        else:
            UF.union(u, v + n)
            UF.union(u + n, v)
        if UF.same(u, u + n):
            is_zero = True
    else:
        UF.reset()
        is_zero = False
    if is_zero:
        print(0)
    else:
        print(Pow2[UF.size // 2])
0