結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-27 23:36:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 803 ms / 2,500 ms
コード長 874 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,392 KB
実行使用メモリ 245,440 KB
最終ジャッジ日時 2024-04-14 15:47:12
合計ジャッジ時間 24,052 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,620 KB
testcase_01 AC 38 ms
52,884 KB
testcase_02 AC 38 ms
53,136 KB
testcase_03 AC 41 ms
54,064 KB
testcase_04 AC 68 ms
72,464 KB
testcase_05 AC 40 ms
54,088 KB
testcase_06 AC 43 ms
55,396 KB
testcase_07 AC 69 ms
74,160 KB
testcase_08 AC 497 ms
173,824 KB
testcase_09 AC 465 ms
167,416 KB
testcase_10 AC 495 ms
175,736 KB
testcase_11 AC 361 ms
133,216 KB
testcase_12 AC 360 ms
128,644 KB
testcase_13 AC 386 ms
140,208 KB
testcase_14 AC 611 ms
190,160 KB
testcase_15 AC 503 ms
178,156 KB
testcase_16 AC 491 ms
179,656 KB
testcase_17 AC 383 ms
142,672 KB
testcase_18 AC 370 ms
131,472 KB
testcase_19 AC 486 ms
178,628 KB
testcase_20 AC 444 ms
159,132 KB
testcase_21 AC 504 ms
187,300 KB
testcase_22 AC 656 ms
203,944 KB
testcase_23 AC 542 ms
198,300 KB
testcase_24 AC 511 ms
170,800 KB
testcase_25 AC 469 ms
165,692 KB
testcase_26 AC 542 ms
198,912 KB
testcase_27 AC 546 ms
201,876 KB
testcase_28 AC 556 ms
206,668 KB
testcase_29 AC 563 ms
206,848 KB
testcase_30 AC 496 ms
176,652 KB
testcase_31 AC 556 ms
206,664 KB
testcase_32 AC 517 ms
195,212 KB
testcase_33 AC 451 ms
160,500 KB
testcase_34 AC 557 ms
207,124 KB
testcase_35 AC 472 ms
163,604 KB
testcase_36 AC 554 ms
199,444 KB
testcase_37 AC 559 ms
206,012 KB
testcase_38 AC 661 ms
198,656 KB
testcase_39 AC 413 ms
133,292 KB
testcase_40 AC 468 ms
155,272 KB
testcase_41 AC 677 ms
210,688 KB
testcase_42 AC 803 ms
245,440 KB
testcase_43 AC 45 ms
61,088 KB
testcase_44 AC 47 ms
67,220 KB
testcase_45 AC 272 ms
98,848 KB
testcase_46 AC 430 ms
101,096 KB
testcase_47 AC 309 ms
100,480 KB
testcase_48 AC 386 ms
101,040 KB
testcase_49 AC 271 ms
98,776 KB
testcase_50 AC 279 ms
98,936 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

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

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

            return t, start

    return [], -1


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