結果

問題 No.2290 UnUnion Find
ユーザー ニックネームニックネーム
提出日時 2023-05-05 22:06:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 910 ms / 2,000 ms
コード長 722 bytes
コンパイル時間 562 ms
コンパイル使用メモリ 86,864 KB
実行使用メモリ 110,936 KB
最終ジャッジ日時 2023-08-15 04:13:07
合計ジャッジ時間 36,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,112 KB
testcase_01 AC 74 ms
71,168 KB
testcase_02 AC 507 ms
78,248 KB
testcase_03 AC 436 ms
93,212 KB
testcase_04 AC 455 ms
93,068 KB
testcase_05 AC 470 ms
92,988 KB
testcase_06 AC 459 ms
93,212 KB
testcase_07 AC 470 ms
93,056 KB
testcase_08 AC 476 ms
93,020 KB
testcase_09 AC 473 ms
93,020 KB
testcase_10 AC 461 ms
93,020 KB
testcase_11 AC 476 ms
92,988 KB
testcase_12 AC 462 ms
92,828 KB
testcase_13 AC 456 ms
93,004 KB
testcase_14 AC 472 ms
93,044 KB
testcase_15 AC 471 ms
93,212 KB
testcase_16 AC 456 ms
93,192 KB
testcase_17 AC 470 ms
93,168 KB
testcase_18 AC 476 ms
93,108 KB
testcase_19 AC 886 ms
95,308 KB
testcase_20 AC 781 ms
110,884 KB
testcase_21 AC 718 ms
81,436 KB
testcase_22 AC 794 ms
85,388 KB
testcase_23 AC 846 ms
86,740 KB
testcase_24 AC 712 ms
102,072 KB
testcase_25 AC 765 ms
84,336 KB
testcase_26 AC 717 ms
105,268 KB
testcase_27 AC 805 ms
102,160 KB
testcase_28 AC 811 ms
91,176 KB
testcase_29 AC 850 ms
99,672 KB
testcase_30 AC 761 ms
82,812 KB
testcase_31 AC 805 ms
97,352 KB
testcase_32 AC 796 ms
85,304 KB
testcase_33 AC 857 ms
89,904 KB
testcase_34 AC 750 ms
110,936 KB
testcase_35 AC 832 ms
105,196 KB
testcase_36 AC 805 ms
84,192 KB
testcase_37 AC 871 ms
88,708 KB
testcase_38 AC 803 ms
84,264 KB
testcase_39 AC 824 ms
86,352 KB
testcase_40 AC 875 ms
102,208 KB
testcase_41 AC 834 ms
84,208 KB
testcase_42 AC 883 ms
94,920 KB
testcase_43 AC 910 ms
92,608 KB
testcase_44 AC 463 ms
93,064 KB
testcase_45 AC 493 ms
92,852 KB
testcase_46 AC 516 ms
92,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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
0