結果

問題 No.2290 UnUnion Find
ユーザー navel_tosnavel_tos
提出日時 2023-06-01 12:12:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 810 ms / 2,000 ms
コード長 1,208 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 113,924 KB
最終ジャッジ日時 2023-08-28 02:01:08
合計ジャッジ時間 34,214 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,252 KB
testcase_01 AC 69 ms
71,236 KB
testcase_02 AC 561 ms
78,336 KB
testcase_03 AC 466 ms
94,544 KB
testcase_04 AC 460 ms
94,404 KB
testcase_05 AC 469 ms
94,184 KB
testcase_06 AC 461 ms
94,468 KB
testcase_07 AC 489 ms
94,380 KB
testcase_08 AC 487 ms
94,484 KB
testcase_09 AC 490 ms
94,536 KB
testcase_10 AC 472 ms
94,404 KB
testcase_11 AC 484 ms
94,644 KB
testcase_12 AC 472 ms
94,588 KB
testcase_13 AC 473 ms
94,404 KB
testcase_14 AC 486 ms
94,600 KB
testcase_15 AC 484 ms
94,472 KB
testcase_16 AC 466 ms
94,548 KB
testcase_17 AC 480 ms
94,484 KB
testcase_18 AC 477 ms
94,456 KB
testcase_19 AC 699 ms
96,724 KB
testcase_20 AC 711 ms
113,924 KB
testcase_21 AC 729 ms
82,024 KB
testcase_22 AC 739 ms
85,908 KB
testcase_23 AC 723 ms
86,156 KB
testcase_24 AC 581 ms
104,168 KB
testcase_25 AC 718 ms
83,272 KB
testcase_26 AC 611 ms
107,964 KB
testcase_27 AC 695 ms
104,652 KB
testcase_28 AC 716 ms
92,412 KB
testcase_29 AC 730 ms
101,420 KB
testcase_30 AC 780 ms
82,452 KB
testcase_31 AC 689 ms
98,824 KB
testcase_32 AC 741 ms
83,244 KB
testcase_33 AC 730 ms
90,808 KB
testcase_34 AC 694 ms
113,740 KB
testcase_35 AC 741 ms
107,552 KB
testcase_36 AC 754 ms
84,856 KB
testcase_37 AC 737 ms
89,296 KB
testcase_38 AC 752 ms
84,512 KB
testcase_39 AC 721 ms
86,844 KB
testcase_40 AC 810 ms
104,588 KB
testcase_41 AC 784 ms
84,480 KB
testcase_42 AC 784 ms
96,368 KB
testcase_43 AC 772 ms
94,768 KB
testcase_44 AC 490 ms
94,416 KB
testcase_45 AC 512 ms
94,580 KB
testcase_46 AC 547 ms
94,668 KB
権限があれば一括ダウンロードができます

ソースコード

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