結果
| 問題 |
No.2290 UnUnion Find
|
| コンテスト | |
| ユーザー |
hir355
|
| 提出日時 | 2023-05-05 21:34:20 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,545 bytes |
| コンパイル時間 | 421 ms |
| コンパイル使用メモリ | 81,792 KB |
| 実行使用メモリ | 114,168 KB |
| 最終ジャッジ日時 | 2024-11-23 06:19:31 |
| 合計ジャッジ時間 | 26,087 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 11 WA * 35 |
ソースコード
class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n + 1)
self.size = [1] * (n + 1)
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.rank[x] < self.rank[y]:
self.par[x] = y
self.size[y] += self.size[x]
else:
self.par[y] = x
self.size[x] += self.size[y]
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def same_check(self, x, y):
return self.find(x) == self.find(y)
n, q = map(int, input().split())
que = [list(map(int, input().split())) for _ in range(q)]
uf = UnionFind(n)
uf2 = UnionFind(n)
for i in range(q):
if que[i][0] == 1:
uf.unite(que[i][1] - 1, que[i][2] - 1)
if uf.size[uf.find(0)] == n:
for i in range(n):
if not uf2.same_check(0, i):
x = i
break
else:
for i in range(n):
if not uf2.same_check(0, i):
x = i
uf = UnionFind(n)
for i in range(q):
if que[i][0] == 1:
uf.unite(que[i][1] - 1, que[i][2] - 1)
else:
if uf.same_check(que[i][1] - 1, 0):
if uf.same_check(que[i][1] - 1, x):
print(-1)
else:
print(x + 1)
else:
print(1)
hir355