結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-28 08:21:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,072 ms / 2,500 ms
コード長 879 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 211,688 KB
最終ジャッジ日時 2024-04-14 15:51:59
合計ジャッジ時間 24,607 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,016 KB
testcase_01 AC 44 ms
52,400 KB
testcase_02 AC 38 ms
52,264 KB
testcase_03 AC 42 ms
54,356 KB
testcase_04 AC 69 ms
74,008 KB
testcase_05 AC 41 ms
53,828 KB
testcase_06 AC 44 ms
55,812 KB
testcase_07 AC 68 ms
74,376 KB
testcase_08 AC 503 ms
173,496 KB
testcase_09 AC 464 ms
167,672 KB
testcase_10 AC 492 ms
175,548 KB
testcase_11 AC 366 ms
132,700 KB
testcase_12 AC 364 ms
127,784 KB
testcase_13 AC 392 ms
139,836 KB
testcase_14 AC 619 ms
190,528 KB
testcase_15 AC 506 ms
178,640 KB
testcase_16 AC 481 ms
180,016 KB
testcase_17 AC 396 ms
142,916 KB
testcase_18 AC 372 ms
130,844 KB
testcase_19 AC 484 ms
178,192 KB
testcase_20 AC 442 ms
158,440 KB
testcase_21 AC 497 ms
186,692 KB
testcase_22 AC 652 ms
202,184 KB
testcase_23 AC 543 ms
199,768 KB
testcase_24 AC 501 ms
170,248 KB
testcase_25 AC 467 ms
165,612 KB
testcase_26 AC 540 ms
199,780 KB
testcase_27 AC 541 ms
200,908 KB
testcase_28 AC 545 ms
206,852 KB
testcase_29 AC 551 ms
206,224 KB
testcase_30 AC 483 ms
176,012 KB
testcase_31 AC 552 ms
207,320 KB
testcase_32 AC 516 ms
195,600 KB
testcase_33 AC 451 ms
159,704 KB
testcase_34 AC 550 ms
206,760 KB
testcase_35 AC 460 ms
163,828 KB
testcase_36 AC 546 ms
199,940 KB
testcase_37 AC 549 ms
205,848 KB
testcase_38 AC 691 ms
211,688 KB
testcase_39 AC 402 ms
132,440 KB
testcase_40 AC 465 ms
155,180 KB
testcase_41 AC 619 ms
196,464 KB
testcase_42 AC 546 ms
165,628 KB
testcase_43 AC 43 ms
61,492 KB
testcase_44 AC 45 ms
65,704 KB
testcase_45 AC 271 ms
98,692 KB
testcase_46 AC 1,072 ms
112,528 KB
testcase_47 AC 309 ms
100,168 KB
testcase_48 AC 587 ms
105,052 KB
testcase_49 AC 269 ms
98,520 KB
testcase_50 AC 269 ms
98,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(500000)


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
    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, i < h)
                exit()

            return t, start

    return [], -1


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