結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-06-20 17:33:19 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,426 bytes |
| コンパイル時間 | 271 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 824,732 KB |
| 最終ジャッジ日時 | 2024-06-20 17:33:28 |
| 合計ジャッジ時間 | 8,031 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 MLE * 1 -- * 12 |
ソースコード
from collections import defaultdict
def fnd(p, x):
if p[x] != x:
p[x] = fnd(p, p[x])
return p[x]
def uni(p, r, x, y):
rx, ry = fnd(p, x), fnd(p, y)
if rx != ry:
if r[rx] > r[ry]:
p[ry] = rx
elif r[rx] < r[ry]:
p[rx] = ry
else:
p[ry] = rx
r[rx] += 1
def conn(g, n):
p = list(range(n))
r = [0] * n
for u in g:
for v in g[u]:
uni(p, r, u, v)
root = fnd(p, 0)
return all(fnd(p, i) == root for i in range(n))
def solve():
n = int(input().strip())
b = [tuple(map(int, input().strip().split())) for _ in range(n - 1)]
g = defaultdict(set)
for u, v in b:
g[u].add(v)
g[v].add(u)
for u, v in b:
g[u].remove(v)
g[v].remove(u)
if not conn(g, n):
pairs = {(i, j) for i in range(n) for j in range(i + 1, n) if j not in g[i]}
can_reconnect = False
for x, y in pairs:
g[x].add(y)
g[y].add(x)
if conn(g, n):
can_reconnect = True
g[x].remove(y)
g[y].remove(x)
if can_reconnect:
break
if not can_reconnect:
print("Alice")
return
g[u].add(v)
g[v].add(u)
print("Bob")
solve()