結果
問題 | No.2293 無向辺 2-SAT |
ユーザー | vwxyz |
提出日時 | 2023-11-30 13:32:08 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,753 ms / 4,000 ms |
コード長 | 5,459 bytes |
コンパイル時間 | 318 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 191,568 KB |
最終ジャッジ日時 | 2024-09-26 14:03:02 |
合計ジャッジ時間 | 73,990 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 143 ms
89,088 KB |
testcase_01 | AC | 223 ms
160,512 KB |
testcase_02 | AC | 142 ms
89,088 KB |
testcase_03 | AC | 1,753 ms
163,328 KB |
testcase_04 | AC | 795 ms
171,232 KB |
testcase_05 | AC | 691 ms
133,156 KB |
testcase_06 | AC | 668 ms
132,360 KB |
testcase_07 | AC | 351 ms
161,920 KB |
testcase_08 | AC | 779 ms
170,924 KB |
testcase_09 | AC | 655 ms
162,816 KB |
testcase_10 | AC | 747 ms
162,816 KB |
testcase_11 | AC | 814 ms
171,204 KB |
testcase_12 | AC | 783 ms
170,736 KB |
testcase_13 | AC | 609 ms
96,340 KB |
testcase_14 | AC | 690 ms
97,012 KB |
testcase_15 | AC | 990 ms
106,216 KB |
testcase_16 | AC | 756 ms
171,120 KB |
testcase_17 | AC | 834 ms
176,524 KB |
testcase_18 | AC | 721 ms
170,944 KB |
testcase_19 | AC | 459 ms
132,444 KB |
testcase_20 | AC | 1,402 ms
183,452 KB |
testcase_21 | AC | 1,497 ms
183,828 KB |
testcase_22 | AC | 1,498 ms
189,508 KB |
testcase_23 | AC | 1,413 ms
188,384 KB |
testcase_24 | AC | 1,451 ms
189,876 KB |
testcase_25 | AC | 1,385 ms
188,644 KB |
testcase_26 | AC | 1,440 ms
189,896 KB |
testcase_27 | AC | 1,524 ms
189,460 KB |
testcase_28 | AC | 1,501 ms
190,040 KB |
testcase_29 | AC | 1,508 ms
190,272 KB |
testcase_30 | AC | 1,467 ms
189,592 KB |
testcase_31 | AC | 1,391 ms
187,684 KB |
testcase_32 | AC | 1,673 ms
163,776 KB |
testcase_33 | AC | 871 ms
162,048 KB |
testcase_34 | AC | 1,665 ms
163,356 KB |
testcase_35 | AC | 1,623 ms
164,704 KB |
testcase_36 | AC | 1,647 ms
163,660 KB |
testcase_37 | AC | 846 ms
162,068 KB |
testcase_38 | AC | 1,679 ms
164,532 KB |
testcase_39 | AC | 1,667 ms
163,364 KB |
testcase_40 | AC | 1,657 ms
164,948 KB |
testcase_41 | AC | 647 ms
162,048 KB |
testcase_42 | AC | 1,427 ms
162,944 KB |
testcase_43 | AC | 1,618 ms
162,448 KB |
testcase_44 | AC | 1,704 ms
174,992 KB |
testcase_45 | AC | 1,679 ms
176,892 KB |
testcase_46 | AC | 1,582 ms
184,144 KB |
testcase_47 | AC | 1,512 ms
183,688 KB |
testcase_48 | AC | 1,585 ms
183,480 KB |
testcase_49 | AC | 1,406 ms
182,168 KB |
testcase_50 | AC | 1,381 ms
183,496 KB |
testcase_51 | AC | 1,427 ms
185,768 KB |
testcase_52 | AC | 1,465 ms
184,904 KB |
testcase_53 | AC | 1,379 ms
187,008 KB |
testcase_54 | AC | 1,432 ms
191,568 KB |
testcase_55 | AC | 1,436 ms
188,228 KB |
ソースコード
import bisect import copy import decimal import fractions import heapq import itertools import math import random import sys import time from collections import Counter,deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import gcd as GCD read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines write=sys.stdout.write #import pypyjit #pypyjit.set_param('max_unroll_recursion=-1') #sys.set_int_max_str_digits(10**9) class UnionFind: def __init__(self,N,label=None,f=None,weighted=False,rollback=False): self.N=N self.parents=[None]*self.N self.size=[1]*self.N self.roots={i for i in range(self.N)} self.label=label if self.label!=None: self.label=[x for x in label] self.f=f self.weighted=weighted if self.weighted: self.weight=[0]*self.N self.rollback=rollback if self.rollback: self.operate_list=[] self.operate_set=[] def Find(self,x): stack=[] while self.parents[x]!=None: stack.append(x) x=self.parents[x] if not self.rollback: if self.weighted: w=0 for y in stack[::-1]: self.parents[y]=x w+=self.weight[y] self.weight[y]=w else: for y in stack[::-1]: self.parents[y]=x return x def Union(self,x,y,w=None): root_x=self.Find(x) root_y=self.Find(y) if self.rollback: self.operate_list.append([]) self.operate_set.append([]) if root_x==root_y: if self.weighted: if self.weight[y]-self.weight[x]==w: return True else: return False else: if self.size[root_x]<self.size[root_y]: x,y=y,x root_x,root_y=root_y,root_x if self.weighted: w=-w if self.rollback: self.operate_list[-1].append((self.parents,root_y,self.parents[root_y])) self.operate_list[-1].append((self.size,root_x,self.size[root_x])) self.operate_set[-1].append(root_y) if self.label!=None: self.operate_list[-1]((self.label,root_x,self.label[root_x])) if self.weighted: self.operate_list[-1].append((self.weight,root_y,self.weight[root_y])) self.parents[root_y]=root_x self.size[root_x]+=self.size[root_y] self.roots.remove(root_y) if self.label!=None: self.label[root_x]=self.f(self.label[root_x],self.label[root_y]) if self.weighted: self.weight[root_y]=w+self.weight[x]-self.weight[y] def Size(self,x): return self.size[self.Find(x)] def Same(self,x,y): return self.Find(x)==self.Find(y) def Label(self,x): return self.label[self.Find(x)] def Weight(self,x,y): root_x=self.Find(x) root_y=self.Find(y) if root_x!=root_y: return None return self.weight[y]-self.weight[x] def Roots(self): return list(self.roots) def Linked_Components_Count(self): return len(self.roots) def Linked_Components(self): linked_components=defaultdict(list) for x in range(self.N): linked_components[self.Find(x)].append(x) return linked_components def Rollback(self): assert self.rollback if self.operate_list: for lst,x,v in self.operate_list.pop(): lst[x]=v for x in self.operate_set.pop(): self.roots.add(x) return True else: return False def __str__(self): linked_components=defaultdict(list) for x in range(self.N): linked_components[self.Find(x)].append(x) return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys()))) N,Q=map(int,readline().split()) mod=998244353 UF=UnionFind(2*N) exist=True record=[] for q in range(Q): query=tuple(map(int,readline().split())) if query[0]==2: _,u,v=query u-=1;v-=1 UF.Union(u*2,v*2+1) UF.Union(v*2,u*2+1) record.append(u) record.append(v) if UF.Same(u*2,u*2+1): exist=False elif query[0]==1: _,u,v=query u-=1;v-=1 UF.Union(u*2,v*2) UF.Union(v*2+1,u*2+1) record.append(u) record.append(v) if UF.Same(u*2,u*2+1): exist=False else: for x in record: UF.parents[2*x]=None UF.parents[2*x+1]=None UF.size[2*x]=1 UF.size[2*x+1]=1 UF.roots.add(2*x) UF.roots.add(2*x+1) record=[] exist=True if exist: ans=pow(2,UF.Linked_Components_Count()//2,mod) else: ans=0 print(ans)