結果

問題 No.2290 UnUnion Find
ユーザー komkompikomkompi
提出日時 2023-06-11 01:12:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 999 bytes
コンパイル時間 717 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 113,888 KB
最終ジャッジ日時 2023-08-31 01:38:29
合計ジャッジ時間 34,504 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,172 KB
testcase_01 AC 71 ms
71,352 KB
testcase_02 AC 497 ms
78,556 KB
testcase_03 AC 425 ms
94,508 KB
testcase_04 AC 439 ms
94,312 KB
testcase_05 AC 458 ms
94,548 KB
testcase_06 AC 422 ms
94,432 KB
testcase_07 AC 440 ms
94,412 KB
testcase_08 AC 427 ms
94,532 KB
testcase_09 AC 433 ms
94,424 KB
testcase_10 AC 424 ms
94,244 KB
testcase_11 AC 426 ms
94,588 KB
testcase_12 AC 422 ms
94,552 KB
testcase_13 AC 429 ms
94,416 KB
testcase_14 AC 424 ms
94,484 KB
testcase_15 AC 423 ms
94,592 KB
testcase_16 AC 429 ms
94,084 KB
testcase_17 AC 426 ms
94,440 KB
testcase_18 AC 439 ms
94,108 KB
testcase_19 WA -
testcase_20 AC 704 ms
113,748 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 825 ms
104,448 KB
testcase_25 WA -
testcase_26 AC 757 ms
108,240 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 807 ms
101,788 KB
testcase_30 WA -
testcase_31 AC 792 ms
99,248 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 714 ms
113,888 KB
testcase_35 AC 799 ms
107,940 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 778 ms
105,072 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 437 ms
94,528 KB
testcase_45 AC 461 ms
94,668 KB
testcase_46 AC 434 ms
94,488 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)

for _ in range(Q):
    q=input().split()
    if q[0]=="1":
        u,v=int(q[1])-1,int(q[2])-1
        unite(u,v)
        
        recog(u)
        recog(v)

    else:
        v=int(q[1])-1
        ans=-1
        for u in is_par:
            if u!=find(v):
                ans=u+1
                break
        print(ans)
                

0