POJ 1838 Problem Solving report

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.