結果
問題 | No.2290 UnUnion Find |
ユーザー | vwxyz |
提出日時 | 2023-05-05 23:43:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 448 ms / 2,000 ms |
コード長 | 3,026 bytes |
コンパイル時間 | 291 ms |
コンパイル使用メモリ | 82,332 KB |
実行使用メモリ | 110,776 KB |
最終ジャッジ日時 | 2024-11-23 12:56:16 |
合計ジャッジ時間 | 21,774 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
54,312 KB |
testcase_01 | AC | 52 ms
54,040 KB |
testcase_02 | AC | 245 ms
77,668 KB |
testcase_03 | AC | 277 ms
92,044 KB |
testcase_04 | AC | 274 ms
92,116 KB |
testcase_05 | AC | 285 ms
92,120 KB |
testcase_06 | AC | 274 ms
92,492 KB |
testcase_07 | AC | 294 ms
92,436 KB |
testcase_08 | AC | 294 ms
92,300 KB |
testcase_09 | AC | 297 ms
92,112 KB |
testcase_10 | AC | 276 ms
92,504 KB |
testcase_11 | AC | 299 ms
92,212 KB |
testcase_12 | AC | 293 ms
92,088 KB |
testcase_13 | AC | 290 ms
92,072 KB |
testcase_14 | AC | 290 ms
92,728 KB |
testcase_15 | AC | 304 ms
92,040 KB |
testcase_16 | AC | 280 ms
92,240 KB |
testcase_17 | AC | 296 ms
92,148 KB |
testcase_18 | AC | 304 ms
92,400 KB |
testcase_19 | AC | 408 ms
94,712 KB |
testcase_20 | AC | 439 ms
110,776 KB |
testcase_21 | AC | 313 ms
80,284 KB |
testcase_22 | AC | 366 ms
84,628 KB |
testcase_23 | AC | 354 ms
84,220 KB |
testcase_24 | AC | 353 ms
101,956 KB |
testcase_25 | AC | 344 ms
82,192 KB |
testcase_26 | AC | 351 ms
105,312 KB |
testcase_27 | AC | 420 ms
102,032 KB |
testcase_28 | AC | 394 ms
90,872 KB |
testcase_29 | AC | 427 ms
99,324 KB |
testcase_30 | AC | 372 ms
80,528 KB |
testcase_31 | AC | 448 ms
96,844 KB |
testcase_32 | AC | 350 ms
82,500 KB |
testcase_33 | AC | 416 ms
88,908 KB |
testcase_34 | AC | 412 ms
110,756 KB |
testcase_35 | AC | 430 ms
105,352 KB |
testcase_36 | AC | 352 ms
82,500 KB |
testcase_37 | AC | 394 ms
87,860 KB |
testcase_38 | AC | 364 ms
82,536 KB |
testcase_39 | AC | 366 ms
85,220 KB |
testcase_40 | AC | 447 ms
101,912 KB |
testcase_41 | AC | 368 ms
82,544 KB |
testcase_42 | AC | 426 ms
94,380 KB |
testcase_43 | AC | 408 ms
92,436 KB |
testcase_44 | AC | 285 ms
92,120 KB |
testcase_45 | AC | 300 ms
92,608 KB |
testcase_46 | AC | 309 ms
92,496 KB |
ソースコード
import sys readline=sys.stdin.readline from collections import defaultdict class UnionFind: def __init__(self,N,label=None,f=None,weighted=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 def Find(self,x): stack=[] while self.parents[x]!=None: stack.append(x) x=self.parents[x] 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 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 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(int) for x in range(self.N): linked_components[self.Find(x)].append(x) return linked_components 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}: {m}" for r,m in linked_components.items()) N,Q=map(int,readline().split()) UF=UnionFind(N) for q in range(Q): query=tuple(map(int,readline().split())) if query[0]==1: _,u,v=query u-=1;v-=1 UF.Union(u,v) else: _,u=query u-=1 for r in UF.roots: if not UF.Same(u,r): ans=r+1 break else: ans=-1 print(ans)