結果

問題 No.2290 UnUnion Find
ユーザー titiatitia
提出日時 2023-05-05 21:39:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,276 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 57,984 KB
最終ジャッジ日時 2024-05-02 15:37:01
合計ジャッジ時間 39,855 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,752 KB
testcase_01 AC 29 ms
10,752 KB
testcase_02 AC 538 ms
37,632 KB
testcase_03 AC 761 ms
51,712 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 919 ms
55,168 KB
testcase_25 WA -
testcase_26 AC 981 ms
56,576 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

QS=[list(map(int,input().split())) for i in range(Q)]
last=10**7
for i in range(Q):
    if len(QS[i])==2:
        continue
    com,u,v=QS[i]

    if com==1:
        if find(u)!=find(v):
            if Nodes[find(u)]+Nodes[find(v)]==N:
                last=i
                break
            else:
                Union(u,v)

ANS=[-1]*(N+1)

for i in range(N+1):
    if find(i)!=1:
        no_one=i

for i in range(N+1):
    if find(i)==1:
        ANS[i]=no_one
    else:
        ANS[i]=1

for i in range(Q):
    if len(QS[i])==3:
        continue
    com,u=QS[i]

    if com==2:
        if i>=last:
            print(-1)
        else:
            print(ANS[u])
0