結果

問題 No.2290 UnUnion Find
ユーザー neuphyneuphy
提出日時 2023-05-06 20:51:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,435 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 91,196 KB
最終ジャッジ日時 2024-05-03 04:53:03
合計ジャッジ時間 32,337 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,220 KB
testcase_01 AC 38 ms
55,468 KB
testcase_02 AC 461 ms
77,512 KB
testcase_03 WA -
testcase_04 AC 430 ms
83,536 KB
testcase_05 AC 425 ms
83,308 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 397 ms
83,428 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 498 ms
83,476 KB
testcase_12 AC 477 ms
83,676 KB
testcase_13 AC 411 ms
83,560 KB
testcase_14 AC 421 ms
83,412 KB
testcase_15 AC 410 ms
83,588 KB
testcase_16 AC 408 ms
83,544 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 913 ms
88,480 KB
testcase_20 AC 707 ms
91,196 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 787 ms
90,140 KB
testcase_25 WA -
testcase_26 AC 750 ms
90,536 KB
testcase_27 AC 784 ms
90,284 KB
testcase_28 AC 730 ms
86,212 KB
testcase_29 AC 782 ms
90,160 KB
testcase_30 WA -
testcase_31 AC 829 ms
88,720 KB
testcase_32 AC 726 ms
82,776 KB
testcase_33 WA -
testcase_34 AC 707 ms
90,860 KB
testcase_35 AC 799 ms
90,344 KB
testcase_36 WA -
testcase_37 AC 778 ms
85,420 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 752 ms
90,608 KB
testcase_41 WA -
testcase_42 AC 801 ms
87,604 KB
testcase_43 WA -
testcase_44 AC 421 ms
83,368 KB
testcase_45 WA -
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Union_Find():
    def __init__(self, N):
        self.size = [1] * N
        self.parent = [i for i in range(N)]

    def find(self, x):
        if self.parent[x] == x:
            return x
        self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def unit(self, x, y):
        x, y = self.find(x), self.find(y)
        
        if x == y:
            return x
        
        if self.size[x] < self.size[y]:
            x, y = y, x
        
        self.size[x] += self.size[y]
        self.parent[y] = x
        return x


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

UF = Union_Find(N)

dq = deque()
for i in range(N):
    dq.append(i)

used = [False] * N

for i in range(Q):
    
    q = list(map(int, input().split()))
    
    if q[0] == 1:
        
        q[1] -= 1
        q[2] -= 1
        used[UF.find(q[1])] = True
        used[UF.find(q[2])] = True
        
        used[UF.unit(q[1], q[2])] = False

        while used[dq[0]]:
            dq.popleft()

        dq.rotate()

        while used[dq[0]]:
            dq.popleft()

        dq.rotate()

        while used[dq[0]]:
            dq.popleft()
    
    else:

        q[1] -= 1

        if len(dq) == 1:
            print(-1)
        elif UF.is_same(dq[0], q[1]):
            print(dq[-1] + 1)
        else:
            print(dq[0] + 1)
0