結果
| 問題 |
No.1776 Love Triangle 2 (Hard)
|
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 12:53:06 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,909 bytes |
| コンパイル時間 | 209 ms |
| コンパイル使用メモリ | 82,316 KB |
| 実行使用メモリ | 848,948 KB |
| 最終ジャッジ日時 | 2025-06-12 12:54:40 |
| 合計ジャッジ時間 | 9,021 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 MLE * 1 -- * 171 |
ソースコード
from collections import deque
n, m = map(int, input().split())
X, Y, Z = map(int, input().split())
friends = set()
for _ in range(m):
a, b = map(int, input().split())
friends.add((a, b))
friends.add((b, a))
# Build adjacency list for non-friend pairs
adj = [[] for _ in range(n + 1)]
for u in range(1, n + 1):
for v in range(1, n + 1):
if u != v and (u, v) not in friends:
adj[u].append(v)
adj[u].sort() # Ensure lex order
# BFS setup
visited = set()
queue = deque()
initial_mask = 1 << (X - 1)
queue.append((X, initial_mask, False, False, [X]))
found = False
answer_k = None
answer_path = None
while queue:
current, mask, y_flag, z_flag, path = queue.popleft()
for neighbor in adj[current]:
if neighbor == X:
if len(path) >= 3 and y_flag and z_flag:
# Check if the cycle is valid (last step is non-friend)
# Since neighbor is in adj[current], current and X are non-friends
full_path = path + [X]
k = len(path)
if not found or k < answer_k or (k == answer_k and full_path < answer_path):
answer_k = k
answer_path = full_path
found = True
else:
if not (mask & (1 << (neighbor - 1))):
new_mask = mask | (1 << (neighbor - 1))
new_y = y_flag or (neighbor == Y)
new_z = z_flag or (neighbor == Z)
state = (neighbor, new_mask, new_y, new_z)
if state not in visited:
visited.add(state)
new_path = path + [neighbor]
queue.append((neighbor, new_mask, new_y, new_z, new_path))
if found:
break # Exit early if found to ensure lex order
if found:
print(answer_k)
print(' '.join(map(str, answer_path)))
else:
print(-1)
gew1fw