結果

問題 No.1244 Black Segment
ユーザー brthyyjp
提出日時 2022-03-24 18:55:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 621 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 82,284 KB
実行使用メモリ 96,140 KB
最終ジャッジ日時 2024-10-13 02:35:38
合計ジャッジ時間 7,662 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

from collections import deque
INF = 10**18

n, m, a, b = map(int, input().split())
edge = [[] for i in range(n+2)]
for i in range(m):
    l, r = map(int, input().split())
    r += 1
    l = max(l, a)
    r = min(r, b+1)
    edge[l].append(r)
    edge[r].append(l)

q = deque([])
q.append(a)
dist = [INF]*(n+2)
dist[a] = 0
while q:
    v = q.popleft()
    for u in edge[v]:
        if dist[u] != INF:
            continue
        dist[u] = dist[v]+1
        q.append(u)

ans = dist[b+1]
if ans < INF:
    print(ans)
else:
    print(-1)
0