結果

問題 No.1244 Black Segment
ユーザー titia
提出日時 2025-07-14 08:49:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 414 ms / 2,000 ms
コード長 563 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 40,832 KB
最終ジャッジ日時 2025-07-14 08:49:42
合計ジャッジ時間 10,145 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from collections import deque

N,M,A,B=map(int,input().split())

X=[]

for i in range(M):
    l,r=map(int,input().split())

    l=max(l,A)
    r=min(r,B)

    X.append((l,r))

E=[[] for i in range(N+1)]

for x,y in X:
    E[x-1].append(y)
    E[y].append(x-1)

DIS=[1<<60]*(N+1)

DIS[A-1]=0
Q=deque()
Q.append(A-1)

while Q:
    x=Q.popleft()

    for to in E[x]:
        if DIS[to]>DIS[x]+1:
            DIS[to]=DIS[x]+1
            Q.append(to)

ANS=DIS[B]

if ANS>1000000:
    print(-1)
else:
    print(ANS)
            
0