Solution AGC015C
Analysis
The key observation is this sentence:
For every pair of two blue square and , there is at most one path that starts from , repeatedly proceeds to an adjacent (side by side) blue square and finally reaches , without traversing the same square more than once.
This could be rewritten as
For each two node, only one simple path connects them
which is a definition of a forest.
With forests, we have the simple conclusion that, . This is intuitive in that, if the forest is reduced to a tree, then rhs is , which is yet another definition of a tree.
Based on this observation, we can use 2D prefix sum to maintain the rectangular sum of nodes, vertical edges and horizontal edges. Note that edges cannot lead to nodes out of the boundary.
Code
1 | const int N = 2020; |
Comment
Take note of the unconventional statements in the problem. In this problem, the given connected components are special in that they form several trees, and this is the key observation that ultimately leads to the solution of this problem.