結果

問題 No.2290 UnUnion Find
ユーザー titiatitia
提出日時 2023-05-05 21:54:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,100 ms / 2,000 ms
コード長 1,288 bytes
コンパイル時間 793 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 55,604 KB
最終ジャッジ日時 2023-08-15 03:53:33
合計ジャッジ時間 44,564 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,000 KB
testcase_01 AC 18 ms
8,076 KB
testcase_02 AC 498 ms
35,284 KB
testcase_03 AC 785 ms
49,032 KB
testcase_04 AC 819 ms
49,844 KB
testcase_05 AC 813 ms
49,908 KB
testcase_06 AC 812 ms
49,936 KB
testcase_07 AC 818 ms
49,884 KB
testcase_08 AC 823 ms
48,996 KB
testcase_09 AC 813 ms
49,008 KB
testcase_10 AC 810 ms
49,940 KB
testcase_11 AC 829 ms
49,152 KB
testcase_12 AC 820 ms
49,936 KB
testcase_13 AC 827 ms
49,996 KB
testcase_14 AC 817 ms
49,820 KB
testcase_15 AC 830 ms
49,936 KB
testcase_16 AC 819 ms
49,000 KB
testcase_17 AC 812 ms
49,944 KB
testcase_18 AC 824 ms
49,848 KB
testcase_19 AC 944 ms
50,592 KB
testcase_20 AC 1,091 ms
55,560 KB
testcase_21 AC 566 ms
41,828 KB
testcase_22 AC 746 ms
45,384 KB
testcase_23 AC 759 ms
45,336 KB
testcase_24 AC 1,008 ms
52,776 KB
testcase_25 AC 695 ms
44,404 KB
testcase_26 AC 1,043 ms
54,012 KB
testcase_27 AC 1,074 ms
53,244 KB
testcase_28 AC 900 ms
48,364 KB
testcase_29 AC 1,033 ms
51,784 KB
testcase_30 AC 601 ms
42,404 KB
testcase_31 AC 1,023 ms
51,024 KB
testcase_32 AC 709 ms
44,484 KB
testcase_33 AC 855 ms
48,148 KB
testcase_34 AC 1,097 ms
55,604 KB
testcase_35 AC 1,100 ms
53,912 KB
testcase_36 AC 727 ms
44,976 KB
testcase_37 AC 828 ms
47,352 KB
testcase_38 AC 719 ms
44,764 KB
testcase_39 AC 755 ms
45,992 KB
testcase_40 AC 1,065 ms
53,204 KB
testcase_41 AC 696 ms
44,188 KB
testcase_42 AC 948 ms
50,084 KB
testcase_43 AC 927 ms
49,708 KB
testcase_44 AC 824 ms
49,852 KB
testcase_45 AC 870 ms
49,988 KB
testcase_46 AC 871 ms
49,940 KB
権限があれば一括ダウンロードができます

ソースコード

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)!=find(1):
        no_one=i

for i in range(N+1):
    if find(i)==find(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