結果
| 問題 | No.2290 UnUnion Find |
| コンテスト | |
| ユーザー |
ntuda
|
| 提出日時 | 2023-05-06 12:33:09 |
| 言語 | PyPy3 (7.3.23 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 617 ms / 2,000 ms |
| + 75µs | |
| コード長 | 650 bytes |
| 記録 | |
| コンパイル時間 | 64 ms |
| コンパイル使用メモリ | 82,876 KB |
| 実行使用メモリ | 116,848 KB |
| 最終ジャッジ日時 | 2026-09-28 19:46:11 |
| 合計ジャッジ時間 | 26,387 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 46 |
ソースコード
N,Q = map(int,input().split())
P = [-1] * (N+1)
Ps = set(range(1,N+1))
def root(x):
if P[x] < 0: return x
P[x] = root(P[x]) # 経路圧縮
return P[x]
def unite(x,y):
x = root(x)
y = root(y)
if x == y: return
if x > y: x,y = y,x
P[x] += P[y]
P[y] = x
Ps.remove(y)
def same(x,y):
return root(x) == root(y)
for _ in range(Q):
qu = list(map(int,input().split()))
if qu[0] == 1:
unite(qu[1],qu[2])
else:
if len(Ps) == 1:
print(-1)
else:
for p in Ps:
if not same(p,qu[1]):
print(p)
break
ntuda