結果

問題 No.2293 無向辺 2-SAT
ユーザー chineristACchineristAC
提出日時 2023-05-05 23:41:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,093 ms / 4,000 ms
コード長 3,168 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 139,452 KB
最終ジャッジ日時 2024-11-23 12:54:09
合計ジャッジ時間 47,456 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
56,320 KB
testcase_01 AC 68 ms
73,344 KB
testcase_02 AC 52 ms
56,192 KB
testcase_03 AC 612 ms
131,276 KB
testcase_04 AC 765 ms
135,592 KB
testcase_05 AC 704 ms
128,444 KB
testcase_06 AC 705 ms
127,540 KB
testcase_07 AC 264 ms
90,752 KB
testcase_08 AC 751 ms
131,916 KB
testcase_09 AC 657 ms
121,564 KB
testcase_10 AC 661 ms
121,340 KB
testcase_11 AC 805 ms
137,400 KB
testcase_12 AC 764 ms
134,620 KB
testcase_13 AC 504 ms
94,396 KB
testcase_14 AC 469 ms
82,496 KB
testcase_15 AC 461 ms
82,284 KB
testcase_16 AC 773 ms
135,604 KB
testcase_17 AC 816 ms
139,452 KB
testcase_18 AC 769 ms
135,756 KB
testcase_19 AC 431 ms
106,600 KB
testcase_20 AC 1,093 ms
127,200 KB
testcase_21 AC 1,020 ms
122,900 KB
testcase_22 AC 931 ms
130,244 KB
testcase_23 AC 873 ms
128,444 KB
testcase_24 AC 915 ms
128,944 KB
testcase_25 AC 869 ms
128,164 KB
testcase_26 AC 893 ms
130,200 KB
testcase_27 AC 911 ms
130,000 KB
testcase_28 AC 1,007 ms
129,496 KB
testcase_29 AC 965 ms
130,424 KB
testcase_30 AC 982 ms
130,340 KB
testcase_31 AC 901 ms
130,536 KB
testcase_32 AC 702 ms
110,852 KB
testcase_33 AC 694 ms
111,180 KB
testcase_34 AC 702 ms
111,900 KB
testcase_35 AC 701 ms
111,560 KB
testcase_36 AC 703 ms
110,804 KB
testcase_37 AC 699 ms
111,436 KB
testcase_38 AC 703 ms
110,952 KB
testcase_39 AC 697 ms
110,560 KB
testcase_40 AC 684 ms
111,080 KB
testcase_41 AC 452 ms
98,716 KB
testcase_42 AC 592 ms
104,524 KB
testcase_43 AC 669 ms
109,320 KB
testcase_44 AC 735 ms
113,972 KB
testcase_45 AC 779 ms
118,664 KB
testcase_46 AC 807 ms
121,620 KB
testcase_47 AC 835 ms
124,020 KB
testcase_48 AC 857 ms
125,184 KB
testcase_49 AC 876 ms
125,768 KB
testcase_50 AC 851 ms
125,876 KB
testcase_51 AC 860 ms
127,084 KB
testcase_52 AC 852 ms
127,432 KB
testcase_53 AC 862 ms
130,060 KB
testcase_54 AC 936 ms
134,804 KB
testcase_55 AC 1,020 ms
136,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

class WeightedUnionFind():
    def __init__(self,N):
        self.n = N
        self.parent = [i for i in range(N)]
        self.size = [1 for i in range(N)]
        self.val = [0 for i in range(N)]
        self.flag = True
        self.edge = [[] for i in range(N)]
        self.change = []
        self.free = N

    def dfs(self,v,pv):
        stack = [(v,pv)]
        new_parent = self.parent[pv]
        while stack:
            v,pv = stack.pop()
            self.parent[v] = new_parent
            for nv,w in self.edge[v]:
                if nv!=pv:
                    self.val[nv] = self.val[v] ^ w
                    stack.append((nv,v))

    def unite(self,x,y,w):
        self.change.append(x)
        self.change.append(y)
        if not self.flag:
            return
        if self.parent[x]==self.parent[y]:
            self.flag &= ((self.val[x] ^ self.val[y]) == w)
            return
        
        self.free -= 1

        if self.size[self.parent[x]]>self.size[self.parent[y]]:
            self.edge[x].append((y,w))
            self.edge[y].append((x,w))
            self.size[self.parent[x]] += self.size[self.parent[y]]
            self.val[y] = self.val[x] ^ w
            self.dfs(y,x)
        else:
            self.edge[x].append((y,w))
            self.edge[y].append((x,w))
            self.size[self.parent[y]] += self.size[self.parent[x]]
            self.val[x] = self.val[y] ^ w
            self.dfs(x,y)
    
    def reset(self):
        self.flag = True
        self.free = self.n
        for x in self.change:
            self.parent[x] = x
            self.size[x] = 1
            self.val[x] = 0
            self.edge[x] = []
        self.change = []


N,Q = mi()
uf = WeightedUnionFind(N)
for _ in range(Q):
    t,*query = mi()
    if t == 1:
        u,v = query
        u,v = u-1,v-1
        uf.unite(u,v,0)
    elif t == 2:
        u,v = query
        u,v = u-1,v-1
        uf.unite(u,v,1)
    else:
        uf.reset()
    
    if uf.flag:
        print(pow(2,uf.free,998244353))
    else:
        print(0)
0