結果

問題 No.2565 はじめてのおつかい
ユーザー Lucagon
提出日時 2023-12-02 16:31:20
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 652 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 731,904 KB
最終ジャッジ日時 2024-09-26 20:23:14
合計ジャッジ時間 5,691 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 3 MLE * 1 -- * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from sys import setrecursionlimit
setrecursionlimit(10**9)

def dfs(pos,step,cnt):
    global ans
    if pos == 1 and cnt == 2:
        ans = min(ans,step)
        return
    if pos in visited: return
    
    visited.add(pos)
    
    for i in g[pos]:
        if pos == n or pos == n-1:
            dfs(i,step+1,cnt+1)
        else:
            dfs(i,step+1,cnt)

        visited.discard(pos)

n,m = map(int,input().split())
g = defaultdict(list)

for i in range(m):
    u,v = map(int,input().split())
    g[u].append(v)

visited = set()
ans = 10**6
dfs(1,0,0)

if ans == 10**6:
    print(-1)
    exit()

print(ans)
0