結果

問題 No.1647 Travel in Mitaru city 2
ユーザー sotanishy
提出日時 2021-08-15 00:28:13
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,042 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 81,724 KB
実行使用メモリ 135,180 KB
最終ジャッジ日時 2024-10-13 03:28:52
合計ジャッジ時間 21,685 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

H, W, N = map(int, input().split())
G = [set() for _ in range(H+W)]
for i in range(N):
    x, y = map(lambda x: int(x) - 1, input().split())
    G[x].add((H+y, i))
    G[H+y].add((x, i))
st = []
for v in range(H+W):
    if len(G[v]) == 1:
        st.append(v)
while st:
    v = st.pop()
    for u, i in G[v]:
        G[u].remove((v, i))
        if len(G[u]) == 1:
            st.append(u)
    G[v] = set()
prev = [None] * (H+W)
for s in range(H):
    if prev[s] is not None:
        continue
    st = [(s, -1, -1)]
    while st:
        v, p, j = st.pop()
        prev[v] = (p, j)
        for u, i in G[v]:
            if (u, i) == prev[v]:
                continue
            if u == s:
                ans = [i]
                while v != s:
                    v, i = prev[v]
                    ans.append(i)
                print(len(ans))
                print(*map(lambda x: x + 1, ans[::-1]))
                exit()
            if prev[u] is None:
                st.append((u, v, i))
print(-1)
0