Editorial for COCI '23 Contest 4 #4 Putovanje
Submitting an official solution before solving the problem yourself is a bannable offence.
To check if a hotel can be located in a particular node (city), it is sufficient to calculate the minimum
distances from that node to all others using the breadth-first search algorithm and then compare the
obtained distances with the given distances from the task. If they match (except where ), the
hotel can be located in that node. The complexity of this check is
.
In the first and third subtasks, we check for each node in the same way whether a hotel can be located in
it. The overall complexity is .
In the second subtask, we are looking for nodes that are at a distance of from city
. A special case is
when all
, in which case the hotel can be in any node. We only run breadth-first search for one
city, so the complexity is
.
In the final subtask, we run multisource BFS. Instead of calculating distances for each node separately, we
compute them simultaneously for all nodes. The idea is to calculate, for each node ,
– the distance of
that node from the hotel, and
where
is the distance between
nodes
and
. In simple terms,
is the set of all nodes
from which we could obtain the given
(those
where distances
and
are not contradictory). The hotel can only be in nodes for which
and
.
Starting from the node with the largest distance , we perform a breadth-first search, simultaneously
adding nodes whose distance from the hotel equals the distance of the node in the current iteration of the
algorithm. If, from node
, we observe an edge to a node
that we have already visited, and
,
we add all nodes from set
to set
.
Set can have a size of
, so the complexity is not significantly improved. However, notice that bitset
supports all necessary operations! The overall complexity then becomes
, and it is sufficient to
solve the entire task.
You can find more about this structure at the link.
Comments