## Editorial for Yet Another Contest 4 P3 - Canadian Cactus Competition

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Josh

Note that there may be multiple valid schemes of assigning values, with one such scheme being mentioned below.

In this subtask, the beauty of the graph can only be or . For the beauty of the graph to be , every edge must connect a node with a value of and a node with a value of . This corresponds to a -colouring of the graph. Hence, all we need to do is to check if the graph is bipartite.

Time complexity:

Let . Since we are only dealing with bits, the maximum possible beauty of the graph is . For the graph to have a beauty of , every edge must have a colour of . If we consider the -th bit (the most significant bit), then every edge must connect a node with a value that contains this bit and node with a value that doesn't contain this bit. Hence, the graph must be bipartite. Therefore, if the graph is bipartite, we can generate a -colouring, set all nodes of one colour to have a value of , and set all nodes of the other colour to have a value of .

Otherwise, the beauty of the graph cannot have the -th bit set, so the maximum possible beauty of the graph is . It turns out that this beauty is always achievable, with the following construction:

First, consider any spanning tree of the graph, and find its -colouring. For all nodes of one colour, we will set their values to , and for all nodes of the other colour, we will set their values to . As long as we do not modify the -th bit any further, then the colours of all edges in the spanning tree will have the -th bit set, and hence be greater than .

Then, consider the edges not part of the spanning tree. Due to the properties of a cactus graph, these edges must form an acyclic subgraph (can you work out the details why?). We can generate a -colouring of this subgraph. For all nodes of one colour, we will increase their value by . Then, all edges in this subgraph must have the -th bit set for , which is sufficient to guarantee that their value is not less than .

Time complexity:

Let be the largest integer such that . Then, since we are only dealing with bits, the maximum possible beauty of the graph is . For the same reason as in subtask , this is possible if and only if the graph is bipartite. If the graph is bipartite, we can construct a solution by generating a -colouring of the graph, setting all nodes of one colour to have a value of , and setting all nodes of the other colour to have a value of .

Otherwise, the beauty of the graph cannot have the -th bit set, so the maximum possible beauty of the graph is . It turns out that this beauty is always achievable. To do this, generate a -colouring of the graph (this is guaranteed to exist). Then, set all nodes of the first colour to have a value of , all nodes of the second colour to have a value of , and all nodes of the third colour to have a value of .

To generate a -colouring of the graph, it suffices to perform a DFS or BFS over the graph. When we visit a node for the first time, we will set its colour to be any colour such that no adjacent node has already been visited and assigned that colour. The details of the proof of why this works is left as an exercise to the reader (hint: use the block graph to show that when a node is visited, at most two of its neighbours have already been visited).

To simplify implementation, we note that if we always choose the largest available colour when processing a node for the first time, then if the graph is bipartite, we will automatically find a -colouring. Thus, we can handle both cases at the same time without needing to check whether the graph is bipartite.

Time complexity: