18/03: Problem C

This commit is contained in:
augustin64 2024-04-02 22:26:39 +02:00
parent 7bf260628a
commit 59bcdd0401

50
18-03-24/c.py Normal file
View File

@ -0,0 +1,50 @@
## Récupération des entrées
n, m = [int(i) for i in input().split()]
# adj = [list() for i in range(n+1)]
anti_adj = [list() for i in range(n+1)]
for i in range(m):
u, v = [int(i) for i in input().split()]
anti_adj[v].append(u)
# adj[u].append(v)
k = int(input())
p = [int(i) for i in input().split()]
## Initialisation des structures
dist = [-1 for i in range(n+1)]
succ = {i+1:set() for i in range(n)}
size = {i+1:0 for i in range(n)}
non_vus = set((i+1 for i in range(n) if i+1 != p[-1]))
queue = [p[-1]]
dist[p[-1]] = 0
## Calcul des prochains avec leur coût, BFS par la fin
while len(queue) > 0:
nextQ = []
while len(queue) > 0:
s = queue.pop()
for pred in anti_adj[s]:
if pred in non_vus:
non_vus.remove(pred)
nextQ.append(pred)
dist[pred] = dist[s]+1
if dist[pred] == dist[s]+1:
succ[pred].add(s)
size[pred] += 1
queue = nextQ
## Calcul final
mini, maxi = 0, 0
position = p[0]
for i in range(1, k):
next_pos = p[i]
if next_pos not in succ[position]:
mini += 1
maxi += 1
elif len(succ[position]) > 1:
maxi += 1
position = next_pos
print(mini, maxi)