結果
| 問題 |
No.2290 UnUnion Find
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-06 20:53:18 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 772 ms / 2,000 ms |
| コード長 | 1,337 bytes |
| コンパイル時間 | 196 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 88,604 KB |
| 最終ジャッジ日時 | 2024-11-24 02:03:36 |
| 合計ジャッジ時間 | 32,939 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 46 |
ソースコード
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()
while used[dq[-1]]:
dq.pop()
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)