結果
| 問題 |
No.2290 UnUnion Find
|
| コンテスト | |
| ユーザー |
lllllll88938494
|
| 提出日時 | 2023-05-05 22:30:53 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 964 ms / 2,000 ms |
| コード長 | 1,900 bytes |
| コンパイル時間 | 373 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 109,568 KB |
| 最終ジャッジ日時 | 2024-11-23 09:26:43 |
| 合計ジャッジ時間 | 37,681 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 46 |
ソースコード
class UnionFind:
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
from collections import deque
n,q= map(int,input().split())
nots = set()
uf = UnionFind(n+1)
for i in range(1,n+1):
nots.add(i)
s=set()
lis=deque()
for i in range(q):
t = list(map(int,input().split()))
if t[0] == 1:
x,y=t[1],t[2]
if x > y:
x,y = y,x
nots.discard(x)
nots.discard(y)
uf.union(x,y)
fx=uf.find(x)
if fx not in s:
s.add(x)
lis.append(x)
else:
x=t[1]
fx=uf.find(x)
if fx not in s:
xx=x+1
if xx == n+1:
xx = 1
print(xx)
else:
if uf.size(fx) == n:
print(-1)
elif len(nots) > 0:
for i in nots:
print(i)
break
else:
while len(lis)>1:
if fx == lis[-1]:
lis.appendleft(lis.pop())
else:
if fx == uf.find(lis[-1]):
lis.pop()
else:
print(lis[-1])
break
lllllll88938494