結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-28 08:39:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 718 ms / 2,500 ms
コード長 934 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 73,728 KB
最終ジャッジ日時 2024-04-21 04:46:21
合計ジャッジ時間 30,075 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,752 KB
testcase_01 AC 27 ms
10,624 KB
testcase_02 AC 29 ms
10,752 KB
testcase_03 AC 29 ms
10,880 KB
testcase_04 AC 34 ms
11,264 KB
testcase_05 AC 29 ms
10,880 KB
testcase_06 AC 29 ms
10,880 KB
testcase_07 AC 35 ms
11,520 KB
testcase_08 AC 662 ms
68,480 KB
testcase_09 AC 599 ms
63,616 KB
testcase_10 AC 629 ms
68,608 KB
testcase_11 AC 516 ms
58,112 KB
testcase_12 AC 496 ms
57,216 KB
testcase_13 AC 546 ms
61,044 KB
testcase_14 AC 688 ms
70,272 KB
testcase_15 AC 677 ms
69,376 KB
testcase_16 AC 609 ms
66,432 KB
testcase_17 AC 547 ms
60,912 KB
testcase_18 AC 560 ms
59,904 KB
testcase_19 AC 651 ms
73,584 KB
testcase_20 AC 617 ms
66,944 KB
testcase_21 AC 679 ms
72,064 KB
testcase_22 AC 681 ms
71,680 KB
testcase_23 AC 714 ms
71,936 KB
testcase_24 AC 654 ms
67,200 KB
testcase_25 AC 684 ms
68,864 KB
testcase_26 AC 710 ms
71,936 KB
testcase_27 AC 717 ms
72,320 KB
testcase_28 AC 715 ms
73,600 KB
testcase_29 AC 718 ms
73,600 KB
testcase_30 AC 695 ms
70,272 KB
testcase_31 AC 718 ms
73,600 KB
testcase_32 AC 718 ms
73,728 KB
testcase_33 AC 643 ms
68,096 KB
testcase_34 AC 707 ms
73,728 KB
testcase_35 AC 669 ms
68,480 KB
testcase_36 AC 718 ms
73,600 KB
testcase_37 AC 714 ms
73,600 KB
testcase_38 AC 639 ms
64,512 KB
testcase_39 AC 589 ms
60,416 KB
testcase_40 AC 636 ms
64,000 KB
testcase_41 AC 590 ms
60,544 KB
testcase_42 AC 640 ms
64,384 KB
testcase_43 AC 30 ms
10,880 KB
testcase_44 AC 100 ms
26,624 KB
testcase_45 AC 454 ms
52,352 KB
testcase_46 AC 587 ms
52,480 KB
testcase_47 AC 470 ms
52,352 KB
testcase_48 AC 490 ms
52,480 KB
testcase_49 AC 450 ms
52,352 KB
testcase_50 AC 455 ms
52,352 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