Editorial for Facebook Hacker Cup '15 Round 3 P3 - Gentrification
Submitting an official solution before solving the problem yourself is a bannable offence.
In the given directed graph of neighbourhoods and roads, two nodes  and 
 may be selected for gentrification either if both of them are reachable from one another, or if neither of them is reachable from the other. Note that the former holds if and only if 
 and 
 are in the same strongly connected component (SCC). The latter holds if and only if their respective SCCs are not reachable from one another. From this, it's not hard to see that each SCC can be considered as a whole – it's optimal to either use all of its nodes, or none of them.
As such, let's start by converting the given directed graph of neighbourhoods and roads into a second graph, by collapsing each SCC into a single node labeled with a weight equal to the number of nodes from the original graph which are in that component. All of the SCCs can be found in  time due to Kosaraju's algorithm, and the second graph can be assembled from them (joined by corresponding edges present in the original graph) in 
 time as well. Note that this new graph must be a directed acyclic graph (DAG).
We're now trying to find an antichain in this DAG with a maximized sum of node weights. An antichain is a set of nodes, such that no two of them are reachable from one another. To solve this problem, we'll need to convert this second graph into a third graph – an "equivalent" (for our antichain-finding purposes) DAG with unweighted nodes, and then find the largest size of an antichain in it (in other words, its width).
The third graph can be constructed by considering each node  in the second graph in turn. Let's say that node 
 has a set of incoming edges 
, a set of outgoing edges 
, and a weight of 
. In the third graph, this single node will be replaced by a node 
 at which all edges in 
 will end, a node 
 at which all edges in 
 will begin, and a set of 
 nodes linking nodes 
 and 
 in parallel. That is, for each of these 
 nodes, there should be an edge from 
 to it, and from it to 
. Intuitively, it can be seen that all 
 of these intermediate nodes may be included in an antichain (but not along with 
 or 
), just as the 
 nodes in the corresponding SCC in the original graph may be gentrified together. As a final note, this third graph is guaranteed to have at most 
 nodes.
Finally, the width of this final DAG may be found due to Dilworth's Theorem by computing its transitive closure, constructing a bipartite graph with  nodes based on it, and finding the size of the maximum matching in it. Computing the transitive closure and matching each take at most 
 time, which is acceptable.
Comments