結果

問題 No.3263 違法な散歩道
ユーザー programogumogu
提出日時 2025-09-06 18:28:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 697 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 233,404 KB
最終ジャッジ日時 2025-09-06 18:29:02
合計ジャッジ時間 14,968 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
n,m = map(int,input().split())
h = [set() for i in range(n)]

for i in range(m):
  u,v = map(int,input().split())
  u -= 1
  v -= 1
  h[u].add(v)
  h[v].add(u)
#print(h)
k = int(input())
if k != 0:
  a = set([int(x)-1 for x in input().split()])
else:
  a = set()
ans = [10**18]
#(頂点i)+(遭遇回数)*(頂点数n)
dat = [True]*(5*n)
def bfs(i,prev,dist,n):
  #print(i,prev,dist,n)
  dat[i] = False
  if i%n == n-1:
    ans[0] = min(ans[0],dist)
  for j in h[i%n]:
    if j in a:
      nex = j+i//n*n+n
    else:
      nex = j
    if nex < 5*n and dat[nex]:
      bfs(nex,i,dist+1,n)

bfs(0,-1,0,n)
if ans[0] != 10**18:
  print(ans[0])
else:
  print(-1)
0