結果

問題 No.1647 Travel in Mitaru city 2
ユーザー MitarushiMitarushi
提出日時 2021-07-28 07:18:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 789 ms / 2,500 ms
コード長 873 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 82,636 KB
実行使用メモリ 245,012 KB
最終ジャッジ日時 2024-04-14 15:47:37
合計ジャッジ時間 24,484 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,852 KB
testcase_01 AC 38 ms
53,028 KB
testcase_02 AC 37 ms
53,176 KB
testcase_03 AC 42 ms
54,172 KB
testcase_04 AC 68 ms
72,536 KB
testcase_05 AC 41 ms
53,760 KB
testcase_06 AC 43 ms
54,740 KB
testcase_07 AC 70 ms
74,516 KB
testcase_08 AC 493 ms
174,440 KB
testcase_09 AC 462 ms
167,532 KB
testcase_10 AC 489 ms
175,640 KB
testcase_11 AC 361 ms
133,920 KB
testcase_12 AC 358 ms
127,520 KB
testcase_13 AC 381 ms
141,024 KB
testcase_14 AC 606 ms
189,708 KB
testcase_15 AC 503 ms
178,456 KB
testcase_16 AC 480 ms
180,296 KB
testcase_17 AC 387 ms
143,144 KB
testcase_18 AC 372 ms
131,880 KB
testcase_19 AC 486 ms
178,628 KB
testcase_20 AC 441 ms
159,232 KB
testcase_21 AC 501 ms
187,548 KB
testcase_22 AC 651 ms
202,848 KB
testcase_23 AC 545 ms
198,984 KB
testcase_24 AC 508 ms
172,312 KB
testcase_25 AC 469 ms
165,720 KB
testcase_26 AC 540 ms
197,928 KB
testcase_27 AC 545 ms
199,732 KB
testcase_28 AC 563 ms
205,784 KB
testcase_29 AC 556 ms
205,908 KB
testcase_30 AC 489 ms
176,948 KB
testcase_31 AC 555 ms
205,864 KB
testcase_32 AC 516 ms
195,064 KB
testcase_33 AC 450 ms
160,992 KB
testcase_34 AC 558 ms
206,120 KB
testcase_35 AC 470 ms
163,920 KB
testcase_36 AC 549 ms
199,292 KB
testcase_37 AC 554 ms
205,396 KB
testcase_38 AC 663 ms
198,308 KB
testcase_39 AC 403 ms
133,464 KB
testcase_40 AC 475 ms
154,028 KB
testcase_41 AC 668 ms
210,096 KB
testcase_42 AC 789 ms
245,012 KB
testcase_43 AC 46 ms
61,196 KB
testcase_44 AC 47 ms
65,044 KB
testcase_45 AC 273 ms
98,900 KB
testcase_46 AC 422 ms
100,868 KB
testcase_47 AC 306 ms
100,188 KB
testcase_48 AC 388 ms
101,388 KB
testcase_49 AC 264 ms
98,564 KB
testcase_50 AC 271 ms
99,016 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
    is_h = i < h
    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, is_h)
                exit()

            return t, start

    return [], -1


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