結果

問題 No.1647 Travel in Mitaru city 2
ユーザー H3PO4
提出日時 2021-08-13 22:29:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,000 bytes
コンパイル時間 470 ms
コンパイル使用メモリ 82,268 KB
実行使用メモリ 115,468 KB
最終ジャッジ日時 2024-10-03 20:08:23
合計ジャッジ時間 18,043 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 WA * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

H, W, N = map(int, input().split())
rows = [[] for _ in range(H)]
columns = [[] for _ in range(W)]

points = []
for i in range(N):
    x, y = (int(x) - 1 for x in input().split())
    points.append((x, y))
    rows[x].append(i)
    columns[y].append(i)

parent = [None] * N
nonvisited = set(range(N))

while nonvisited:
    v0 = nonvisited.pop()
    d = deque([(v0, -1)])
    while d:
        v, p = d.pop()
        parent[v] = p
        nonvisited.discard(v)
        row, col = points[v]
        for x in rows[row] + columns[col]:
            if x == v or x == p:
                continue
            if x in nonvisited:
                d.append((x, v))
            else:
                # cycle found
                ans = [v + 1]
                y = parent[v]
                while y != v and y != -1:
                    ans.append(y + 1)
                    y = parent[y]
                print(len(ans))
                print(*ans)
                exit()
print(-1)
0