結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-28 08:39:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 864 ms / 2,500 ms
コード長 934 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 248,580 KB
最終ジャッジ日時 2024-04-14 15:52:28
合計ジャッジ時間 22,703 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,756 KB
testcase_01 AC 38 ms
52,904 KB
testcase_02 AC 38 ms
52,964 KB
testcase_03 AC 40 ms
54,576 KB
testcase_04 AC 51 ms
65,880 KB
testcase_05 AC 39 ms
53,636 KB
testcase_06 AC 40 ms
53,636 KB
testcase_07 AC 52 ms
67,696 KB
testcase_08 AC 475 ms
185,404 KB
testcase_09 AC 421 ms
172,520 KB
testcase_10 AC 467 ms
180,444 KB
testcase_11 AC 317 ms
133,348 KB
testcase_12 AC 305 ms
121,432 KB
testcase_13 AC 338 ms
138,612 KB
testcase_14 AC 578 ms
198,368 KB
testcase_15 AC 467 ms
180,292 KB
testcase_16 AC 454 ms
184,556 KB
testcase_17 AC 352 ms
143,028 KB
testcase_18 AC 326 ms
131,632 KB
testcase_19 AC 447 ms
178,756 KB
testcase_20 AC 399 ms
159,588 KB
testcase_21 AC 460 ms
181,224 KB
testcase_22 AC 609 ms
200,948 KB
testcase_23 AC 488 ms
184,332 KB
testcase_24 AC 468 ms
166,832 KB
testcase_25 AC 445 ms
172,504 KB
testcase_26 AC 479 ms
185,652 KB
testcase_27 AC 482 ms
186,844 KB
testcase_28 AC 494 ms
195,652 KB
testcase_29 AC 495 ms
193,648 KB
testcase_30 AC 454 ms
175,488 KB
testcase_31 AC 500 ms
195,476 KB
testcase_32 AC 464 ms
184,772 KB
testcase_33 AC 425 ms
168,288 KB
testcase_34 AC 495 ms
196,176 KB
testcase_35 AC 436 ms
171,324 KB
testcase_36 AC 496 ms
191,544 KB
testcase_37 AC 499 ms
193,284 KB
testcase_38 AC 626 ms
199,292 KB
testcase_39 AC 368 ms
135,508 KB
testcase_40 AC 427 ms
150,888 KB
testcase_41 AC 671 ms
213,352 KB
testcase_42 AC 864 ms
248,580 KB
testcase_43 AC 47 ms
61,872 KB
testcase_44 AC 48 ms
65,844 KB
testcase_45 AC 232 ms
98,700 KB
testcase_46 AC 384 ms
101,092 KB
testcase_47 AC 255 ms
99,172 KB
testcase_48 AC 336 ms
101,012 KB
testcase_49 AC 224 ms
98,556 KB
testcase_50 AC 230 ms
98,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline


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