結果

問題 No.1647 Travel in Mitaru city 2
ユーザー H3PO4H3PO4
提出日時 2024-04-07 09:43:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,252 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 134,624 KB
最終ジャッジ日時 2024-04-07 09:43:31
合計ジャッジ時間 22,153 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,604 KB
testcase_01 AC 38 ms
55,604 KB
testcase_02 AC 38 ms
55,604 KB
testcase_03 AC 42 ms
55,604 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 347 ms
132,256 KB
testcase_09 AC 311 ms
128,828 KB
testcase_10 AC 349 ms
132,080 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 297 ms
132,020 KB
testcase_14 AC 360 ms
132,316 KB
testcase_15 AC 384 ms
132,172 KB
testcase_16 AC 341 ms
131,888 KB
testcase_17 AC 325 ms
128,912 KB
testcase_18 AC 334 ms
132,820 KB
testcase_19 AC 342 ms
132,720 KB
testcase_20 AC 297 ms
132,700 KB
testcase_21 AC 273 ms
132,708 KB
testcase_22 AC 360 ms
132,684 KB
testcase_23 AC 351 ms
132,708 KB
testcase_24 AC 315 ms
132,708 KB
testcase_25 AC 362 ms
132,692 KB
testcase_26 AC 342 ms
132,704 KB
testcase_27 AC 364 ms
132,716 KB
testcase_28 AC 391 ms
132,772 KB
testcase_29 AC 342 ms
132,756 KB
testcase_30 AC 265 ms
132,776 KB
testcase_31 AC 352 ms
132,772 KB
testcase_32 AC 272 ms
132,768 KB
testcase_33 AC 303 ms
132,768 KB
testcase_34 AC 326 ms
132,772 KB
testcase_35 AC 303 ms
132,772 KB
testcase_36 AC 335 ms
132,776 KB
testcase_37 AC 328 ms
132,776 KB
testcase_38 AC 372 ms
132,724 KB
testcase_39 AC 311 ms
133,324 KB
testcase_40 AC 343 ms
132,836 KB
testcase_41 AC 358 ms
132,828 KB
testcase_42 AC 338 ms
132,856 KB
testcase_43 AC 46 ms
64,184 KB
testcase_44 AC 71 ms
99,712 KB
testcase_45 AC 271 ms
134,612 KB
testcase_46 AC 369 ms
134,604 KB
testcase_47 AC 265 ms
134,616 KB
testcase_48 AC 305 ms
134,624 KB
testcase_49 WA -
testcase_50 AC 257 ms
131,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0