POI - 2018 - Bike Paths
Author: Neo Wang
Explanation
Notice that groups of intersections can form strongly connected components. Therefore, treat each component as a node with initial value equal to the size of the component. Since the resulting strongly connected components form a directed acyclic graph (specifically a tree, because of the "justness" from the problem statement), process the edges in reverse topographical order with the following relation where is the # of intersections that can be reached from component :
The final answer for intersection is , since we exclude the starting intersection from the answer.
Warning!
This solution only works on a tree (and not a general DAG), because of over counting.
Suppose we have a graph with nodes, and the edges
1 2 1 3 2 4 3 4
which correspond to the edges .
A valid topographical sort could be in the order , which would not work because node is counted twice. Our calls would be as follows:
Evidently, is counted twice in .
Implementation
Time Complexity:
C++
Code Snippet: Benq Template (Click to expand)/*** Description: Kosaraju's Algorithm, DFS twice to generate* strongly connected components in topological order. $a,b$* in same component if both $a\to b$ and $b\to a$ exist.* Time: O(N+M)* Source: Wikipedia* Verification: POI 8 peaceful commission*/
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!