結果

問題 No.2290 UnUnion Find
ユーザー ニックネームニックネーム
提出日時 2023-05-05 22:06:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 894 ms / 2,000 ms
コード長 722 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 108,928 KB
最終ジャッジ日時 2024-05-02 16:25:22
合計ジャッジ時間 35,999 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
51,840 KB
testcase_01 AC 42 ms
52,096 KB
testcase_02 AC 516 ms
76,288 KB
testcase_03 AC 431 ms
91,264 KB
testcase_04 AC 443 ms
91,264 KB
testcase_05 AC 464 ms
91,520 KB
testcase_06 AC 460 ms
91,264 KB
testcase_07 AC 467 ms
91,136 KB
testcase_08 AC 473 ms
91,008 KB
testcase_09 AC 469 ms
91,520 KB
testcase_10 AC 449 ms
91,264 KB
testcase_11 AC 502 ms
91,520 KB
testcase_12 AC 459 ms
91,136 KB
testcase_13 AC 459 ms
91,264 KB
testcase_14 AC 466 ms
91,520 KB
testcase_15 AC 475 ms
91,136 KB
testcase_16 AC 459 ms
91,648 KB
testcase_17 AC 458 ms
91,008 KB
testcase_18 AC 472 ms
91,648 KB
testcase_19 AC 859 ms
93,440 KB
testcase_20 AC 765 ms
108,928 KB
testcase_21 AC 700 ms
79,444 KB
testcase_22 AC 792 ms
83,968 KB
testcase_23 AC 832 ms
84,096 KB
testcase_24 AC 706 ms
100,352 KB
testcase_25 AC 770 ms
81,024 KB
testcase_26 AC 707 ms
103,424 KB
testcase_27 AC 791 ms
100,352 KB
testcase_28 AC 812 ms
89,600 KB
testcase_29 AC 824 ms
98,048 KB
testcase_30 AC 775 ms
79,808 KB
testcase_31 AC 791 ms
95,616 KB
testcase_32 AC 785 ms
81,152 KB
testcase_33 AC 870 ms
88,320 KB
testcase_34 AC 742 ms
108,928 KB
testcase_35 AC 841 ms
103,296 KB
testcase_36 AC 807 ms
82,176 KB
testcase_37 AC 881 ms
86,656 KB
testcase_38 AC 825 ms
81,664 KB
testcase_39 AC 810 ms
84,480 KB
testcase_40 AC 858 ms
100,864 KB
testcase_41 AC 844 ms
80,856 KB
testcase_42 AC 844 ms
93,184 KB
testcase_43 AC 894 ms
91,392 KB
testcase_44 AC 459 ms
91,520 KB
testcase_45 AC 488 ms
91,648 KB
testcase_46 AC 511 ms
91,392 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