Editorial for COCI '23 Contest 3 #5 Slučajna Cesta
Submitting an official solution before solving the problem yourself is a bannable offence.
First, we'll calculate the expected beauty of a path that starts at the root. We can calculate it with a standard
dynamic , where
is the beauty of park
and
is the probability that Vito goes from park
to park
(from now on we're assuming he starts in the
root). It only remains to calculate
.
In the third subtask the graph is a binary tree so we know that every vertex can have ,
or
children. If
has one child, the probability that the edge is directed downwards is
and in that case Vito takes that
road. If it has two children we have four cases: both edges directed downwards, only left edge downwards,
only right edge downwards and neither edge directed downwards. Each case has a probability of
,
in second (third) case Vito continues his path towards left (right) child, in fourth case his journey ends,
and in the first case he randomly chooses towards which child he'll go with probabilities
. Summing
up probabilities that Vito goes towards left child over all cases we get
. We get the same value for
probability he goes towards the right child and we can now solve
of third subtask.
Observation. Probability that from vertex Vito goes to his child
is equal for every child of
.
It follows from the symmetry, and we can convince ourselves by analysing cases for arbitrary pair of children similar to the former analysis.
Since the probability that Vito ends his path in vertex with
children is
(all edges directed upwards),
it follows that probability that he takes a path from
to its child
is equal to
.
Applying dynamic programming to all starting vertices we get a solution with time complexity
for
complete second subtask.
For final solution we have to apply rerooting. For that we need expected value of a path given that from the
starting vertex we go upwards. In case where parent of current vertex is not the root we can calculate that
value by adding expected beauty of path upwards of parent and subtractic expected beauty of path starting
at parent and going to current vertex: . And if the parent
is the root then we subtract the expected beauty of path towards current vertex and normalize probability
due to the change of number of children:
,
for
the number of children of root
.
Now we can calculate the expected beauty of path starting at arbitrary vertex with and
in
. Total time complexity is
.
For complete solution of the third subtask it was enough to calculate the probability for and apply
rerooting.
Alternatively, we can apply general rerooting algorithm in time complexity .
Comments