結果

問題 No.2290 UnUnion Find
ユーザー navel_tosnavel_tos
提出日時 2023-06-01 12:11:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,194 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 86,648 KB
実行使用メモリ 113,864 KB
最終ジャッジ日時 2023-08-28 02:00:32
合計ジャッジ時間 31,657 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,164 KB
testcase_01 WA -
testcase_02 AC 524 ms
78,676 KB
testcase_03 AC 453 ms
94,616 KB
testcase_04 AC 459 ms
94,396 KB
testcase_05 AC 471 ms
94,400 KB
testcase_06 AC 448 ms
94,468 KB
testcase_07 AC 475 ms
94,392 KB
testcase_08 AC 473 ms
94,460 KB
testcase_09 AC 468 ms
94,340 KB
testcase_10 AC 449 ms
94,476 KB
testcase_11 AC 485 ms
94,588 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 479 ms
94,452 KB
testcase_15 AC 485 ms
94,600 KB
testcase_16 AC 458 ms
94,404 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 659 ms
96,740 KB
testcase_20 AC 662 ms
113,708 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 657 ms
86,080 KB
testcase_24 AC 563 ms
103,716 KB
testcase_25 AC 671 ms
83,276 KB
testcase_26 AC 577 ms
107,844 KB
testcase_27 AC 650 ms
104,708 KB
testcase_28 AC 665 ms
92,352 KB
testcase_29 AC 673 ms
101,640 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 673 ms
91,164 KB
testcase_34 AC 649 ms
113,864 KB
testcase_35 AC 681 ms
107,572 KB
testcase_36 AC 686 ms
84,120 KB
testcase_37 AC 675 ms
89,820 KB
testcase_38 AC 678 ms
83,984 KB
testcase_39 WA -
testcase_40 AC 679 ms
104,856 KB
testcase_41 WA -
testcase_42 AC 652 ms
96,516 KB
testcase_43 AC 663 ms
94,652 KB
testcase_44 AC 458 ms
94,400 KB
testcase_45 AC 483 ms
94,672 KB
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

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:
        x=UF.check()
        if len(x)==1: print(-1); continue
        for i in x:
            if i!=u: print(i+1); break
0