結果

問題 No.2290 UnUnion Find
ユーザー komkompikomkompi
提出日時 2023-06-11 01:16:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,125 ms / 2,000 ms
コード長 986 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 113,120 KB
最終ジャッジ日時 2023-08-31 01:39:10
合計ジャッジ時間 38,900 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,408 KB
testcase_01 AC 71 ms
71,268 KB
testcase_02 AC 488 ms
77,276 KB
testcase_03 AC 449 ms
98,036 KB
testcase_04 AC 476 ms
98,376 KB
testcase_05 AC 472 ms
98,344 KB
testcase_06 AC 472 ms
98,440 KB
testcase_07 AC 467 ms
98,320 KB
testcase_08 AC 464 ms
98,300 KB
testcase_09 AC 476 ms
98,424 KB
testcase_10 AC 466 ms
98,512 KB
testcase_11 AC 469 ms
98,496 KB
testcase_12 AC 460 ms
98,256 KB
testcase_13 AC 468 ms
98,508 KB
testcase_14 AC 470 ms
98,356 KB
testcase_15 AC 464 ms
98,564 KB
testcase_16 AC 471 ms
98,368 KB
testcase_17 AC 456 ms
98,340 KB
testcase_18 AC 460 ms
98,400 KB
testcase_19 AC 1,078 ms
101,584 KB
testcase_20 AC 783 ms
113,040 KB
testcase_21 AC 858 ms
82,276 KB
testcase_22 AC 1,017 ms
91,548 KB
testcase_23 AC 918 ms
87,640 KB
testcase_24 AC 995 ms
110,160 KB
testcase_25 AC 847 ms
84,348 KB
testcase_26 AC 681 ms
107,100 KB
testcase_27 AC 877 ms
103,920 KB
testcase_28 AC 1,013 ms
95,812 KB
testcase_29 AC 914 ms
107,376 KB
testcase_30 AC 816 ms
84,236 KB
testcase_31 AC 995 ms
104,056 KB
testcase_32 AC 834 ms
84,492 KB
testcase_33 AC 872 ms
92,024 KB
testcase_34 AC 722 ms
113,120 KB
testcase_35 AC 1,004 ms
107,084 KB
testcase_36 AC 913 ms
88,076 KB
testcase_37 AC 1,055 ms
92,244 KB
testcase_38 AC 913 ms
85,500 KB
testcase_39 AC 920 ms
87,336 KB
testcase_40 AC 867 ms
104,128 KB
testcase_41 AC 926 ms
86,256 KB
testcase_42 AC 1,113 ms
100,896 KB
testcase_43 AC 1,125 ms
99,044 KB
testcase_44 AC 479 ms
98,388 KB
testcase_45 AC 484 ms
99,488 KB
testcase_46 AC 525 ms
97,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
# Your code here!

N,Q=map(int,input().split())

par = [i for i in range(N+1)]
rank = [0]*(N+1)
def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x]) #経路圧縮
        return par[x]
def same(x,y):
    return find(x) == find(y)
def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
        return 0
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x]==rank[y]:rank[x]+=1


is_par=set([i for i in range(N)])

def recog(n):
    if n in is_par:
        if find(n)!=n:
            is_par.remove(n)
    else:
        if find(n)==n:
            is_par.add(n)

l,r=0,N-1
for _ in range(Q):
    q=input().split()
    if q[0]=="1":
        u,v=int(q[1])-1,int(q[2])-1
        unite(u,v)
        
        while l!=find(l):
            l+=1
        while r!=find(r):
            r-=1

    else:
        v=int(q[1])-1
        print(-1 if l==r else l+1 if same(v,r) else r+1)
                

0