結果

問題 No.2290 UnUnion Find
ユーザー komkompikomkompi
提出日時 2023-06-11 01:04:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 945 bytes
コンパイル時間 920 ms
コンパイル使用メモリ 86,876 KB
実行使用メモリ 114,252 KB
最終ジャッジ日時 2023-08-31 01:37:46
合計ジャッジ時間 34,661 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,560 KB
testcase_01 AC 70 ms
71,204 KB
testcase_02 AC 487 ms
78,688 KB
testcase_03 AC 442 ms
95,004 KB
testcase_04 AC 447 ms
94,960 KB
testcase_05 AC 450 ms
95,060 KB
testcase_06 AC 432 ms
95,148 KB
testcase_07 AC 426 ms
95,148 KB
testcase_08 AC 424 ms
95,108 KB
testcase_09 AC 430 ms
94,816 KB
testcase_10 AC 430 ms
95,036 KB
testcase_11 AC 445 ms
95,076 KB
testcase_12 AC 431 ms
94,680 KB
testcase_13 AC 443 ms
94,996 KB
testcase_14 AC 421 ms
94,916 KB
testcase_15 AC 431 ms
94,888 KB
testcase_16 AC 426 ms
94,912 KB
testcase_17 AC 427 ms
94,792 KB
testcase_18 AC 433 ms
95,072 KB
testcase_19 WA -
testcase_20 AC 717 ms
113,616 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 787 ms
104,348 KB
testcase_25 WA -
testcase_26 AC 754 ms
107,944 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 784 ms
101,824 KB
testcase_30 WA -
testcase_31 AC 789 ms
99,312 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 700 ms
114,252 KB
testcase_35 AC 771 ms
108,160 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 771 ms
104,988 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 429 ms
95,040 KB
testcase_45 AC 415 ms
94,416 KB
testcase_46 AC 427 ms
94,832 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)])

for _ in range(Q):
    q=input().split()
    if q[0]=="1":
        u,v=int(q[1])-1,int(q[2])-1
        unite(u,v)
        if find(u)!=u and u in is_par:
            is_par.remove(u)
        if find(v)!=v and v in is_par:
            is_par.remove(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