結果
問題 |
No.2290 UnUnion Find
|
ユーザー |
![]() |
提出日時 | 2023-05-05 22:05:46 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 722 bytes |
コンパイル時間 | 631 ms |
コンパイル使用メモリ | 12,416 KB |
実行使用メモリ | 40,264 KB |
最終ジャッジ日時 | 2024-11-23 07:48:56 |
合計ジャッジ時間 | 91,896 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 31 TLE * 15 |
ソースコード
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