結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-27 23:40:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 793 ms / 2,500 ms
コード長 886 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 245,020 KB
最終ジャッジ日時 2024-04-14 15:48:06
合計ジャッジ時間 24,146 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,016 KB
testcase_01 AC 39 ms
52,924 KB
testcase_02 AC 39 ms
52,528 KB
testcase_03 AC 41 ms
53,616 KB
testcase_04 AC 67 ms
72,764 KB
testcase_05 AC 40 ms
53,832 KB
testcase_06 AC 42 ms
54,516 KB
testcase_07 AC 70 ms
74,028 KB
testcase_08 AC 516 ms
173,748 KB
testcase_09 AC 467 ms
168,480 KB
testcase_10 AC 503 ms
176,672 KB
testcase_11 AC 362 ms
132,928 KB
testcase_12 AC 365 ms
128,248 KB
testcase_13 AC 382 ms
139,336 KB
testcase_14 AC 615 ms
189,516 KB
testcase_15 AC 507 ms
178,128 KB
testcase_16 AC 496 ms
179,660 KB
testcase_17 AC 387 ms
142,652 KB
testcase_18 AC 373 ms
130,720 KB
testcase_19 AC 483 ms
179,068 KB
testcase_20 AC 442 ms
158,128 KB
testcase_21 AC 504 ms
186,832 KB
testcase_22 AC 674 ms
203,724 KB
testcase_23 AC 552 ms
197,988 KB
testcase_24 AC 503 ms
170,852 KB
testcase_25 AC 473 ms
166,372 KB
testcase_26 AC 544 ms
197,816 KB
testcase_27 AC 547 ms
201,796 KB
testcase_28 AC 558 ms
206,644 KB
testcase_29 AC 554 ms
207,344 KB
testcase_30 AC 494 ms
177,552 KB
testcase_31 AC 560 ms
207,148 KB
testcase_32 AC 520 ms
196,132 KB
testcase_33 AC 454 ms
159,900 KB
testcase_34 AC 557 ms
206,696 KB
testcase_35 AC 472 ms
163,164 KB
testcase_36 AC 550 ms
199,340 KB
testcase_37 AC 558 ms
206,200 KB
testcase_38 AC 665 ms
198,316 KB
testcase_39 AC 409 ms
131,600 KB
testcase_40 AC 471 ms
154,184 KB
testcase_41 AC 677 ms
209,516 KB
testcase_42 AC 793 ms
245,020 KB
testcase_43 AC 46 ms
61,012 KB
testcase_44 AC 47 ms
65,864 KB
testcase_45 AC 273 ms
98,632 KB
testcase_46 AC 431 ms
100,608 KB
testcase_47 AC 308 ms
100,104 KB
testcase_48 AC 380 ms
101,008 KB
testcase_49 AC 266 ms
98,520 KB
testcase_50 AC 272 ms
98,696 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):
    is_h = i < h
    visited[i] = True

    for j, idx in edge[i]:
        if j == prev:
            continue

        if visited[j]:
            return [idx], j

        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