結果

問題 No.1647 Travel in Mitaru city 2
ユーザー H3PO4
提出日時 2024-03-09 17:42:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,224 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 82,636 KB
実行使用メモリ 129,896 KB
最終ジャッジ日時 2024-09-29 21:16:52
合計ジャッジ時間 31,611 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 3 WA * 45
権限があれば一括ダウンロードができます

ソースコード

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())
    rows[x].append(i)
    columns[y].append(i)

G = [[] for _ in range(N)]
for row in rows:
    for i in range(len(row) - 1):
        G[row[i]].append(row[i + 1])
        G[row[i + 1]].append(row[i])
for column in columns:
    for i in range(len(column) - 1):
        G[column[i]].append(column[i + 1])
        G[column[i + 1]].append(column[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)
        for x in G[v]:
            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