結果

問題 No.2293 無向辺 2-SAT
ユーザー とりゐとりゐ
提出日時 2023-05-05 23:01:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,529 ms / 4,000 ms
コード長 2,018 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 86,920 KB
実行使用メモリ 146,632 KB
最終ジャッジ日時 2023-08-15 06:02:13
合計ジャッジ時間 52,301 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,632 KB
testcase_01 AC 104 ms
81,292 KB
testcase_02 AC 94 ms
71,720 KB
testcase_03 AC 1,529 ms
146,632 KB
testcase_04 AC 352 ms
85,436 KB
testcase_05 AC 347 ms
83,868 KB
testcase_06 AC 344 ms
84,808 KB
testcase_07 AC 191 ms
83,116 KB
testcase_08 AC 340 ms
85,048 KB
testcase_09 AC 329 ms
85,000 KB
testcase_10 AC 404 ms
86,880 KB
testcase_11 AC 359 ms
85,756 KB
testcase_12 AC 355 ms
85,180 KB
testcase_13 AC 429 ms
83,368 KB
testcase_14 AC 506 ms
83,816 KB
testcase_15 AC 707 ms
86,444 KB
testcase_16 AC 368 ms
86,076 KB
testcase_17 AC 398 ms
90,812 KB
testcase_18 AC 339 ms
85,628 KB
testcase_19 AC 259 ms
83,020 KB
testcase_20 AC 794 ms
93,848 KB
testcase_21 AC 990 ms
93,960 KB
testcase_22 AC 863 ms
102,012 KB
testcase_23 AC 778 ms
97,396 KB
testcase_24 AC 803 ms
101,008 KB
testcase_25 AC 767 ms
100,064 KB
testcase_26 AC 804 ms
100,480 KB
testcase_27 AC 768 ms
97,620 KB
testcase_28 AC 830 ms
99,068 KB
testcase_29 AC 784 ms
99,920 KB
testcase_30 AC 801 ms
101,740 KB
testcase_31 AC 795 ms
97,752 KB
testcase_32 AC 1,303 ms
103,164 KB
testcase_33 AC 1,284 ms
103,112 KB
testcase_34 AC 1,327 ms
106,552 KB
testcase_35 AC 1,339 ms
104,260 KB
testcase_36 AC 1,345 ms
102,960 KB
testcase_37 AC 1,285 ms
101,988 KB
testcase_38 AC 1,297 ms
105,412 KB
testcase_39 AC 1,291 ms
104,816 KB
testcase_40 AC 1,346 ms
106,484 KB
testcase_41 AC 694 ms
93,064 KB
testcase_42 AC 1,075 ms
98,448 KB
testcase_43 AC 1,271 ms
100,572 KB
testcase_44 AC 1,254 ms
104,276 KB
testcase_45 AC 1,198 ms
99,916 KB
testcase_46 AC 1,042 ms
96,488 KB
testcase_47 AC 956 ms
97,992 KB
testcase_48 AC 896 ms
95,024 KB
testcase_49 AC 825 ms
94,604 KB
testcase_50 AC 850 ms
96,476 KB
testcase_51 AC 788 ms
95,188 KB
testcase_52 AC 748 ms
92,516 KB
testcase_53 AC 706 ms
95,676 KB
testcase_54 AC 761 ms
99,964 KB
testcase_55 AC 678 ms
102,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
input=lambda :stdin.readline()[:-1]

from collections import defaultdict

class UnionFind():
  def __init__(self,n):
    self.n=n
    self.parents=[-1]*n
    self.res=[]

  def find(self,x):
    if self.parents[x]<0:
      return x
    else:
      self.parents[x]=self.find(self.parents[x])
      return self.parents[x]

  def clear(self):
    for i in self.res:
      self.parents[i]=-1
    self.res=[]
    
  def union(self,x,y):
    x=self.find(x)
    y=self.find(y)

    self.res.append(x)
    self.res.append(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

  def size(self,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


mod=998244353
n,q=map(int,input().split())
pow2=[1]*(n+1)
for i in range(n):
  pow2[i+1]=pow2[i]*2%mod

free=n
flag=True
uf=UnionFind(2*n)
for i in range(q):
  query=list(map(lambda x:int(x)-1,input().split()))
  if query[0]==1:
    x,y=query[1:]
    if not uf.same(2*x,2*y+1):
      uf.union(2*x,2*y+1)
      uf.union(2*x+1,2*y)
      free-=1
    if uf.same(2*x,2*x+1):
      flag=False
    if uf.same(2*y,2*y+1):
      flag=False
  elif query[0]==0:
    x,y=query[1:]
    if not uf.same(2*x,2*y):
      uf.union(2*x,2*y)
      uf.union(2*x+1,2*y+1)
      free-=1
    if uf.same(2*x,2*x+1):
      flag=False
    if uf.same(2*y,2*y+1):
      flag=False
  else:
    uf.clear()
    free=n
    flag=True
  
  if flag:
    print(pow2[free])
  else:
    print(0)
  #print(uf.all_group_members())
0