Flood Fill
Time limit2sMemory limit128 MB
Given M points and a threshold D, group points whose taxicab distance is at most D into connected components, then report the number of components and the largest component size.
- Level
Hard8 of 10
- Topics
- Union-find, Sorting, Divide and conquer, Geometry
- Solved
- No attempts yet
Problem
There are points on the plane. The taxicab distance between two points and is defined as .
Two points are said to be directly connected if their taxicab distance is at most . A group of points that can reach one another by following a chain of direct connections forms a single island. In other words, an island is a connected component of the "directly connected" relation.
For the given points, find the number of islands and the size of the largest island (the number of points it contains).
When , only points on the same cell or one orthogonal step apart are connected, which is the classic Flood Fill problem. This problem generalises that threshold to an arbitrary .
Input
The first line contains the number of points and the distance threshold . (, )
Each of the next lines contains the coordinates and of a point. ()
The same coordinates may appear more than once; in that case the two points have taxicab distance and therefore always belong to the same island.
Output
Print the number of islands and the size of the largest island on one line, separated by a space.
Hint
Because the coordinate range is very large, comparing every pair of points directly can be slow. If you change coordinates to and , the taxicab distance becomes the Chebyshev distance . Then two points are directly connected exactly when and — that is, when they fall inside a common axis-aligned square of side in the transformed coordinates.