結果
| 問題 |
No.1647 Travel in Mitaru city 2
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-07 09:43:07 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,252 bytes |
| コンパイル時間 | 209 ms |
| コンパイル使用メモリ | 82,852 KB |
| 実行使用メモリ | 135,176 KB |
| 最終ジャッジ日時 | 2024-10-01 04:24:39 |
| 合計ジャッジ時間 | 21,218 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 WA * 7 |
ソースコード
from collections import deque
def dfs(H, W, G):
que = deque()
parents = [None] * (H + W)
nonvisited = set(range(H + W))
while nonvisited:
x = next(iter(nonvisited)) # get single element from set but not remove
que.append((x, -1, -1))
while que:
x, p, j = que.pop()
parents[x] = (p, j)
nonvisited.remove(x)
for y, i in G[x]:
if parents[x] == (y, i):
continue
if y in nonvisited:
que.append((y, x, i))
else:
return x, y, i, parents
print(-1)
exit()
H, W, N = map(int, input().split())
G = [[] for _ in range(H + W)]
points = [None] * N
for i in range(1, N + 1):
x, y = map(lambda x: int(x) - 1, input().split())
y += H
G[x].append((y, i))
G[y].append((x, i))
points[i - 1] = (x, y)
X, Y, I, parents = dfs(H, W, G)
path = [I]
while X != Y:
path.append(parents[X][1])
X, _ = parents[X]
if points[path[0] - 1][1] != points[path[1] - 1][1]:
assert points[path[0] - 1][0] == points[path[1] - 1][0]
path = path[::-1] # pathは偶数長なので単に反転させるだけでよい
print(len(path))
print(*path)