제출 #931615

#제출 시각아이디문제언어결과실행 시간메모리
931615dnialhJakarta Skyscrapers (APIO15_skyscraper)Pypy 3
57 / 100
1067 ms154632 KiB
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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...