結果

問題 No.1647 Travel in Mitaru city 2
ユーザー tktk_snsn
提出日時 2021-08-13 22:03:50
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 966 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 72,576 KB
最終ジャッジ日時 2024-10-03 19:05:23
合計ジャッジ時間 41,803 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 WA * 2 RE * 16 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W, N = map(int, input().split())
X = [-1] * N
Y = [-1] * N
row = [[] for _ in range(H+1)]
col = [[] for _ in range(W+1)]
for i in range(N):
    X[i], Y[i] = map(int, input().split())
    row[X[i]].append(i)
    col[Y[i]].append(i)

v0 = [0] * N
v1 = [0] * N

pos = [X, Y]
G = [row, col]
vis = [v0, v1]
tour = []


def dfs(s, F, p=-1):
    vis[F][s] = 1
    tour.append(s + 1)
    for t in G[F][pos[F][s]]:
        if t == p or t == s:
            continue
        if vis[1-F][t]:
            tour.append(t + 1)
            return True
        if dfs(t, 1-F, s):
            return True
    tour.pop()
    vis[F][s] = 0
    return False


for i in range(N):
    if vis[0][i]:
        continue
    tour = []
    if dfs(i, 0):
        tour.reverse()
        last = tour.pop()
        idx = tour.index(last)
        print(len(tour) - idx)
        print(*tour[idx:])
        exit()
print(-1)
0