結果

問題 No.2293 無向辺 2-SAT
ユーザー 👑 KazunKazun
提出日時 2023-05-05 22:21:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,622 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 148,792 KB
最終ジャッジ日時 2024-05-02 17:00:28
合計ジャッジ時間 31,290 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
52,992 KB
testcase_01 AC 41 ms
54,528 KB
testcase_02 AC 40 ms
52,864 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 343 ms
85,120 KB
testcase_07 AC 200 ms
84,480 KB
testcase_08 AC 341 ms
85,760 KB
testcase_09 AC 316 ms
85,632 KB
testcase_10 AC 308 ms
85,376 KB
testcase_11 WA -
testcase_12 AC 359 ms
85,760 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 381 ms
85,760 KB
testcase_19 AC 231 ms
81,024 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class RollBack_Union_Find():
    __slots__=["n", "parents", "__history", "__snap_time", "__group_count"]
    def __init__(self, N):
        """ 0,1,...,N-1 を要素として初期化する.

        N: 要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.__history=[]
        self.__snap_time=[]
        self.__group_count=[]

    def find(self, x):
        """ 要素 x の属している族を調べる.

        x: 要素
        """

        while self.parents[x]>=0:
            x=self.parents[x]

        return x

    def union(self, x, y):
        """ 要素 x,y を同一視する.

        [input]
        x,y: 要素

        [output]
        元々が非連結 → True
        元々が連結 → False
        """
        x=self.find(x); y=self.find(y)

        par=self.parents

        self.__history.append((x, par[x]))
        self.__history.append((y, par[y]))

        if self.__group_count:
            count=self.__group_count[-1]
        else:
            count=self.n

        if x==y:
            self.__group_count.append(count)
            return False

        if par[x]>par[y]:
            x,y=y,x

        par[x]+=par[y]
        par[y]=x

        self.__group_count.append(count-1)

        return True

    def size(self, x):
        """ 要素 x の属している族の要素の数.

        x: 要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """ 要素 x,y は同一視されているか?

        x,y: 要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """ 要素 x が属している族の要素.
        ※族の要素の個数が欲しいときは size を使うこと!!

        x: 要素
        """
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def representative(self):
        """ 代表元のリスト
        """
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ 族の個数
        """

        if self.__group_count:
            return self.__group_count[-1]
        else:
            return self.n

    def get_time(self):
        return len(self.__history)>>1

    def undo(self):
        """ 1 回分の union を戻る.

        """

        if self.__history:
            y,b=self.__history.pop()
            self.parents[y]=b

            x,a=self.__history.pop()
            self.parents[x]=a

            self.__group_count.pop()

    def snapshot(self):
        """ スナップショットを撮る

        """

        self.__snap_time.append(self.get_time())

    def rollback(self, time=-1):
        """ 時刻 time 直前まで戻る.

        """

        if time==-1:
            if self.__snap_time:
                time=self.__snap_time[-1]
            else:
                return

        if time>self.get_time():
            return

        T=time<<1
        while T<len(self.__history):
            self.undo()

    def all_group_members(self):
        """ 全ての族の出力
        """
        X={r:[] for r in self.representative()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def group_list(self):
        """ 各要素が属している族のリストを出力する.

        """
        return [self.find(x) for x in range(self.n)]

    def __str__(self):
        return str(self.all_group_members().values())[13:-2]

    def __repr__(self):
        return "RollBack Union Find: "+str(self)

    def __getitem__(self,index):
        return self.find(index)

    def __setitem__(self,x,y):
        self.union(x,y)

#==================================================
def solve():
    N,Q=map(int,input().split())
    U=RollBack_Union_Find(N)

    ans=[0]*Q
    valid=True
    T={}
    for q in range(Q):
        mode,*value=map(int,input().split())
        if mode==1 or mode==2:
            u,v=value
            u-=1; v-=1

            if (u not in T):
                if (v not in T):
                    T[u]=0
                else:
                    u,v=v,u
            mode-=1
            if v not in T:
                T[v]=T[u]^mode
            else:
                if T[v]!=T[u]^mode:
                    valid=False
            U.union(u,v)
        else:
            U.rollback(0)
            T={}
            valid=True

        ans[q]=pow(2,U.group_count(),998244353) if valid else 0
    return ans

#==================================================
import sys
input=sys.stdin.readline
write=sys.stdout.write

write("\n".join(map(str,solve())))
0