結果

問題 No.2293 無向辺 2-SAT
ユーザー とりゐとりゐ
提出日時 2023-05-05 23:01:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,349 ms / 4,000 ms
コード長 2,018 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 141,272 KB
最終ジャッジ日時 2024-05-02 18:04:38
合計ジャッジ時間 43,530 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,016 KB
testcase_01 AC 50 ms
64,512 KB
testcase_02 AC 40 ms
54,400 KB
testcase_03 AC 1,349 ms
141,272 KB
testcase_04 AC 313 ms
83,592 KB
testcase_05 AC 277 ms
81,208 KB
testcase_06 AC 285 ms
81,668 KB
testcase_07 AC 140 ms
81,792 KB
testcase_08 AC 289 ms
83,864 KB
testcase_09 AC 265 ms
83,744 KB
testcase_10 AC 332 ms
85,272 KB
testcase_11 AC 284 ms
83,456 KB
testcase_12 AC 287 ms
83,200 KB
testcase_13 AC 331 ms
80,344 KB
testcase_14 AC 419 ms
81,076 KB
testcase_15 AC 588 ms
83,968 KB
testcase_16 AC 277 ms
83,232 KB
testcase_17 AC 310 ms
88,216 KB
testcase_18 AC 263 ms
83,072 KB
testcase_19 AC 198 ms
80,216 KB
testcase_20 AC 673 ms
89,584 KB
testcase_21 AC 839 ms
91,596 KB
testcase_22 AC 688 ms
97,348 KB
testcase_23 AC 647 ms
94,236 KB
testcase_24 AC 659 ms
95,308 KB
testcase_25 AC 616 ms
94,400 KB
testcase_26 AC 659 ms
94,268 KB
testcase_27 AC 643 ms
93,732 KB
testcase_28 AC 683 ms
95,728 KB
testcase_29 AC 657 ms
95,084 KB
testcase_30 AC 654 ms
94,764 KB
testcase_31 AC 644 ms
94,540 KB
testcase_32 AC 1,096 ms
96,904 KB
testcase_33 AC 1,087 ms
97,640 KB
testcase_34 AC 1,131 ms
97,156 KB
testcase_35 AC 1,099 ms
97,320 KB
testcase_36 AC 1,089 ms
95,864 KB
testcase_37 AC 1,144 ms
96,944 KB
testcase_38 AC 1,116 ms
96,084 KB
testcase_39 AC 1,116 ms
97,872 KB
testcase_40 AC 1,134 ms
95,756 KB
testcase_41 AC 606 ms
89,524 KB
testcase_42 AC 980 ms
92,808 KB
testcase_43 AC 1,071 ms
94,508 KB
testcase_44 AC 1,078 ms
94,664 KB
testcase_45 AC 1,027 ms
94,976 KB
testcase_46 AC 888 ms
92,636 KB
testcase_47 AC 827 ms
90,524 KB
testcase_48 AC 752 ms
90,352 KB
testcase_49 AC 710 ms
88,612 KB
testcase_50 AC 667 ms
91,056 KB
testcase_51 AC 655 ms
89,640 KB
testcase_52 AC 644 ms
92,432 KB
testcase_53 AC 580 ms
91,848 KB
testcase_54 AC 612 ms
94,580 KB
testcase_55 AC 579 ms
97,300 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