結果
| 問題 |
No.2290 UnUnion Find
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-06 20:49:19 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,354 bytes |
| コンパイル時間 | 274 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 89,876 KB |
| 最終ジャッジ日時 | 2024-11-24 02:01:08 |
| 合計ジャッジ時間 | 31,639 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 29 WA * 17 |
ソースコード
from collections import deque
class Union_Find():
def __init__(self, N):
self.size = [1] * N
self.parent = [0] * N
for i in range(N):
self.parent[i] = i
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()
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)