結果

問題 No.2290 UnUnion Find
ユーザー navel_tosnavel_tos
提出日時 2023-06-01 12:09:46
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,148 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 94,604 KB
最終ジャッジ日時 2023-08-28 01:59:59
合計ジャッジ時間 21,940 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,236 KB
testcase_01 AC 72 ms
71,252 KB
testcase_02 AC 663 ms
78,428 KB
testcase_03 AC 516 ms
94,596 KB
testcase_04 AC 518 ms
94,484 KB
testcase_05 AC 516 ms
94,528 KB
testcase_06 AC 527 ms
94,452 KB
testcase_07 AC 524 ms
94,424 KB
testcase_08 AC 522 ms
94,604 KB
testcase_09 AC 520 ms
94,600 KB
testcase_10 AC 526 ms
94,192 KB
testcase_11 AC 519 ms
94,172 KB
testcase_12 AC 520 ms
94,476 KB
testcase_13 AC 521 ms
94,424 KB
testcase_14 AC 527 ms
94,508 KB
testcase_15 AC 535 ms
94,480 KB
testcase_16 AC 526 ms
94,580 KB
testcase_17 AC 525 ms
94,464 KB
testcase_18 AC 527 ms
94,424 KB
testcase_19 TLE -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder2290 UnUnion Find

#UnionFind
class UnionFind:
    def __init__(self,N): self._parent=[-1 for i in[0]*N]; self._Plist=set([i for i in range(N)])
    def find(self,v):  #頂点vの親を探し、経路圧縮する
        vertices=[]
        while self._parent[v]>=0: vertices.append(v);v=self._parent[v]
        for i in vertices: self._parent[i]=v
        return v
    def unite(self,x,y):  #頂点xとyを併合し、併合の有無を返す
        x,y = self.find(x),self.find(y)
        if x==y: return 0
        if self._parent[x]>self._parent[y]: x,y=y,x  #負値で管理
        self._parent[x]+=self._parent[y]; self._parent[y]=x
        self._Plist.discard(y); return 1
    def same(self,x,y):return self.find(x)==self.find(y)   #xとyは同一集合か返す
    def size(self,x):  return -self._parent[self.find(x)]  #xの集合のサイズを求める
    def check(self): return self._Plist


f=lambda:list(map(int,input().split()))
N,Q=f(); UF=UnionFind(N)
for _ in range(Q):
    t,u,*v=f(); u-=1
    if t==1: v=v[0]-1; UF.unite(u,v)
    if t==2: p=set([UF.find(u)])^UF.check(); print(next(iter(p))+1) if p else print(-1)
0