結果

問題 No.2290 UnUnion Find
ユーザー ntudantuda
提出日時 2023-05-06 12:33:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 984 ms / 2,000 ms
コード長 650 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 109,288 KB
最終ジャッジ日時 2024-05-03 01:20:10
合計ジャッジ時間 36,205 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,820 KB
testcase_01 AC 39 ms
52,380 KB
testcase_02 AC 514 ms
76,264 KB
testcase_03 AC 417 ms
91,436 KB
testcase_04 AC 448 ms
91,640 KB
testcase_05 AC 448 ms
91,348 KB
testcase_06 AC 444 ms
91,352 KB
testcase_07 AC 448 ms
91,464 KB
testcase_08 AC 453 ms
91,472 KB
testcase_09 AC 443 ms
91,604 KB
testcase_10 AC 443 ms
91,352 KB
testcase_11 AC 447 ms
91,476 KB
testcase_12 AC 457 ms
91,508 KB
testcase_13 AC 442 ms
91,340 KB
testcase_14 AC 451 ms
91,408 KB
testcase_15 AC 448 ms
91,420 KB
testcase_16 AC 447 ms
91,356 KB
testcase_17 AC 461 ms
91,648 KB
testcase_18 AC 458 ms
91,456 KB
testcase_19 AC 911 ms
93,404 KB
testcase_20 AC 828 ms
109,288 KB
testcase_21 AC 717 ms
80,040 KB
testcase_22 AC 843 ms
83,496 KB
testcase_23 AC 889 ms
83,720 KB
testcase_24 AC 722 ms
100,500 KB
testcase_25 AC 872 ms
82,052 KB
testcase_26 AC 689 ms
103,508 KB
testcase_27 AC 906 ms
100,660 KB
testcase_28 AC 908 ms
89,532 KB
testcase_29 AC 922 ms
98,000 KB
testcase_30 AC 814 ms
80,792 KB
testcase_31 AC 870 ms
95,384 KB
testcase_32 AC 837 ms
81,564 KB
testcase_33 AC 895 ms
88,420 KB
testcase_34 AC 843 ms
109,248 KB
testcase_35 AC 832 ms
103,640 KB
testcase_36 AC 875 ms
82,032 KB
testcase_37 AC 923 ms
86,972 KB
testcase_38 AC 844 ms
82,100 KB
testcase_39 AC 870 ms
84,512 KB
testcase_40 AC 923 ms
100,556 KB
testcase_41 AC 890 ms
82,144 KB
testcase_42 AC 984 ms
93,408 KB
testcase_43 AC 930 ms
91,536 KB
testcase_44 AC 458 ms
91,340 KB
testcase_45 AC 474 ms
91,528 KB
testcase_46 AC 516 ms
91,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,Q = map(int,input().split())
P = [-1] * (N+1)
Ps = set(range(1,N+1))

def root(x):
    if P[x] < 0: return x
    P[x] = root(P[x])  # 経路圧縮
    return P[x]

def unite(x,y):
    x = root(x)
    y = root(y)
    if x == y: return
    if x > y: x,y = y,x
    P[x] += P[y]
    P[y] = x
    Ps.remove(y)

def same(x,y):
    return root(x) == root(y)

for _ in range(Q):
    qu = list(map(int,input().split()))
    if qu[0] == 1:
        unite(qu[1],qu[2])
    else:
        if len(Ps) == 1:
            print(-1)
        else:
            for p in Ps:
                if not same(p,qu[1]):
                    print(p)
                    break


0