This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
adj = [[] for _ in range(n)]
sta = []
power = []
for _ in range(m):
bi, pi = map(int, input().split())
adj[bi].append(_)
sta.append(bi)
power.append(pi)
seen = [0] * n
pair = set()
amt = 0
st = [(sta[0], 0)]
while not seen[sta[1]]:
nex = []
def add(u, v):
if 0 <= u < n:
if (u, v) not in pair:
pair.add((u, v))
nex.append((u, v))
while st:
loc, p = st.pop()
if not seen[loc]:
seen[loc] = 1
for u in adj[loc]:
st.append((loc, power[u]))
add(loc + p, p)
add(loc - p, p)
amt += 1
st = nex
if st == [] and not seen[sta[1]]:
print(-1)
sys.exit()
print(amt - 1)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |