結果
問題 | No.2290 UnUnion Find |
ユーザー |
![]() |
提出日時 | 2023-05-05 22:06:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 834 ms / 2,000 ms |
コード長 | 722 bytes |
コンパイル時間 | 291 ms |
コンパイル使用メモリ | 82,452 KB |
実行使用メモリ | 109,184 KB |
最終ジャッジ日時 | 2024-11-23 07:52:52 |
合計ジャッジ時間 | 32,528 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 46 |
ソースコード
class UF: def __init__(self,n): self.p = [-1]*n; self.i = set(range(n)) def f(self,x): if self.p[x]<0: return x else: self.p[x] = self.f(self.p[x]); return self.p[x] def u(self,x,y): x = self.f(x); y = self.f(y) if x==y: return if -self.p[x]<-self.p[y]: x,y = y,x self.p[x] += self.p[y]; self.i.remove(y); self.p[y] = x def s(self,x,y): return self.f(x) == self.f(y) n,q = map(int,input().split()) uf = UF(n) for _ in range(q): p = list(map(int,input().split())) if p[0]==1: uf.u(p[1]-1,p[2]-1) if p[0]==2: if len(uf.i)==1: print(-1); continue i = uf.f(p[1]-1) for j in uf.i: if i!=j: print(j+1); break