結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-27 23:35:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 785 ms / 2,500 ms
コード長 907 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 246,572 KB
最終ジャッジ日時 2024-04-14 15:45:59
合計ジャッジ時間 24,435 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,660 KB
testcase_01 AC 38 ms
53,940 KB
testcase_02 AC 38 ms
53,020 KB
testcase_03 AC 42 ms
54,220 KB
testcase_04 AC 68 ms
73,052 KB
testcase_05 AC 41 ms
53,776 KB
testcase_06 AC 44 ms
54,184 KB
testcase_07 AC 69 ms
74,172 KB
testcase_08 AC 488 ms
175,220 KB
testcase_09 AC 449 ms
169,804 KB
testcase_10 AC 482 ms
176,976 KB
testcase_11 AC 353 ms
133,520 KB
testcase_12 AC 348 ms
128,364 KB
testcase_13 AC 368 ms
139,832 KB
testcase_14 AC 596 ms
191,196 KB
testcase_15 AC 490 ms
178,560 KB
testcase_16 AC 474 ms
181,104 KB
testcase_17 AC 380 ms
143,536 KB
testcase_18 AC 359 ms
131,960 KB
testcase_19 AC 475 ms
179,060 KB
testcase_20 AC 438 ms
159,136 KB
testcase_21 AC 488 ms
188,216 KB
testcase_22 AC 647 ms
205,468 KB
testcase_23 AC 532 ms
201,420 KB
testcase_24 AC 500 ms
172,368 KB
testcase_25 AC 465 ms
167,560 KB
testcase_26 AC 559 ms
200,928 KB
testcase_27 AC 535 ms
203,164 KB
testcase_28 AC 551 ms
208,632 KB
testcase_29 AC 550 ms
208,224 KB
testcase_30 AC 490 ms
179,924 KB
testcase_31 AC 555 ms
208,668 KB
testcase_32 AC 511 ms
195,964 KB
testcase_33 AC 444 ms
160,116 KB
testcase_34 AC 551 ms
208,896 KB
testcase_35 AC 461 ms
165,460 KB
testcase_36 AC 548 ms
205,164 KB
testcase_37 AC 554 ms
208,532 KB
testcase_38 AC 645 ms
199,020 KB
testcase_39 AC 397 ms
132,124 KB
testcase_40 AC 448 ms
146,700 KB
testcase_41 AC 660 ms
210,000 KB
testcase_42 AC 785 ms
246,572 KB
testcase_43 AC 46 ms
60,944 KB
testcase_44 AC 46 ms
65,516 KB
testcase_45 AC 270 ms
98,912 KB
testcase_46 AC 431 ms
101,128 KB
testcase_47 AC 297 ms
100,172 KB
testcase_48 AC 374 ms
101,220 KB
testcase_49 AC 261 ms
98,748 KB
testcase_50 AC 267 ms
98,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)


h, w, n = map(int, input().split())

edge = [[] for _ in range(h + w)]

for idx in range(n):
    x, y = [int(i) - 1 for i in input().split()]
    y = y + h
    edge[x].append((y, idx))
    edge[y].append((x, idx))

visited = [False] * (h + w)


def output(t, is_h):
    t.reverse()
    if not is_h:
        t = t[1:] + t[:1]

    print(len(t))
    print(*(i+1 for i in t))


def dfs(i, prev=-1):
    if visited[i]:
        return [], i
    is_h = i < h
    visited[i] = True

    for j, idx in edge[i]:
        if j == prev:
            continue

        result = dfs(j, i)
        if result is not None:
            t, start = result
            t.append(idx)

            if start == i:
                output(t, is_h)
                exit()

            return t, start

    return None


for i in range(h + w):
    if not visited[i]:
        dfs(i)
print(-1)
0