結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-27 23:25:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 977 ms / 2,500 ms
コード長 909 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 74,480 KB
最終ジャッジ日時 2024-04-21 04:48:27
合計ジャッジ時間 39,360 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,624 KB
testcase_01 AC 29 ms
10,624 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 30 ms
10,752 KB
testcase_04 AC 39 ms
11,392 KB
testcase_05 AC 29 ms
10,752 KB
testcase_06 AC 31 ms
10,880 KB
testcase_07 AC 45 ms
11,520 KB
testcase_08 AC 892 ms
68,608 KB
testcase_09 AC 827 ms
63,744 KB
testcase_10 AC 901 ms
68,608 KB
testcase_11 AC 733 ms
58,344 KB
testcase_12 AC 727 ms
57,344 KB
testcase_13 AC 787 ms
61,668 KB
testcase_14 AC 925 ms
70,272 KB
testcase_15 AC 905 ms
69,504 KB
testcase_16 AC 857 ms
66,304 KB
testcase_17 AC 775 ms
61,660 KB
testcase_18 AC 800 ms
60,240 KB
testcase_19 AC 924 ms
74,480 KB
testcase_20 AC 871 ms
66,944 KB
testcase_21 AC 950 ms
72,476 KB
testcase_22 AC 943 ms
71,680 KB
testcase_23 AC 938 ms
72,064 KB
testcase_24 AC 878 ms
67,328 KB
testcase_25 AC 883 ms
68,992 KB
testcase_26 AC 946 ms
71,936 KB
testcase_27 AC 943 ms
72,320 KB
testcase_28 AC 967 ms
73,600 KB
testcase_29 AC 942 ms
73,728 KB
testcase_30 AC 936 ms
70,272 KB
testcase_31 AC 945 ms
73,600 KB
testcase_32 AC 950 ms
73,728 KB
testcase_33 AC 913 ms
67,968 KB
testcase_34 AC 962 ms
73,728 KB
testcase_35 AC 917 ms
68,608 KB
testcase_36 AC 977 ms
73,728 KB
testcase_37 AC 972 ms
73,728 KB
testcase_38 AC 884 ms
64,640 KB
testcase_39 AC 837 ms
60,416 KB
testcase_40 AC 876 ms
64,128 KB
testcase_41 AC 830 ms
60,672 KB
testcase_42 AC 871 ms
64,384 KB
testcase_43 AC 31 ms
11,008 KB
testcase_44 AC 107 ms
26,624 KB
testcase_45 AC 682 ms
52,480 KB
testcase_46 AC 813 ms
52,352 KB
testcase_47 AC 674 ms
52,480 KB
testcase_48 AC 716 ms
52,352 KB
testcase_49 AC 668 ms
52,352 KB
testcase_50 AC 672 ms
52,352 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