結果
問題 | No.2290 UnUnion Find |
ユーザー | vwxyz |
提出日時 | 2023-05-05 23:43:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 397 ms / 2,000 ms |
コード長 | 3,026 bytes |
コンパイル時間 | 368 ms |
コンパイル使用メモリ | 82,816 KB |
実行使用メモリ | 110,720 KB |
最終ジャッジ日時 | 2024-05-02 19:01:48 |
合計ジャッジ時間 | 18,944 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,888 KB |
testcase_01 | AC | 43 ms
54,656 KB |
testcase_02 | AC | 186 ms
77,568 KB |
testcase_03 | AC | 244 ms
92,032 KB |
testcase_04 | AC | 246 ms
92,416 KB |
testcase_05 | AC | 240 ms
92,160 KB |
testcase_06 | AC | 256 ms
92,160 KB |
testcase_07 | AC | 264 ms
92,416 KB |
testcase_08 | AC | 257 ms
92,032 KB |
testcase_09 | AC | 265 ms
92,160 KB |
testcase_10 | AC | 242 ms
92,032 KB |
testcase_11 | AC | 251 ms
92,288 KB |
testcase_12 | AC | 246 ms
92,160 KB |
testcase_13 | AC | 247 ms
92,032 KB |
testcase_14 | AC | 239 ms
92,288 KB |
testcase_15 | AC | 262 ms
92,160 KB |
testcase_16 | AC | 234 ms
92,032 KB |
testcase_17 | AC | 240 ms
91,904 KB |
testcase_18 | AC | 281 ms
92,160 KB |
testcase_19 | AC | 359 ms
94,720 KB |
testcase_20 | AC | 397 ms
110,720 KB |
testcase_21 | AC | 267 ms
80,028 KB |
testcase_22 | AC | 312 ms
84,420 KB |
testcase_23 | AC | 303 ms
84,352 KB |
testcase_24 | AC | 298 ms
101,760 KB |
testcase_25 | AC | 293 ms
82,036 KB |
testcase_26 | AC | 299 ms
105,216 KB |
testcase_27 | AC | 366 ms
102,016 KB |
testcase_28 | AC | 328 ms
90,624 KB |
testcase_29 | AC | 355 ms
98,816 KB |
testcase_30 | AC | 307 ms
80,392 KB |
testcase_31 | AC | 351 ms
96,640 KB |
testcase_32 | AC | 300 ms
82,552 KB |
testcase_33 | AC | 352 ms
89,344 KB |
testcase_34 | AC | 373 ms
110,720 KB |
testcase_35 | AC | 367 ms
105,088 KB |
testcase_36 | AC | 305 ms
82,560 KB |
testcase_37 | AC | 340 ms
87,808 KB |
testcase_38 | AC | 313 ms
82,708 KB |
testcase_39 | AC | 320 ms
85,120 KB |
testcase_40 | AC | 358 ms
102,016 KB |
testcase_41 | AC | 319 ms
82,196 KB |
testcase_42 | AC | 351 ms
94,208 KB |
testcase_43 | AC | 343 ms
92,416 KB |
testcase_44 | AC | 245 ms
92,160 KB |
testcase_45 | AC | 261 ms
92,416 KB |
testcase_46 | AC | 272 ms
92,288 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)