Constellations
InterviewTime limit1sMemory limit128 MB
Given up to 500 star coordinates, connect each star to its nearest neighbor(s) and count the connected components of the resulting graph.
- Level
Medium5 of 10
- Topics
- Graph, Union-find, Geometry, Implementation
- Solved
- No attempts yet
Problem
One night, while camping out under the open sky, Big Ed was gazing at the stars. Ed had never bothered to learn the constellations, but he decided that grouping the stars together was a sensible thing to do. Being methodical, he settled on the following simple rules:
- Every star belongs to the same constellation as its closest neighbor.
- If the closest neighbor is not unique, then the star and all of its closest neighbors belong to the same constellation.
- If A is in the same constellation as B, then B is in the same constellation as A.
- If A is in the same constellation as B, and B is in the same constellation as C, then A is in the same constellation as C.
Here, "closest" is measured by the ordinary Euclidean distance between the two stars' coordinates.
For example, if the sky looked like the following:

then there are 3 constellations: {1, 2, 3, 4, 5}, {6, 7, 8}, {9, 10}.
Input
The input consists of a sequence of sky descriptions. Each description begins with a line containing a single integer (), the number of stars.
The next lines each give the coordinates of one star as two integers and ().
A line containing marks the end of the input.
Output
For each sky description, print a single line of the form
Sky s contains c constellations.
where is the number of the sky description (starting from 1) and is the number of constellations.