Editorial for The Contest Contest 1 P6 - A Typical DMOPC Problem
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For the remainder of the editorial, we'll let  denote the degree of node 
.
Since the graph is a tree, there exists a unique path from node  to 
. Observe that in order for Alice to always reach node 
, this path must be a line graph. That is, 
 and every intermediate node on the path must have degree 
.
Why is this? Consider what happens if Alice arrives at a node  with 
. If 
, Alice is at a dead end and can't continue. If 
, Alice chooses between two edges, of which only one of them is on the path to node 
. No matter how Alice reaches node 
, there is always the possibility of Alice not taking the edge on the path to node 
.
Once we determine if the path satisfies the above requirements, any assignment of edge weights should suffice since Alice's path is already predetermined.
Time Complexity:Subtask 2 Hint
Build a graph where every node can reach node .
Subtask 2
In this subtask, Alice always chooses between  edges at each node.
Consider a directed acyclic graph  of maximal size with the following properties:
- Node 
is in
and has an outdegree of
 - Every node 
excluding node
has an outdegree of exactly
 
It follows that if Alice starts at a node in , she can always be forced to reach node 
 since we can set the 
 outgoing edges to be the min and max edge at her current node. Furthermore, if Alice doesn't start in 
, we cannot guarantee that Alice can always reach node 
. Why? Since no matter how she got to that node, she will choose between 
 edges, of which at most 
 of them takes her into 
. This means she can always take an edge to some node not in 
. This results in an infinite loop, so Alice can never reach node 
.
Thus, we want to determine if node  is in 
. This inspires us to run our own version of topological sort.
We try to construct , which initially only contains node 
. On each iteration, we find a node 
 that has at least 
 edges connecting to nodes in 
. If such an 
 exists, we add it and the 
 edges we used to 
, where the 
 edges become outgoing edges from 
. We repeat this process until node 
 is in 
 or there are no more nodes to add.
How can we assign edge weights to maintain these properties? When we add the  edges to 
, we can set their weights to be the minimum and maximum unused edge weights (call them 
 and 
 respectively with initial values of 
 and 
). This works because when we label the remaining edges that connect to node 
, they will have a weight strictly between 
 and 
. Hence Alice will still pick the same edges, no matter how she arrived at node 
.
If node  is in 
 after running the "topological sort", we simply label the remaining edges with weights in the interval 
 and we're done. Otherwise, there is no solution.
Subtask 3 Hint
What happens if Alice visits node  again? When would this make a difference?
Subtask 3
In this subtask, the number of edges Alice chooses at each node can vary. We let  represent the number of edges Alice could take if she was at node 
. We have:
To accomodate this, we modify the second requirement of  so that every node 
 has an outdegree of exactly 
.
We first run the same process described in subtask 2 but with some slight modifications to account for when . If this does not yield a solution, there is one remaining case to check for. To see it, we make the following observations:
Let's examine node  and the definition of 
. When Alice is initially at node 
, 
. However, if Alice arrives at node 
 again, we now have 
. In particular, the value of 
 changes if 
 or 
. If 
, Alice will stop at node 
 so this doesn't work. If 
, there are two cases. Let 
 denote the number of edges that connect a node 
 to a node in 
. If 
, there is still no solution. However, if 
, a solution may exist. Starting from node 
, Alice could either take the edge that leads to 
 or take the other edge. If Alice takes the latter edge, it is possible for Alice to eventually be forced to either take an edge that goes into 
 or take this edge back to node 
, which forces her into 
.
To check for this case, we again run the "topological sort" described in subtask 2, except this time we define  to be 
. Furthermore, we let 
 denote the order of when node 
 was added to 
. Thus, 
 has the following additional properties:
- For all 
, the
outgoing edges from node
connect to nodes with lower
values.
 
We claim that a solution exists if and only if the following conditions hold:
- There exists an edge 
connecting nodes
and
, where
and
. WLOG
.
 - Let 
represent the sequence of nodes on a path of length
from node
to node
(
and
), where
.
Then letdenote the sequence of edges on this path, where
is the edge she takes to get from node
to
. This path must exist and
.
 
In addition, we have a process of determining such , if they exist.
Proof of claim and outline of process
Consider the following process of determining a possible  and 
:
The base case is when Alice starts at node . Recall that 
 so she has two choices:
- Take the edge that connects to the node in 
with the lower
value. She is now guaranteed to reach node
.
 - Take the edge that connects to a node in 
with the higher
value. Call this node
.
 
Now suppose that Alice is at some node , 
, where 
 (the edge she took to get to 
) is in 
 and 
. Notice that there are now 
 outgoing edges Alice could take to be guaranteed to reach node 
, since 
 is one of the 
 outgoing edges and she can't take the same edge consecutively. Hence, Alice could take one other edge. Call this edge and the node it leads to as 
 and 
 respectively. We consider the possible cases:
- Case 1: 
We haveand
, so we continue the process with node
.
 - Case 2: 
Observe that this edge must connect to a node in(that is,
). If this wasn't the case, when Alice arrives at node
she is not guaranteed to reach node
(recall that a node is in
iff she is guaranteed to reach node
from that node). Next, observe that if Alice took edge
to
, she is guaranteed to reach node
since she can be forced to take any of the
outgoing edges from node
. We take
to be
,
to be
,
to be
,
to be
and we're done.
 
Since  is finite and 
, at some point we must reach case 2 and terminate the process.
Using the process outlined in the above proof, we can find a possible  and 
 by performing a DFS traversal starting from node 
. Implementation is left as an exercise to the reader. If such 
 and 
 exist, we rerun "topological sort" in order to relabel the edges. The following modifications are made:
- Initialize 
(recall
is the maximum unused edge weight) as
, since we reserve
for labelling edges in
.
 - If the current node 
for some
, we know that one of the outgoing edges is
. Set the weight of
to be
. If
, set the other edge to be the minimum unused edge. After labelling, the edges
will have weights of
respectively.
 
This assignment of edge weights guarantees that Alice must always reach node , the proof of which is left as an exercise for the reader.
Time Complexity: 
Comments