結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-28 08:19:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 702 ms / 2,500 ms
コード長 857 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 82,904 KB
実行使用メモリ 211,528 KB
最終ジャッジ日時 2024-04-14 15:51:09
合計ジャッジ時間 24,440 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,224 KB
testcase_01 AC 38 ms
53,024 KB
testcase_02 AC 39 ms
52,872 KB
testcase_03 AC 42 ms
54,012 KB
testcase_04 AC 68 ms
73,072 KB
testcase_05 AC 41 ms
54,428 KB
testcase_06 AC 43 ms
54,800 KB
testcase_07 AC 70 ms
74,428 KB
testcase_08 AC 503 ms
174,272 KB
testcase_09 AC 463 ms
167,552 KB
testcase_10 AC 506 ms
176,760 KB
testcase_11 AC 365 ms
133,956 KB
testcase_12 AC 369 ms
128,264 KB
testcase_13 AC 392 ms
139,504 KB
testcase_14 AC 610 ms
190,488 KB
testcase_15 AC 510 ms
177,232 KB
testcase_16 AC 496 ms
179,544 KB
testcase_17 AC 391 ms
143,768 KB
testcase_18 AC 373 ms
131,476 KB
testcase_19 AC 500 ms
178,812 KB
testcase_20 AC 451 ms
159,012 KB
testcase_21 AC 506 ms
186,476 KB
testcase_22 AC 675 ms
202,760 KB
testcase_23 AC 551 ms
198,120 KB
testcase_24 AC 510 ms
170,272 KB
testcase_25 AC 480 ms
165,652 KB
testcase_26 AC 548 ms
199,596 KB
testcase_27 AC 552 ms
201,620 KB
testcase_28 AC 563 ms
206,116 KB
testcase_29 AC 559 ms
206,556 KB
testcase_30 AC 494 ms
176,320 KB
testcase_31 AC 561 ms
206,044 KB
testcase_32 AC 527 ms
196,052 KB
testcase_33 AC 461 ms
159,944 KB
testcase_34 AC 570 ms
207,016 KB
testcase_35 AC 470 ms
164,292 KB
testcase_36 AC 558 ms
199,700 KB
testcase_37 AC 556 ms
206,152 KB
testcase_38 AC 702 ms
211,528 KB
testcase_39 AC 406 ms
132,772 KB
testcase_40 AC 474 ms
153,936 KB
testcase_41 AC 632 ms
195,948 KB
testcase_42 AC 569 ms
165,008 KB
testcase_43 AC 44 ms
59,248 KB
testcase_44 AC 48 ms
65,432 KB
testcase_45 AC 276 ms
99,120 KB
testcase_46 AC 411 ms
100,272 KB
testcase_47 AC 298 ms
100,060 KB
testcase_48 AC 358 ms
100,592 KB
testcase_49 AC 273 ms
98,648 KB
testcase_50 AC 271 ms
98,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(500000)


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
    visited[i] = True

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

        t, start = dfs(j, i)
        if start != -1:
            t.append(idx)

            if start == i:
                output(t, i < h)
                exit()

            return t, start

    return [], -1


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