To solve this problem, we need to determine if a robot starting at node s can reach node d exactly at time t in a directed graph where all edges have length 1.
Approach
The key insights to solve this problem are:
- Parity Check: Since each edge takes exactly 1 time unit, the time to reach any node from
shas a parity (even or odd). We need to check ifthas the same parity as any valid path fromstod. - Minimal Time: Compute the minimal time to reach
dwith both even and odd parity using BFS. - Cycle Check: If there exists an odd-length cycle in the strongly connected component (SCC) of
d, the robot can adjust its time by looping this cycle to reacht(even ifthas a different parity than the minimal time). If no such cycle exists, the time difference betweentand the minimal time must be even.
Solution Code
import sys
from collections import deque
def main():
input = sys.stdin.read().split()
ptr = 0
n = int(input[ptr])
ptr +=1
m = int(input[ptr])
ptr +=1
adj = [[] for _ in range(n+1)] # nodes are 1-based?
for _ in range(m):
u = int(input[ptr])
ptr +=1
v = int(input[ptr])
ptr +=1
adj[u].append(v)
s = int(input[ptr])
ptr +=1
d = int(input[ptr])
ptr +=1
t = int(input[ptr])
ptr +=1
# Step1: Compute even and odd minimal times from s
INF = float('inf')
even = [INF]*(n+1)
odd = [INF]*(n+1)
q = deque()
even[s] =0
q.append( (s, 0) ) # (node, parity:0 even,1 odd)
while q:
u, p = q.popleft()
current_time = even[u] if p ==0 else odd[u]
for v in adj[u]:
new_p = p ^ 1
new_time = current_time + 1
if new_p == 0:
if new_time < even[v]:
even[v] = new_time
q.append( (v, new_p) )
else:
if new_time < odd[v]:
odd[v] = new_time
q.append( (v, new_p) )
# Step 2: Check if t is possible with parity
target_parity = t % 2
if target_parity == 0:
if even[d] == INF:
print("No")
return
min_t = even[d]
else:
if odd[d] == INF:
print("No")
return
min_t = odd[d]
if t < min_t:
print("No")
return
# Step3: Check if (t - min_t) even OR d's SCC has odd cycle
if (t - min_t) % 2 == 0:
print("Yes")
return
# Now need to check if d's SCC has odd cycle
# First find SCC of d using Kosaraju's algorithm
# Step 3a: Find all nodes reachable from d (forward pass)
reachable = [False]*(n+1)
q = deque()
q.append(d)
reachable[d] = True
while q:
u = q.popleft()
for v in adj[u]:
if not reachable[v]:
reachable[v] = True
q.append(v)
# Step3b: Reverse the graph
adj_rev = [[] for _ in range(n+1)]
for u in range(1, n+1):
for v in adj[u]:
adj_rev[v].append(u)
# Step3c: Find nodes that can reach d (backward pass)
can_reach_d = [False]*(n+1)
q.append(d)
can_reach_d[d] = True
while q:
u = q.popleft()
for v in adj_rev[u]:
if not can_reach_d[v]:
can_reach_d[v] = True
q.append(v)
# SCC of d is nodes where reachable and can_reach_d are True
scc = [u for u in range(1, n+1) if reachable[u] and can_reach_d[u]]
# Now check if this SCC has an odd cycle
# BFS to check parity conflict
parity = {}
q = deque()
has_odd_cycle = False
if d in scc:
parity[d] =0
q.append(d)
while q and not has_odd_cycle:
u = q.popleft()
for v in adj[u]:
if v not in scc:
continue
if v not in parity:
parity[v] = parity[u] + 1
q.append(v)
else:
if parity[u] + 1 != parity[v]:
has_odd_cycle = True
break
if has_odd_cycle:
break
if has_odd_cycle:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
Explanation
- Parity and Minimal Time: We compute the minimal time to reach each node from
susing BFS, tracking both even and odd parity times. - Reachability Check: We check if
dis reachable with the parity oftand iftis at least the minimal time for that parity. - Cycle Detection: We find the SCC of
dand check if it contains an odd-length cycle. If it does, the robot can adjust its time to reacht; otherwise, the time difference must be even. This ensures we handle all possible cases for the robot to reachdat exactly timet.
This approach efficiently handles all edge cases and provides the correct result based on the problem's constraints.


作者声明:本文包含人工智能生成内容。