Editorial for COCI '17 Contest 2 #5 Usmjeri
Submitting an official solution before solving the problem yourself is a bannable offence.
To begin with, we'll make node the root of the tree. For node that is not a root, let denote its parent. Instead of directing the edges, let's imagine we're colouring them in two colours so that one colour denotes that the edge is directed from the child to the parent, and the other from the parent to the child. Let's observe two nodes and . Let node be their lowest common ancestor (LCA). Notice that there is a path from to or from to if and only if the following three conditions are met:
- All edges on the path from to are of the same colour
- All edges on the path from to are of the same colour
- If is different from and , then edges and are of different colours
Let's now construct a graph where the nodes denote the edges of the given tree in the following way. For each given pair with LCA , we will add the following edges to the graph:
- The nodes that represent the adjacent edges on the path from to will be connected with a blue edge
- The nodes that represent the adjacent edges on the path from to will be connected with a blue edge
- If is different from and , the nodes that represent edges , will be connected with a red edge
Now we want to know the number of possible ways to colour the nodes of this graph in two colours so that the nodes connected with a blue edge are of the same colour, and the ones connected with a red edge are of different colours. We can see that the connected components of the graph are mutually independent, so the solution is equal to the product of solutions by individual components. Furthermore, we can see that the colour of a node uniquely identifies the colours of all the other nodes in its component. Additionally, if we have a valid colouring scheme, it stays valid if we change the colour of all the nodes. This means that each component has or valid colouring schemes, i.e. we only need to determine whether such a colouring scheme exists. We can do this with a DFS algorithm, starting from an arbitrary node and spread recursively, changing the colour when we reach a red edge and taking care of possible colouring contradictions. If there are no contradictions in any of the components, the final solution will be , where is the number of components, otherwise it is .
Now we are only left with constructing the aforementioned graph. A naive construction is of the complexity and wasn't fast enough for all the points. Notice that for each node of the tree, except the root and its children, we need to determine whether the nodes that represent edges and are connected with a blue edge. We can do this by using a recursive function that returns the minimal depth of a node such that we have added blue edges on the path from that node to a node in the subtree of . If the value of is smaller than the depth of , then we add the blue edge, otherwise we don't. The value of is calculated by taking into account its values for the children of and also - the minimal depth of a node such that we have added blue edges on the path from that node to . We can get these values by, for each given pair with LCA , we update the values and with the depth of node if it is smaller than the values , or , so far.
The total time complexity of this solution is , and memory .
Comments