結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,180 KB
testcase_01 AC 38 ms
53,272 KB
testcase_02 AC 38 ms
53,444 KB
testcase_03 AC 42 ms
55,724 KB
testcase_04 AC 68 ms
73,992 KB
testcase_05 AC 41 ms
53,512 KB
testcase_06 AC 43 ms
56,044 KB
testcase_07 AC 69 ms
74,376 KB
testcase_08 AC 508 ms
181,212 KB
testcase_09 AC 468 ms
170,768 KB
testcase_10 AC 516 ms
183,316 KB
testcase_11 AC 359 ms
133,632 KB
testcase_12 AC 367 ms
128,868 KB
testcase_13 AC 378 ms
139,844 KB
testcase_14 AC 626 ms
196,984 KB
testcase_15 AC 528 ms
191,588 KB
testcase_16 AC 492 ms
180,264 KB
testcase_17 AC 386 ms
144,320 KB
testcase_18 AC 372 ms
130,880 KB
testcase_19 AC 494 ms
182,436 KB
testcase_20 AC 456 ms
161,616 KB
testcase_21 AC 515 ms
189,280 KB
testcase_22 AC 687 ms
207,832 KB
testcase_23 AC 566 ms
204,832 KB
testcase_24 AC 521 ms
176,348 KB
testcase_25 AC 496 ms
178,024 KB
testcase_26 AC 555 ms
204,836 KB
testcase_27 AC 566 ms
206,368 KB
testcase_28 AC 570 ms
211,316 KB
testcase_29 AC 580 ms
210,688 KB
testcase_30 AC 527 ms
189,804 KB
testcase_31 AC 576 ms
212,364 KB
testcase_32 AC 539 ms
199,600 KB
testcase_33 AC 459 ms
163,772 KB
testcase_34 AC 572 ms
211,500 KB
testcase_35 AC 498 ms
175,500 KB
testcase_36 AC 569 ms
210,540 KB
testcase_37 AC 568 ms
212,164 KB
testcase_38 AC 690 ms
201,876 KB
testcase_39 AC 409 ms
133,744 KB
testcase_40 AC 466 ms
152,772 KB
testcase_41 AC 666 ms
211,392 KB
testcase_42 AC 797 ms
248,556 KB
testcase_43 AC 46 ms
61,660 KB
testcase_44 AC 47 ms
66,776 KB
testcase_45 AC 274 ms
98,852 KB
testcase_46 AC 421 ms
101,360 KB
testcase_47 AC 304 ms
100,284 KB
testcase_48 AC 375 ms
101,200 KB
testcase_49 AC 267 ms
98,932 KB
testcase_50 AC 268 ms
98,696 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):
    t.reverse()
    if not t[0][1]:
        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, is_h))

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

            return t, start

    return None


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