Problem Translation
A vertex is represented by coordinates x, y). If two vertices are adjacent in the horizontal or vertical direction, the two vertices belong to one region, that is, the vertex 1x1, y1 ), point 2x2, y2) adjacent when and only when x1 = x2, | y1-y2 | = 1 or | x1-x2 | = 1, y1 = y2. Given some vertices, adjacent vertices form an area, and the maximum vertices that can be owned by k areas are obtained. K is not greater than the number of regions ).
Solution
The query set can be used. A region indicates a query set. When the two regions are adjacent, the query sets are merged. Record and query the number of vertices in the set at the same time.
Initially, each vertex is a parallel query set.
For vertices with the same x and y coordinates with a difference of 1, merge the points where they are located and query the set. When merging the points, you need to determine whether the two points are located in the same set. When merging, You need to first find the root node of the respective set, and then let one of the root nodes point to another root node to complete the merging.
Merge the points with the same y coordinate and one difference between x coordinate, and query the set.
After merging all adjacent points, sort the number of points in the query set from large to small, and take the sum of the first K values as the result output.
Problem source code, please visit: http://www.51ojr.com/report/full/44