結果

問題 No.2293 無向辺 2-SAT
ユーザー 👑 KazunKazun
提出日時 2023-05-05 22:53:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,037 ms / 4,000 ms
コード長 5,023 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 197,920 KB
最終ジャッジ日時 2023-08-15 05:47:24
合計ジャッジ時間 54,815 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,716 KB
testcase_01 AC 97 ms
73,224 KB
testcase_02 AC 97 ms
71,552 KB
testcase_03 AC 2,037 ms
197,920 KB
testcase_04 AC 975 ms
91,252 KB
testcase_05 AC 515 ms
89,600 KB
testcase_06 AC 491 ms
87,492 KB
testcase_07 AC 250 ms
86,148 KB
testcase_08 AC 483 ms
88,536 KB
testcase_09 AC 446 ms
89,124 KB
testcase_10 AC 423 ms
87,880 KB
testcase_11 AC 546 ms
91,592 KB
testcase_12 AC 500 ms
88,432 KB
testcase_13 AC 476 ms
86,572 KB
testcase_14 AC 603 ms
88,340 KB
testcase_15 AC 850 ms
94,388 KB
testcase_16 AC 544 ms
91,908 KB
testcase_17 AC 772 ms
127,540 KB
testcase_18 AC 518 ms
88,780 KB
testcase_19 AC 352 ms
83,616 KB
testcase_20 AC 878 ms
95,900 KB
testcase_21 AC 1,240 ms
100,128 KB
testcase_22 AC 1,036 ms
118,872 KB
testcase_23 AC 1,044 ms
120,780 KB
testcase_24 AC 1,017 ms
120,284 KB
testcase_25 AC 1,088 ms
122,944 KB
testcase_26 AC 1,019 ms
118,448 KB
testcase_27 AC 1,026 ms
119,984 KB
testcase_28 AC 1,039 ms
121,264 KB
testcase_29 AC 1,048 ms
121,124 KB
testcase_30 AC 1,028 ms
119,808 KB
testcase_31 AC 1,006 ms
119,924 KB
testcase_32 AC 706 ms
90,244 KB
testcase_33 AC 812 ms
93,828 KB
testcase_34 AC 718 ms
90,868 KB
testcase_35 AC 775 ms
91,696 KB
testcase_36 AC 847 ms
92,112 KB
testcase_37 AC 822 ms
91,820 KB
testcase_38 AC 734 ms
92,176 KB
testcase_39 AC 745 ms
92,644 KB
testcase_40 AC 809 ms
93,104 KB
testcase_41 AC 597 ms
89,736 KB
testcase_42 AC 758 ms
92,352 KB
testcase_43 AC 849 ms
92,064 KB
testcase_44 AC 804 ms
92,928 KB
testcase_45 AC 737 ms
90,528 KB
testcase_46 AC 1,076 ms
97,364 KB
testcase_47 AC 1,178 ms
97,260 KB
testcase_48 AC 1,184 ms
98,452 KB
testcase_49 AC 1,133 ms
98,236 KB
testcase_50 AC 1,080 ms
99,964 KB
testcase_51 AC 1,055 ms
103,012 KB
testcase_52 AC 1,007 ms
103,752 KB
testcase_53 AC 1,108 ms
115,684 KB
testcase_54 AC 1,098 ms
125,312 KB
testcase_55 AC 1,086 ms
138,740 KB
権限があれば一括ダウンロードができます

ソースコード

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():
    from collections import defaultdict
    N,Q=map(int,input().split())
    U=RollBack_Union_Find(N)

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

            if up==vp:
                if T[up][u]!=T[vp][v]^mode:
                    valid=False
            else:
                if (up not in T) or (u not in T[up]):
                    T[up][u]=0
                if (vp not in T) or (v not in T[vp]):
                    T[vp][v]=0

                if len(T[up])<len(T[vp]):
                    u,v=v,u
                    up,vp=vp,up
                diff=T[up][u]^T[vp][v]
                for v in T[vp]:
                    T[up][v]=T[vp][v]^diff^mode
                del T[vp]
            U.union(u,v)
        else:
            U.rollback(0)
            T=defaultdict(lambda :defaultdict(int))
            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